- 下載workerman源碼包
- 在start.php中引入源碼,將源碼包vendor目錄放在與start.php同級(jí)目錄
1. start.php 代碼如下:
<?php
require_once 'vendor/autoload.php';
use Workerman\Worker;
/**
* 多端口擦盾,多協(xié)議
*/
// 使用websocket協(xié)議
$ws_worker = new Worker("websocket://0.0.0.0:10086");
// 啟動(dòng)進(jìn)程對(duì)外服務(wù)妇多。如果進(jìn)程啟動(dòng)后建立一個(gè)內(nèi)部通訊端口氯哮,則進(jìn)程數(shù)必須為1
$ws_worker->count = 1;
// 連接事件,阻斷非法連接
$ws_worker->onConnect = function ($connection) use ($domains)
{/*{{{*/
// $connection->onWebSocketConnect = function ($connection, $http_header) use ($domains)
// {
// // 判斷連接來(lái)源是否合法脂凶,不合法關(guān)閉連接
// if (!in_array(parse_url($_SERVER['HTTP_ORIGIN'])['host'], $domains)) {
// $connection->close();
// }
// $connection->send("connection success.");
// };
};/*}}}*/
// 進(jìn)程啟動(dòng)后建立一個(gè)內(nèi)部通訊端口
$ws_worker->onWorkerStart = function ($ws_worker)
{/*{{{*/
/**
* 多個(gè)進(jìn)程監(jiān)聽(tīng)同一個(gè)端口(監(jiān)聽(tīng)套接字不是繼承自父進(jìn)程)
* 需要開啟端口復(fù)用敲街,不然會(huì)報(bào)Address already in use錯(cuò)誤
*/
// 開啟一個(gè)內(nèi)部端口鼻百,方便內(nèi)部系統(tǒng)推送數(shù)據(jù)绞旅,text協(xié)議格式,文本+換行符
/* $inner_text_worker = new Worker("Text://0.0.0.0:10087");
$inner_text_worker->onMessage = function ($connection, $buffer) use ($ws_worker)
{
$data = json_decode($buffer, true);
$id = $data['id'];
// 通過(guò)workerman向id的頁(yè)面推送數(shù)據(jù)
$ret = sendMessageById($id, $buffer);
// 返回推送結(jié)果
$connection->send($ret ? 'ok' : 'fail');
};
$inner_text_worker->listen(); */
$inner_text_worker = new Worker("http://0.0.0.0:10087");
$inner_text_worker->onMessage = function ($http) use ($ws_worker)
{
$id = $_POST['id'];
$status = $_POST['status'];
// 通過(guò)workerman向id的頁(yè)面推送數(shù)據(jù)
$ret = sendMessageById($id, ['id', 'status' => $status]);
// 返回推送結(jié)果
$http->send($ret ? 'ok' : 'fail');
};
$inner_text_worker->listen();
};/*}}}*/
// 增加一個(gè)屬性温艇,用來(lái)保存uid到connection的映射
$ws_worker->uidConnetions = [];
// 客戶端發(fā)送消息時(shí)回調(diào)
$ws_worker->onMessage = function ($connection, $data) use ($ws_worker)
{/*{{{*/
// 判斷當(dāng)前客戶端是否已經(jīng)驗(yàn)證過(guò)
if (!isset($connection->uid)) {
$connection->uid = $data;
$ws_worker->uidConnetions[$connection->uid] = $connection;
return;
}
};/*}}}*/
// 當(dāng)客戶端連接斷開時(shí)
$ws_worker->onClose = function ($connection) use ($ws_worker)
{/*{{{*/
if (isset($connection->uid)) {
unset($ws_worker->uidConnetions[$connection->uid]);
}
};/*}}}*/
/**
* sendMessageById 向指定的客戶端推送數(shù)據(jù)
*
* @param mixed $id
* @param mixed $message
* @access public
* @return bool
*/
function sendMessageById($id, $message)
{/*{{{*/
if (is_array($message))
$message = json_encode($message);
global $ws_worker;
if (isset($ws_worker->uidConnetions[$id])) {
$connection = $ws_worker->uidConnetions[$id];
$connection->send($message);
return true;
}
return false;
}/*}}}*/
/**
* broadcast 向所有用戶推送數(shù)據(jù)
*
* @param mixed $message
* @access public
* @return void
*/
function broadcast($message)
{/*{{{*/
global $ws_worker;
foreach ($ws_worker->uidConnetions as $connection) {
$connection->send($message);
}
}/*}}}*/
Worker::runAll();
2.開啟服務(wù)進(jìn)程:php start.php start
3. 發(fā)送http請(qǐng)求到對(duì)應(yīng)的端口,代碼如下:
<?php
function post($url, $data = [], $timeout = 3)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //設(shè)置超時(shí)
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
// 發(fā)送http請(qǐng)求到http服務(wù)
$data['id'] = 100;
$data['status'] = 6;
// 你的IP地址+端口號(hào)
$url = 'http://192.168.16.101:10087';
$result = post($url, $data);
echo $result . "\n";
4.客戶端連接代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket_client</title>
<script>
//創(chuàng)建一個(gè)socket實(shí)例
ws = new WebSocket("ws://192.168.16.101:10086");
ws.onopen = function () {
console.log("連接成功");
ws.send(100);
//console.log("發(fā)送數(shù)據(jù):tom")
};
ws.onmessage = function (e) {
console.log("收到服務(wù)端信息:" + e.data);
};
</script>
</head>
<body>
</body>
</html>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者