| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- <?php
- namespace App\Services\Socket;
- use Ratchet\MessageComponentInterface;
- use Ratchet\ConnectionInterface;
- class ChatClient implements MessageComponentInterface
- {
- protected $clients;
- public function __construct()
- {
- $this->clients = new \SplObjectStorage;
- }
- public function onOpen(ConnectionInterface $conn)
- {
- $this->clients->attach($conn);
- echo "SocketClient:创建连接【{$conn->resourceId}】成功! \n";
- }
- public function onMessage(ConnectionInterface $from, $msg)
- {
- echo "SocketClient:收到消息 ".$msg.'++'.$from->resourceId."\n";
- $from->send($msg);
- }
- public function onClose(ConnectionInterface $conn)
- {
- $this->clients->detach($conn);
- echo "SocketClient:关闭连接【{$conn->resourceId}】成功\n";
- }
- public function onError(ConnectionInterface $conn, \Exception $e)
- {
- echo "SocketClient:连接错误 {$e->getMessage()}\n";
- $conn->close();
- }
- }
|