###前提・実現したいこと
PHPでのユーザー登録制のウェブサイトにチャット機能を搭載したいと
考えており、Ratchetでのwebsocket通信を利用して、機能を実装したいと
考えています。初学者でしてわかっていない点が多々ありますが、回答よろしくお願いします。
###発生している問題・エラーメッセージ
公式サイト(http://socketo.me/docs/hello-world)では下記のサンプルがあり、利用したいと考えています。下記は公式サイトから抜粋
サーバー側
chat-server.php
<?php use Ratchet\Server\IoServer; use MyApp\Chat; require dirname(__DIR__) . '/vendor/autoload.php'; $server = IoServer::factory( new Chat(), 8080 ); $server->run(); chat.php <?php namespace MyApp; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface; 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 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(); } } クライアント側(一部) <script> var conn = new WebSocket('ws://localhost:8080'); conn.onopen = function(e) { console.log("Connection established!"); }; conn.onmessage = function(e) { console.log(e.data); }; </script> ここでconn.send('Hello World!');を実装して実行すると接続ユーザー全員にHello World!が送れるという仕様です。 実際にチャットを行う際にはtopicを使用して、送信範囲を限定したいと考えています。またユーザー名とユーザー関連情報をチャット時にメッセージと一緒に送信・表示できたらと考えています。 確認したい事項は下記の通りです。 ・上記プログラムを組み込んだ場合、クライアント側でユーザー登録無し、又はサイト内のチャットルーム外からchatサーバーにアクセスして、チャットのメッセージを送れてしまうかもしれないという点がありまして、その対策やその他コミュニティサイトでのチャット機能の実装に際してアドバイス頂ければと思います。 (追記)サーバーのログ出力で何かスタンダードな方法はないでしょうか?Apacheのようなログ出力できるものがあればいいのですが 以上よろしくお願いします。あなたの回答
tips
プレビュー