Tôi có cùng một câu hỏi và đây là những gì tôi đã làm.
Dựa trên hello world tutorial, tôi đã thay thế SplObjectStorage bằng một mảng. Trước khi trình bày các sửa đổi của tôi, tôi muốn bình luận rằng nếu bạn làm theo hướng dẫn đó và hiểu nó, điều duy nhất ngăn bạn tự mình đến giải pháp này có lẽ không biết SplObjectStorage là gì.
class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = array();
}
public function onOpen(ConnectionInterface $conn) {
// Store the new connection to send messages to later
$this->clients[$conn->resourceId] = $conn;
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
$numRecv = count($this->clients) - 1;
echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->clients as $key => $client) {
if ($from !== $client) {
// The sender is not the receiver, send to each client connected
$client->send($msg);
}
}
// Send a message to a known resourceId (in this example the sender)
$client = $this->clients[$from->resourceId];
$client->send("Message successfully sent to $numRecv users.");
}
public function onClose(ConnectionInterface $conn) {
// The connection is closed, remove it, as we can no longer send it messages
unset($this->clients[$conn->resourceId]);
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Tất nhiên để làm cho nó thực sự hữu ích bạn cũng có thể muốn thêm vào kết nối DB và lưu trữ/truy xuất các tài nguyên đó.
Nguồn
2013-07-19 02:43:27
Cảm ơn bạn đã trợ giúp. – lexa
Ví dụ tốt cho React/ZMQ.Nhưng tôi không thể tìm hiểu làm thế nào tôi có thể đẩy dữ liệu thích hợp (ví dụ, một json đặc biệt tôi đã tạo) cho người dùng thích hợp. Ví dụ, tôi phải gửi dữ liệu cho người dùng thích hợp (tôi có danh sách id người dùng). Điều này có cần thiết mà người dùng đã được đăng ký với 'trang cụ thể' (như họ nói ở đây: http://socketo.me/docs/push)? – lexa
igorw, đám mây chúng ta được kết nối bằng cách nào đó (skype, cho exmaple) để đặt câu hỏi trực tiếp cho bạn. Và cảm ơn sự giúp đỡ của bạn. – lexa