<?php
namespace app\index\controller;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Register;
use GatewayWorker\Gateway;
use think\worker\Server;
class Worker extends Server
{
public function __construct()
{
//初始化register进程
new Register('text://0.0.0.0:1238');
//初始化businessWorker进程
$worker = new BusinessWorker();
$worker->name = 'tuoyuyun';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1238';
$worker->eventHandler = 'app\index\controller\Events';//通信接收和发送类
//初始化gateway进程
$gateway = new Gateway('Websocket://0.0.0.0:8282');//硬件通信ip和开放端口
$gateway->name = 'gateway';
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 2900;
$gateway->registerAddress = '127.0.0.1:1238';
}
}
<?php
namespace app\index\controller;
use Exception;
use GatewayWorker\Lib\Gateway;
class Events
{
/**
* 当客户端连接时触发
* 如果业务不需此回调可以删除onConnect
*
* @param int $client_id 连接id
* @throws Exception
*/
public static function onConnect($client_id)
{
Gateway::sendToClient($client_id,json_encode([
'type' => 'init',
'client_id' => $client_id
]));
}
/**
* 当客户端发来消息时触发
* @param int $client_id 连接id
* @param mixed $message 具体消息
* @throws Exception
*/
public static function onMessage($client_id, $message)
{
$list = json_decode($message,true);
if (!$list) return;
switch ($list['type']) {
case 'bind':
$fromId = $list['fromId'];
Gateway::bindUid($client_id, $fromId);
break;
case 'say':
$content = $list['data'];
$fromId = $list['fromId'];
$toId = $list['toId'];
$data = [
'type' => 'text',
'id' => $client_id,
'data' => $content,
'fromId' => $fromId,
'toId' => $toId
];
Gateway::sendToUid($toId,json_encode($data));
break;
case 'all':
GateWay::sendToAll($list['data']);
break;
}
}
/**
* 当用户断开连接时触发
* @param int $client_id 连接id
* @throws Exception
*/
public static function onClose($client_id)
{
// 向所有人发送
GateWay::sendToAll("$client_id logout\r\n");
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>
<input id="fromId" type="hidden" value="{$fromID}">
<input id="toId" type="hidden" value="{$toID}">
<input type="text" id="text">
<button id="send">发送</button>
<div id="div"></div>
</body>
</html>
<script>
var fromId = $('#fromId').val();
var toId = $('#toId').val();
var ws = new WebSocket('ws://47.100.191.127:8282');
ws.onmessage = function (e) {
var message = eval("("+ e.data +")");
switch (message.type) {
case "init":
var bindObj = {
type:"bind",
fromId:fromId
}
var bindMsg = JSON.stringify(bindObj);
ws.send(bindMsg);
break;
case 'text':
var html = '<div style="width: 100px;height: 50px;background-color: pink;">'+ message.data +'</div>';
$("#div").append(html);
break;
}
}
$('#send').click(function () {
var value = $('#text').val();
var obj = {
type:"say",
data:value,
fromId:fromId,
toId:toId
}
var data = JSON.stringify(obj);
ws.send(data);
var html = '<div style="width: 100px;height: 50px;background-color: pink;">'+ value +'</div>';
$("#div").append(html);
$('#text').val('');
});
</script>