मेरे पास एक ही प्रश्न था और मैंने यही किया है।
hello world tutorial के आधार पर, मैंने एक सरणी के साथ SplObjectStorage को प्रतिस्थापित किया है। मेरे संशोधनों को पेश करने से पहले, मैं टिप्पणी करना चाहता हूं कि यदि आपने उस ट्यूटोरियल का पालन किया और इसे समझा, तो केवल एक ही चीज जिसने आपको इस समाधान पर पहुंचने से रोका था शायद यह नहीं जानना कि SplObjectStorage क्या है।
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();
}
}
बेशक
यह वास्तव में उपयोगी है यदि आप भी एक डीबी सिलसिले में जोड़ सकते हैं बनाने के लिए, और स्टोर/उन resourceIds पुनः प्राप्त।
स्रोत
2013-07-19 02:43:27
सहायता के लिए धन्यवाद। – lexa
प्रतिक्रिया/ZMQ के लिए अच्छा उदाहरण।लेकिन मैं यह नहीं समझ सकता कि मैं उचित उपयोगकर्ता को उचित डेटा कैसे धक्का दे सकता हूं (उदाहरण के लिए, मैंने बनाया एक विशेष जेसन)। उदाहरण के लिए, मुझे उचित उपयोगकर्ताओं को डेटा भेजना है (मेरे पास उपयोगकर्ता की आईडी की सूचियां हैं)। क्या यह आवश्यक है कि उपयोगकर्ता पहले से ही 'विशिष्ट पृष्ठ' की सदस्यता ले चुका है (जैसे वे यहां कहते हैं: http://socketo.me/docs/push)? – lexa
igorw, क्लाउड हम आपको सीधे एक प्रश्न पूछने के लिए किसी भी तरह से जुड़े हुए हैं (स्काइप, exmaple के लिए)। और आपकी मदद के लिए धन्यवाद। – lexa