-
Notifications
You must be signed in to change notification settings - Fork 15
/
WebSocketClient.php
156 lines (135 loc) · 4.29 KB
/
WebSocketClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
require "PacketHandler.php";
class WebSocketClient {
private $client;
private $state;
private $host;
private $port;
private $handler;
private $buffer;
private $openCb;
private $messageCb;
private $closeCb;
const HANDSHAKING = 1;
const HANDSHAKED = 2;
const WEBSOCKET_OPCODE_CONTINUATION_FRAME = 0x0;
const WEBSOCKET_OPCODE_TEXT_FRAME = 0x1;
const WEBSOCKET_OPCODE_BINARY_FRAME = 0x2;
const WEBSOCKET_OPCODE_CONNECTION_CLOSE = 0x8;
const WEBSOCKET_OPCODE_PING = 0x9;
const WEBSOCKET_OPCODE_PONG = 0xa;
const TOKEN_LENGHT = 16;
public function __construct()
{
$this->client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$this->client->on("connect", [$this, "onConnect"]);
$this->client->on("receive", [$this, "onReceive"]);
$this->client->on("close", [$this, "onClose"]);
$this->client->on("error", [$this, "onError"]);
$this->handler = new PacketHandler();
$this->buffer = "";
}
public function connect($host, $port)
{
$this->host = $host;
$this->port = $port;
$this->client->connect($host, $port);
}
public function sendHandShake()
{
$this->state = static::HANDSHAKING;
$request = $this->handler->buildHandShakeRequest($this->host, $this->port);
$this->client->send($request);
}
public function onConnect($cli)
{
$this->sendHandShake();
}
public function onReceive($cli, $data)
{
if ($this->state == static::HANDSHAKING) {
$this->buffer .= $data;
$pos = strpos($this->buffer, "\r\n\r\n", true);
if ($pos != false) {
$header = substr($this->buffer, 0, $pos + 4);
$this->buffer = substr($this->buffer, $pos + 4);
if (true == $this->handler->verifyUpgrade($header)) {
$this->state = static::HANDSHAKED;
if (isset($this->openCb))
call_user_func($this->openCb, $this);
} else {
echo "handshake failed\n";
}
}
} else if ($this->state == static::HANDSHAKED) {
$this->buffer .= $data;
}
if ($this->state == static::HANDSHAKED) {
try {
$frame = $this->handler->processDataFrame($this->buffer);
} catch (\Exception $e) {
$cli->close();
return;
}
if ($frame != null) {
if (isset($this->messageCb))
call_user_func($this->messageCb, $this, $frame);
}
}
}
public function onClose($cli)
{
if (isset($this->closeCb))
call_user_func($this->closeCb, $this);
}
public function onError($cli)
{
echo "error occurred\n";
}
public function on($event, $callback)
{
if (strcasecmp($event, "open") === 0) {
$this->openCb = $callback;
} else if (strcasecmp($event, "message") === 0) {
$this->messageCb = $callback;
} else if (strcasecmp($event, "close") === 0) {
$this->closeCb = $callback;
} else {
echo "$event is not supported\n";
}
}
public function send($data, $type = 'text')
{
switch($type)
{
case 'text':
$_type = self::WEBSOCKET_OPCODE_TEXT_FRAME;
break;
case 'binary':
case 'bin':
$_type = self::WEBSOCKET_OPCODE_BINARY_FRAME;
break;
case 'ping':
$_type = self::WEBSOCKET_OPCODE_PING;
break;
case 'close':
$_type = self::WEBSOCKET_OPCODE_CONNECTION_CLOSE;
break;
case 'ping':
$_type = self::WEBSOCKET_OPCODE_PING;
break;
case 'pong':
$_type = self::WEBSOCKET_OPCODE_PONG;
break;
default:
echo "$type is not supported\n";
return;
}
$data = \swoole_websocket_server::pack($data, $_type);
$this->client->send($data);
}
public function getTcpClient()
{
return $this->client;
}
}