swoole.php配置文件:
websocketEvent文件:
websocketHandle文件:
Websocket类对象方法:
broadcast 设置进行广播消息发送
isBroadcast 判断当前是否是广播模式
to 设置收件人fd或聊天室名(可以数组设置多个)
getTo 获取收件人fd或聊天室名
join 当前客户端加入到指定聊天室(可以多个)
leave 当前客户端离开指定聊天室(可以多个)
emit 消息发送
close 关闭当前连接
getSender 获取当前客户端id(即fd)
setSender 设置发件人的fd
\think\swoole\Manager类对象方法:
getServer 获取当前Swoole的服务对象,利用该对象直接操作原生Swoole服务方法
stop 停止服务
客户端:
websocket发送
$(function () {
let ws = new WebSocket("ws://ip:端口");
ws.onopen = function() {
console.log("client:打开连接");
// ws.send("client:hello,服务端");
};
ws.onmessage = function(e) {
console.log(e)
};
ws.onclose = function(params) {
console.log("client:关闭连接");
};
$('.send').click(function(){
let val = $('.message').val();
//['事件名', 真正要发送的数据]
let event = ['test',{
content:val
}];
ws.send(JSON.stringify(event));
let str = '
'+val+''$('.message_list').append(str);
});
})
swoole的回调事件:
需要在app/event.php中注册:
这里我注册了request事件和task、finish事件
HttpRequest.php文件:
Task.php文件: