ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
实际项目里,我们可以将用户uid和fd进行双向绑定(暂不考虑多台服务器分布式部署情况),例如使用Redis保存:在onMessage进行用户信息验证后: ``` $this->redis->set($fd, $uid); $this->redis->set($uid, $fd); ``` 后续需要指定给某人发消息,只需要根据uid/fd发送即可。在onClose事件里进行解绑操作。群发的话只需要遍历一遍[`$server->connections`](https://wiki.swoole.com/wiki/page/427.html)即可。 示例(该项目只实现群发): moell-peng/webim: PHP + Swoole 实现的简单聊天室 https://github.com/moell-peng/webim