json
1コード 2{ 3 "autoload": { 4 "psr-0": { 5 "MyApp": "src" 6 } 7 }, 8 "require": { 9 "cboden/ratchet": "*" 10 } 11}
php
1 2コード 3 4<?php 5use Ratchet\Server\IoServer; 6use MyApp\Chat; 7 8 require dirname(__DIR__) . '/vendor/autoload.php'; 9 10 $server = IoServer::factory( 11 new Chat(), 12 8080 13 ); 14 15 $server->run();
php
1コード 2<?php 3namespace MyApp; 4use Ratchet\MessageComponentInterface; 5use Ratchet\ConnectionInterface; 6 7class Chat implements MessageComponentInterface { 8 protected $clients; 9 10 public function __construct() { 11 $this->clients = new \SplObjectStorage; 12 } 13 14 public function onOpen(ConnectionInterface $conn) { 15 // Store the new connection to send messages to later 16 $this->clients->attach($conn); 17 18 echo "New connection! ({$conn->resourceId})\n"; 19 } 20 21 public function onMessage(ConnectionInterface $from, $msg) { 22 $numRecv = count($this->clients) - 1; 23 echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n" 24 , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's'); 25 26 foreach ($this->clients as $client) { 27 if ($from !== $client) { 28 // The sender is not the receiver, send to each client connected 29 $client->send($msg); 30 } 31 } 32 } 33 34 public function onClose(ConnectionInterface $conn) { 35 // The connection is closed, remove it, as we can no longer send it messages 36 $this->clients->detach($conn); 37 38 echo "Connection {$conn->resourceId} has disconnected\n"; 39 } 40 41 public function onError(ConnectionInterface $conn, \Exception $e) { 42 echo "An error has occurred: {$e->getMessage()}\n"; 43 44 $conn->close(); 45 } 46} 47
上記のコードでwebsocketのさーばーを構築しようとしたのですが、エラーが出ます。
chat-server.phpでは
Warning: require(C:\xampp\htdocs\test/vendor/autoload.php): failed to open stream: No such file or directory in C:\xampp\htdocs\test\bin\chat-server.php on line 5
Fatal error: require(): Failed opening required 'C:\xampp\htdocs\test/vendor/autoload.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\test\bin\chat-server.php on line 5
Chat.phpでは
Fatal error: Interface 'Ratchet\MessageComponentInterface' not found in C:\xampp\htdocs\test\src\MyApp\Chat.php on line 6
のように表示されます。
どのように改善すればよいですか?
あなたの回答
tips
プレビュー