環境
php5.6
ratchet:v0.3.6
PHP版のRatchetを用いて、チャットシステムの勉強をしています。
下記のチュートリアルの内容を組み合わせて、チャットする各ユーザーを
RoomIDのようなものでチャット部屋を分けて、その中でのみメッセージを
配信できるようにしたいと考えています。
(zeroMQを利用してtopicにRoomIDを割り当てるという方法を試しています。)
http://socketo.me/docs/hello-world
http://socketo.me/docs/push
そこでchat-serverは下記の通りのコードにしています。
chat-server.php
<?php use Ratchet\Server\IoServer; use Ratchet\Http\HttpServer; use Ratchet\WebSocket\WsServer; use MyApp\Chat; require dirname(__DIR__) . '/vendor/autoload.php'; $server = IoServer::factory( new HttpServer( new WsServer( new Chat() ) ), 8080 ); $server->run();
chat.php
<?php namespace MyApp; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; use Ratchet\ComponentInterface; class Chat implements MessageComponentInterface { protected $clients; public function __construct() { $this->clients = new \SplObjectStorage; } public function onOpen(ConnectionInterface $conn) { // Store the new connection to send messages to later $this->clients->attach($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 $client) { if ($from !== $client) { // The sender is not the receiver, send to each client connected $client->send($msg); } } } //ここはチュートリアルから追加しています。 public function onSubscribe(ConnectionInterface $conn, $topic) { if (!array_key_exists($topic->getId(), $this->subscribedTopics)) { $this->subscribedTopics[$topic->getId()] = $topic; } } //ここはチュートリアルから追加しています。 public function onPublish(ConnectionInterface $conn, $topic, $event, array $exclude, array $eligible) { $json = json_decode($event); $topic->broadcast(Security::htmlentities($json->input)); } public function onClose(ConnectionInterface $conn) { // The connection is closed, remove it, as we can no longer send it messages $this->clients->detach($conn); echo "Connection {$conn->resourceId} has disconnected\n"; } public function onError(ConnectionInterface $conn, \Exception $e) { echo "An error has occurred: {$e->getMessage()}\n"; $conn->close(); } }
クライアントはまず、webのページに入った直後にchat-serverに接続してtopicにRoomIDを割り当てたいと考えています。
chat.html
<script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script> <script> var conn = new ab.Session('ws://localhost:8080', function() { conn.subscribe('RoomID', function(topic, data) { // This is where you would add the new article to the DOM (beyond the scope of this tutorial) console.log('New article published to category "' + topic + '" : ' + data.title); }); }, function() { console.warn('WebSocket connection closed'); }, {'skipSubprotocolCheck': true} ); </script>
しかし、下記のようなエラーがクライアント側で生じて、一旦接続しますが、すぐに切られてしまいます。
WebSocket connection to 'ws://localhost:8080/' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received
autobahn.min.js:62
対処方法がわからず、アドバイス頂きたくよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/20 12:23
2017/03/20 12:33
退会済みユーザー
2017/03/20 12:52 編集
2017/03/20 13:15
2017/03/21 12:29