|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This package was originally maintained by Sergey Poltaranin |
| 4 | + * Due to to the fact that the repo is abandoned i recreated it. |
| 5 | + * You can find the old repo here: https://github.com/consik/yii2-websocket |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace hunwalk\websocket; |
| 11 | + |
| 12 | +use Exception; |
| 13 | +use hunwalk\websocket\events\ExceptionEvent; |
| 14 | +use hunwalk\websocket\traits\WebsocketEventTrait; |
| 15 | +use Ratchet\ConnectionInterface; |
| 16 | +use Ratchet\Http\HttpServer; |
| 17 | +use Ratchet\MessageComponentInterface; |
| 18 | +use Ratchet\Server\IoServer; |
| 19 | +use Ratchet\WebSocket\WsServer; |
| 20 | +use SplObjectStorage; |
| 21 | +use yii\base\Component; |
| 22 | +use yii\base\InvalidConfigException; |
| 23 | + |
| 24 | +class WebSocketServer extends Component implements MessageComponentInterface |
| 25 | +{ |
| 26 | + use WebsocketEventTrait; |
| 27 | + |
| 28 | + /** |
| 29 | + * @event yii\base\Event Triggered when binding is successfully completed |
| 30 | + */ |
| 31 | + const EVENT_WEBSOCKET_OPEN = 'ws_open'; |
| 32 | + |
| 33 | + /** |
| 34 | + * @event yii\base\Event Triggered when socket listening is closed |
| 35 | + */ |
| 36 | + const EVENT_WEBSOCKET_CLOSE = 'ws_close'; |
| 37 | + |
| 38 | + /** |
| 39 | + * @event ExceptionEvent Triggered when throwed Exception on binding socket |
| 40 | + */ |
| 41 | + const EVENT_WEBSOCKET_OPEN_ERROR = 'ws_open_error'; |
| 42 | + |
| 43 | + /** |
| 44 | + * @event ClientEvent Triggered when client connected to the server |
| 45 | + */ |
| 46 | + const EVENT_CLIENT_CONNECTED = 'ws_client_connected'; |
| 47 | + |
| 48 | + /** |
| 49 | + * @event WSClientErrorEvent Triggered when an error occurs on a Connection |
| 50 | + */ |
| 51 | + const EVENT_CLIENT_ERROR = 'ws_client_error'; |
| 52 | + |
| 53 | + /** |
| 54 | + * @event ClientEvent Triggered when client close connection with server |
| 55 | + */ |
| 56 | + const EVENT_CLIENT_DISCONNECTED = 'ws_client_disconnected'; |
| 57 | + |
| 58 | + /** |
| 59 | + * @event WSClientMessageEvent Triggered when message recieved from client |
| 60 | + */ |
| 61 | + const EVENT_CLIENT_MESSAGE = 'ws_client_message'; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var int $port |
| 65 | + */ |
| 66 | + public $port = 8080; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var bool $closeConnectionOnError |
| 70 | + */ |
| 71 | + protected $closeConnectionOnError = true; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var IoServer|null $server |
| 75 | + */ |
| 76 | + protected $server = null; |
| 77 | + |
| 78 | + /** |
| 79 | + * @var null|SplObjectStorage $clients |
| 80 | + */ |
| 81 | + protected $clients = null; |
| 82 | + |
| 83 | + /** |
| 84 | + * @return bool |
| 85 | + * @event yii\base\Event EVENT_WEBSOCKET_OPEN |
| 86 | + * @event ExceptionEvent EVENT_WEBSOCKET_OPEN_ERROR |
| 87 | + * @throws InvalidConfigException |
| 88 | + */ |
| 89 | + public function start(): bool |
| 90 | + { |
| 91 | + try { |
| 92 | + $this->server = IoServer::factory( |
| 93 | + new HttpServer( |
| 94 | + new WsServer( |
| 95 | + $this |
| 96 | + ) |
| 97 | + ), |
| 98 | + $this->port |
| 99 | + ); |
| 100 | + $this->trigger(self::EVENT_WEBSOCKET_OPEN); |
| 101 | + $this->clients = new SplObjectStorage(); |
| 102 | + $this->server->run(); |
| 103 | + |
| 104 | + return true; |
| 105 | + |
| 106 | + } catch (Exception $e) { |
| 107 | + $event = $this->getExceptionEvent($e); |
| 108 | + $this->trigger(self::EVENT_WEBSOCKET_OPEN_ERROR, $event); |
| 109 | + return false; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return void |
| 115 | + * @event yii\base\Event EVENT_WEBSOCKET_CLOSE |
| 116 | + */ |
| 117 | + public function stop(): void |
| 118 | + { |
| 119 | + $this->server->socket->close(); |
| 120 | + $this->trigger(self::EVENT_WEBSOCKET_CLOSE); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param ConnectionInterface $conn |
| 125 | + * @throws InvalidConfigException |
| 126 | + * @event ClientEvent EVENT_CLIENT_CONNECTED |
| 127 | + */ |
| 128 | + function onOpen(ConnectionInterface $conn): void |
| 129 | + { |
| 130 | + $event = $this->getClientEvent($conn); |
| 131 | + $this->trigger(self::EVENT_CLIENT_CONNECTED, $event); |
| 132 | + |
| 133 | + $this->clients->attach($conn); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @param ConnectionInterface $conn |
| 138 | + * @throws InvalidConfigException |
| 139 | + * @event ClientEvent EVENT_CLIENT_DISCONNECTED |
| 140 | + */ |
| 141 | + function onClose(ConnectionInterface $conn): void |
| 142 | + { |
| 143 | + $event = $this->getClientEvent($conn); |
| 144 | + $this->trigger(self::EVENT_CLIENT_DISCONNECTED, $event); |
| 145 | + |
| 146 | + $this->clients->detach($conn); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @param ConnectionInterface $conn |
| 151 | + * @param Exception $e |
| 152 | + * @throws InvalidConfigException |
| 153 | + * @event ClientErrorEvent EVENT_CLIENT_ERROR |
| 154 | + */ |
| 155 | + function onError(ConnectionInterface $conn, Exception $e): void |
| 156 | + { |
| 157 | + $event = $this->getClientErrorEvent($conn, $e); |
| 158 | + $this->trigger(self::EVENT_CLIENT_ERROR, $event); |
| 159 | + |
| 160 | + if ($this->closeConnectionOnError) { |
| 161 | + $conn->close(); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * @param ConnectionInterface $from |
| 167 | + * @param string $msg |
| 168 | + * @throws InvalidConfigException |
| 169 | + * @event ClientMessageEvent EVENT_CLIENT_MESSAGE |
| 170 | + */ |
| 171 | + function onMessage(ConnectionInterface $from, $msg): void |
| 172 | + { |
| 173 | + $event = $this->getClientMessageEvent($from, $msg); |
| 174 | + $this->trigger(self::EVENT_CLIENT_MESSAGE, $event); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments