1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Websocket\SocketIO; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Packet |
7
|
|
|
*/ |
8
|
|
|
class Packet |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Socket.io packet type `open`. |
12
|
|
|
*/ |
13
|
|
|
const OPEN = 0; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Socket.io packet type `close`. |
17
|
|
|
*/ |
18
|
|
|
const CLOSE = 1; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Socket.io packet type `ping`. |
22
|
|
|
*/ |
23
|
|
|
const PING = 2; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Socket.io packet type `pong`. |
27
|
|
|
*/ |
28
|
|
|
const PONG = 3; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Socket.io packet type `message`. |
32
|
|
|
*/ |
33
|
|
|
const MESSAGE = 4; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Socket.io packet type 'upgrade' |
37
|
|
|
*/ |
38
|
|
|
const UPGRADE = 5; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Socket.io packet type `noop`. |
42
|
|
|
*/ |
43
|
|
|
const NOOP = 6; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Engine.io packet type `connect`. |
47
|
|
|
*/ |
48
|
|
|
const CONNECT = 0; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Engine.io packet type `disconnect`. |
52
|
|
|
*/ |
53
|
|
|
const DISCONNECT = 1; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Engine.io packet type `event`. |
57
|
|
|
*/ |
58
|
|
|
const EVENT = 2; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Engine.io packet type `ack`. |
62
|
|
|
*/ |
63
|
|
|
const ACK = 3; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Engine.io packet type `error`. |
67
|
|
|
*/ |
68
|
|
|
const ERROR = 4; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Engine.io packet type 'binary event' |
72
|
|
|
*/ |
73
|
|
|
const BINARY_EVENT = 5; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Engine.io packet type `binary ack`. For acks with binary arguments. |
77
|
|
|
*/ |
78
|
|
|
const BINARY_ACK = 6; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Socket.io packet types. |
82
|
|
|
*/ |
83
|
|
|
public static $socketTypes = [ |
84
|
|
|
0 => 'OPEN', |
85
|
|
|
1 => 'CLOSE', |
86
|
|
|
2 => 'PING', |
87
|
|
|
3 => 'PONG', |
88
|
|
|
4 => 'MESSAGE', |
89
|
|
|
5 => 'UPGRADE', |
90
|
|
|
6 => 'NOOP', |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Engine.io packet types. |
95
|
|
|
*/ |
96
|
|
|
public static $engineTypes = [ |
97
|
|
|
0 => 'CONNECT', |
98
|
|
|
1 => 'DISCONNECT', |
99
|
|
|
2 => 'EVENT', |
100
|
|
|
3 => 'ACK', |
101
|
|
|
4 => 'ERROR', |
102
|
|
|
5 => 'BINARY_EVENT', |
103
|
|
|
6 => 'BINARY_ACK', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get socket packet type of a raw payload. |
108
|
|
|
* |
109
|
|
|
* @param string $packet |
110
|
|
|
* |
111
|
|
|
* @return int|null |
112
|
|
|
*/ |
113
|
|
|
public static function getSocketType(string $packet) |
114
|
|
|
{ |
115
|
|
|
$type = $packet[0] ?? null; |
116
|
|
|
|
117
|
|
|
if (! array_key_exists($type, static::$socketTypes)) { |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return (int) $type; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get data packet from a raw payload. |
126
|
|
|
* |
127
|
|
|
* @param string $packet |
128
|
|
|
* |
129
|
|
|
* @return array|null |
130
|
|
|
*/ |
131
|
|
|
public static function getPayload(string $packet) |
132
|
|
|
{ |
133
|
|
|
$packet = trim($packet); |
134
|
|
|
$start = strpos($packet, '['); |
135
|
|
|
|
136
|
|
|
if ($start === false || substr($packet, -1) !== ']') { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$data = substr($packet, $start, strlen($packet) - $start); |
141
|
|
|
$data = json_decode($data, true); |
142
|
|
|
|
143
|
|
|
if (is_null($data)) { |
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return [ |
148
|
|
|
'event' => $data[0], |
149
|
|
|
'data' => $data[1] ?? null, |
150
|
|
|
]; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Return if a socket packet belongs to specific type. |
155
|
|
|
* |
156
|
|
|
* @param $packet |
157
|
|
|
* @param string $typeName |
158
|
|
|
* |
159
|
|
|
* @return bool |
160
|
|
|
*/ |
161
|
|
|
public static function isSocketType($packet, string $typeName) |
162
|
|
|
{ |
163
|
|
|
$type = array_search(strtoupper($typeName), static::$socketTypes); |
164
|
|
|
|
165
|
|
|
if ($type === false) { |
166
|
|
|
return false; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return static::getSocketType($packet) === $type; |
170
|
|
|
} |
171
|
|
|
} |