我只是在学习 Ratchet(用于 PHP 实时聊天应用程序的库)。根据文档,以下函数将收到两件事:
然而,它错过了发送给谁。此功能将消息发送给所有连接的人。但我也想实现一对一聊天。有没有办法做到这一点?
这是该库的链接:http://socketo.me/docs/hello-world .
public function onMessage(ConnectionInterface $from, $msg) {
//send the message to all the other clients except the one who sent.
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
最佳答案
我建议您根据用户想要的每次聊天进行不同的订阅。
每个人之间的聊天消息的一个 channel :subscriptionname = room123
对于每个个人聊天,您可以使用不同的签名:subscriptionname = personal456。
然后让您的客户端应用程序监听 2 个不同的订阅,并尝试根据收到消息的订阅的名称对其进行路由。
关于php - Ratchet 中的实时一对一消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36475593/