1.register和gateWay在一台, business在一台.gate使用ws协议.
2.business的Event类实现onWebSocketConnect, 在该方法中调用Gateway::sendToClient($client_id, "Hello $client_id\r\n");
客户端无接收onMessage
business
public static function onWebSocketConnect($client_id, $data)
{
var_export('onWebSocketConnect: ' . $client_id);
var_export('isOnline: ' . Gateway::isOnline($client_id));
var_export('getAllClientIdList: ');
var_export(Gateway::getAllClientIdList());
// 向当前client_id发送数据
Gateway::sendToClient($client_id, "Hello $client_id\r\n");
}
js
websocket = new WebSocket("ws://127.0.0.1:18282");
//连接发生错误的回调方法
websocket.onerror = function(e) {
console.log("llws连接错误!");
setMessageInnerHTML("llws连接关闭!onerror"+new Date().toLocaleString());
};
//连接成功建立的回调方法
websocket.onopen = function(event) {
setInterval(function(){
send({type:'ping'});
}, 10000)
setMessageInnerHTML("llws连接成功!"+new Date().toLocaleString());
}
//接收到消息的回调方法
websocket.onmessage = function(event) {
console.log(event);
console.log("llws收到消息啦:" +event.data);
setMessageInnerHTML(event.data);
}
//连接关闭的回调方法
websocket.onclose = function(e) {
console.log("llws连接关闭!onclose"+new Date().toLocaleString());
setMessageInnerHTML("llws连接关闭!onclose"+new Date().toLocaleString());
}
business
的打印信息
register
的信息
docker
https://github.com/walkor/GatewayWorker/pull/101
https://github.com/walkor/GatewayWorker/pull/101/files