我正在使用Rachet並嘗試給出的代碼爲here。在我的Chrome控制檯窗口中運行以下代碼後,我看不到任何消息。 conn.send()
說未定義:WebSocket:無法在客戶端接收消息
var conn = new WebSocket('ws://localhost:8080');
conn.onopen = function(e) {
console.log("Connection established!");
};
conn.onmessage = function(e) {
console.log(e.data);
};
更新
代碼在服務器端:在行動
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 $client) {
if ($from !== $client) {
print "Someone else is here";
// The sender is not the receiver, send to each client connected
$msg = ' Server responds:- '.$msg;
$client->send($msg);
}
}
}
更新#2
似乎以下檢查問題:
if ($from !== $client) {}
但在去除,這是廣播到所有的客戶端連接,而到誰發送的消息
你能分享你的php代碼嗎?特別是您存儲/保存連接的客戶端的位置? – brense
@brense它在這裏'聊天類'http://socketo.me/docs/hello-world – Volatil3
好吧,在聊天服務器窗口中,你看到你的客戶端連接? '新的連接! xxx' – brense