json
1コード 2{ 3 "autoload": { 4 "psr-4": { 5 "MyApp\": "src" 6 } 7 }, 8 "require": { 9 "cboden/ratchet": "^0.4" 10 } 11}
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
php
1コード 2<?php 3use Ratchet\Server\IoServer; 4use MyApp\Chat; 5use Ratchet\Http\HttpServer; 6use Ratchet\WebSocket\WsServer; 7 8require dirname ( __DIR__ ) . '/vendor/autoload.php'; 9 10$server = IoServer::factory ( new HttpServer( new WsServer( new Chat () ) ), 8080 ); 11$server->run ();
サーバーを立ち上げようとして、cmdで
php chat-server.phpを入力すると
Fatal error: Uncaught Error: Class 'Ratchet\Server\IoServer' not found in C:\tutorial\websocket\bin\chat-server.php:9
Stack trace:
#0 {main}
thrown in C:\tutorial\websocket\bin\chat-server.php on line 9
とエラーが出ます
ディレクトリの階層は
├── bin
│ └── chat-server.php
├── src
│ └── MyApp
│ └── Chat.php
└── vendor
├── autoload.php
├── composer
│ ├── autoload_classmap.php
│ ├── autoload_namespaces.php
│ ├── autoload_psr4.php
│ ├── autoload_real.php
│ ├── ClassLoader.php
│ └── installed.json
です。
改善点を教えてください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/08 14:00