1 | <?php |
||
16 | class WebSocketServer extends StreamServer implements Countable, ReactiveInterface { |
||
17 | |||
18 | /** |
||
19 | * Holds all connected clients. |
||
20 | * |
||
21 | * @var WebSocketClient[] |
||
22 | */ |
||
23 | protected $clients = []; |
||
24 | |||
25 | /** |
||
26 | * @var Reactor |
||
27 | */ |
||
28 | protected $reactor; |
||
29 | |||
30 | /** |
||
31 | * @param $resource |
||
32 | * @param Reactor $reactor |
||
33 | */ |
||
34 | public function __construct ($resource, Reactor $reactor) { |
||
39 | |||
40 | /** |
||
41 | * @return WebSocketClient |
||
42 | */ |
||
43 | public function accept (): WebSocketClient { |
||
53 | |||
54 | /** |
||
55 | * Sends a payload to all clients in the OK state. |
||
56 | * |
||
57 | * @param int $opCode |
||
58 | * @param string $payload |
||
59 | */ |
||
60 | public function broadcast (int $opCode, string $payload) { |
||
67 | |||
68 | /** |
||
69 | * @param string $payload |
||
70 | */ |
||
71 | public function broadcastBinary (string $payload) { |
||
74 | |||
75 | /** |
||
76 | * Sends a ping to all clients in the OK state. |
||
77 | * |
||
78 | * @param string $payload |
||
79 | */ |
||
80 | public function broadcastPing (string $payload = '') { |
||
83 | |||
84 | /** |
||
85 | * Sends a message to all clients in the OK state. |
||
86 | * |
||
87 | * @param string $text |
||
88 | */ |
||
89 | public function broadcastText (string $text) { |
||
92 | |||
93 | /** |
||
94 | * Closes and removes all clients. |
||
95 | * |
||
96 | * @param int $code |
||
97 | * @param string $reason |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function close (int $code = Frame::CLOSE_INTERRUPT, $reason = '') { |
||
112 | |||
113 | /** |
||
114 | * The number of clients attached. |
||
115 | * |
||
116 | * @return int |
||
117 | */ |
||
118 | public function count (): int { |
||
121 | |||
122 | /** |
||
123 | * @return WebSocketClient[] |
||
124 | */ |
||
125 | public function getClients () { |
||
128 | |||
129 | /** |
||
130 | * @param resource $resource |
||
131 | * @return WebSocketClient |
||
132 | */ |
||
133 | protected function newClient ($resource): WebSocketClient { |
||
136 | |||
137 | /** |
||
138 | * WebSocket servers never get OOB data. |
||
139 | */ |
||
140 | final public function onOutOfBand (): void { |
||
143 | |||
144 | /** |
||
145 | * Auto-accept. |
||
146 | */ |
||
147 | public function onReadable (): void { |
||
150 | |||
151 | /** |
||
152 | * Removes the client from the server and reactor. |
||
153 | * |
||
154 | * @param WebSocketClient $client |
||
155 | */ |
||
156 | public function remove ($client): void { |
||
160 | } |