PHP WebSocket ZMQ - 聊天操作 - 向特定用户发送数据 [英] PHP WebSocket ZMQ - Chat Operation - Send data to specific user
问题描述
我正在开发一个基于 Symfony 2.2.11 的 PHP 项目,我安装了与以下教程相关的 socketo http://socketo.me/docs/install 使我的聊天脚本正常工作.
im working on a PHP project based on Symfony 2.2.11 and I installed the socketo related to the following tutorial http://socketo.me/docs/install to make my chat script working.
ServerCommand.php//启动 WebSocket 服务器的命令行代码
ServerCommand.php // Code of the command line that starts the WebSocket server
$oLoop = Factory::create();
// Listen for the web server to make a ZeroMQ push after an ajax request
$oContext = new Context($oLoop);
$oPull = $oContext->getSocket(\ZMQ::SOCKET_PULL);
// LET IT 127.0.0.1
$oPull->bind('tcp://127.0.0.1:5555'); // Binding to 127.0.0.1 means the only client that can connect is itself
$oPull->on('message', array($oChat, 'onMessage'));
// Set up our WebSocket server for clients wanting real-time updates
$oWebSock = new Server($oLoop);
$oWebSock->listen(7979, '0.0.0.0'); // Binding to 0.0.0.0 means remotes can connect
$webServer = new IoServer(
new HttpServer(
new WsServer(
new WampServer(
$oChat
)
)
),
$oWebSock
);
$oLoop->run();
将消息添加到数据库后:MessagesController.php
After a message is being added to database : MessagesController.php
....
// This is our new stuff
$oContext = new \ZMQContext();
$oSocket = $oContext->getSocket(\ZMQ::SOCKET_PUSH, 'PushMe');
$oSocket->connect("tcp://mydomain:5555");
$aData = array(
'topic' => 'message',
'sUsername' => $oUserCurrent->getUsername(),
'sMessage' => $sMessage
);
$oSocket->send(json_encode($aData));
.....
聊天服务:Chat.php
/**
* A lookup of all the topics clients have subscribed to
*/
public function onSubscribe(ConnectionInterface $conn, $topic)
{
// When a visitor subscribes to a topic link the Topic object in a lookup array
$subject = $topic->getId();
$ip = $conn->remoteAddress;
if (!array_key_exists($subject, $this->subscribedTopics))
{
$this->subscribedTopics[$subject] = $topic;
}
$this->clients[] = $conn->resourceId;
echo sprintf("New Connection: %s" . PHP_EOL, $conn->remoteAddress);
}
/**
* @param string JSON'ified string we'll receive from ZeroMQ
*/
public function onMessage($jData)
{
$aData = json_decode($jData, true);
var_dump($aData);
if (!array_key_exists($aData['topic'], $this->subscribedTopics)) {
return;
}
$topic = $this->subscribedTopics[$aData['topic']];
// This sends out everything to multiple users, not what I want!!
// re-send the data to all the clients subscribed to that category
$topic->broadcast($aData);
}
接收数据的JS代码:messages.html.twig:
JS code that receives data : messages.html.twig :
var conn = new ab.Session(
'ws://mydomain:7979' // The host (our Ratchet WebSocket server) to connect to
, function() { // Once the connection has been established
conn.subscribe('message', function(topic, data)
{
console.log(topic);
console.log(data);
});
}
, function() { // When the connection is closed
console.warn('WebSocket connection closed');
}
, { // Additional parameters, we're ignoring the WAMP sub-protocol for older browsers
'skipSubprotocolCheck': true
}
);
所以一切都完美无缺,当我发送一条新消息时,它会转到数据库,然后它会出现在聊天页面上.
So everytings working perfectly, when I send a new Message, it goes to DB then it lands on the page of the chat.
问题:JS脚本到哪里,数据就到哪里,结果是所有用户都能得到相同的记录消息
PROBLEM : The data lands wherever the JS script is, and the result is that all users can get the same recorded message
询问:如何让数据进入特定用户页面?
谢谢
推荐答案
你在后端使用 Ratchet
吧?
所以,这里有您需要的非常好的案例示例:
So, here you have very good example of case you need:
http://socketo.me/docs/hello-world
您应该将您的客户端连接保存在 $clients
属性中(而不是资源 id 的集合!).因此,您可以从该集合中选择一个元素并仅向该客户端发送消息.
You should keep your client connections inside $clients
property (not collection of resources id!). So, you can choose one element from this collection and send a message only to this client.
示例:
public function onSubscribe(ConnectionInterface $conn, $topic)
{
// When a visitor subscribes to a topic link the Topic object in a lookup array
$subject = $topic->getId();
$ip = $conn->remoteAddress;
if (!array_key_exists($subject, $this->subscribedTopics))
{
$this->subscribedTopics[$subject] = $topic;
}
$this->clients[] = $conn; // you add connection to the collection
$conn->send("Hello new user!"); // you send a message only to this one user
}
这篇关于PHP WebSocket ZMQ - 聊天操作 - 向特定用户发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!