- 下载workerman源码包
- 在start.php中引入源码,将源码包vendor目录放在与start.php同级目录
1. start.php 代码如下:
<?php
require_once 'vendor/autoload.php';
use Workerman\Worker;
/**
* 多端口,多协议
*/
// 使用websocket协议
$ws_worker = new Worker("websocket://0.0.0.0:10086");
// 启动进程对外服务。如果进程启动后建立一个内部通讯端口,则进程数必须为1
$ws_worker->count = 1;
// 连接事件,阻断非法连接
$ws_worker->onConnect = function ($connection) use ($domains)
{/*{{{*/
// $connection->onWebSocketConnect = function ($connection, $http_header) use ($domains)
// {
// // 判断连接来源是否合法,不合法关闭连接
// if (!in_array(parse_url($_SERVER['HTTP_ORIGIN'])['host'], $domains)) {
// $connection->close();
// }
// $connection->send("connection success.");
// };
};/*}}}*/
// 进程启动后建立一个内部通讯端口
$ws_worker->onWorkerStart = function ($ws_worker)
{/*{{{*/
/**
* 多个进程监听同一个端口(监听套接字不是继承自父进程)
* 需要开启端口复用,不然会报Address already in use错误
*/
// 开启一个内部端口,方便内部系统推送数据,text协议格式,文本+换行符
/* $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'];
// 通过workerman向id的页面推送数据
$ret = sendMessageById($id, $buffer);
// 返回推送结果
$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'];
// 通过workerman向id的页面推送数据
$ret = sendMessageById($id, ['id', 'status' => $status]);
// 返回推送结果
$http->send($ret ? 'ok' : 'fail');
};
$inner_text_worker->listen();
};/*}}}*/
// 增加一个属性,用来保存uid到connection的映射
$ws_worker->uidConnetions = [];
// 客户端发送消息时回调
$ws_worker->onMessage = function ($connection, $data) use ($ws_worker)
{/*{{{*/
// 判断当前客户端是否已经验证过
if (!isset($connection->uid)) {
$connection->uid = $data;
$ws_worker->uidConnetions[$connection->uid] = $connection;
return;
}
};/*}}}*/
// 当客户端连接断开时
$ws_worker->onClose = function ($connection) use ($ws_worker)
{/*{{{*/
if (isset($connection->uid)) {
unset($ws_worker->uidConnetions[$connection->uid]);
}
};/*}}}*/
/**
* sendMessageById 向指定的客户端推送数据
*
* @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 向所有用户推送数据
*
* @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.开启服务进程:php start.php start
3. 发送http请求到对应的端口,代码如下:
<?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); //设置超时
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;
}
// 发送http请求到http服务
$data['id'] = 100;
$data['status'] = 6;
// 你的IP地址+端口号
$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>
//创建一个socket实例
ws = new WebSocket("ws://192.168.16.101:10086");
ws.onopen = function () {
console.log("连接成功");
ws.send(100);
//console.log("发送数据:tom")
};
ws.onmessage = function (e) {
console.log("收到服务端信息:" + e.data);
};
</script>
</head>
<body>
</body>
</html>
网友评论