實際項目里,我們可以將用戶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