-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathWebSocket.php
More file actions
executable file
·157 lines (131 loc) · 4.66 KB
/
Copy pathWebSocket.php
File metadata and controls
executable file
·157 lines (131 loc) · 4.66 KB
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
157
<?php
namespace Ratchet\Client;
use Evenement\EventEmitterTrait;
use Evenement\EventEmitterInterface;
use React\Socket\ConnectionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Ratchet\RFC6455\Messaging\MessageBuffer;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\Frame;
class WebSocket implements EventEmitterInterface {
use EventEmitterTrait;
/**
* The request headers sent to establish the connection
* @var \Psr\Http\Message\RequestInterface
*/
public $request;
/**
* The response headers received from the server to establish the connection
* @var \Psr\Http\Message\ResponseInterface
*/
public $response;
/**
* @var \React\Socket\ConnectionInterface
*/
protected $_stream;
/**
* @var \Closure
*/
protected $_close;
/**
* WebSocket constructor.
* @param \React\Socket\ConnectionInterface $stream
* @param \Psr\Http\Message\ResponseInterface $response
* @param \Psr\Http\Message\RequestInterface $request
* @event message
* @event ping
* @event pong
* @event close
* @event error
*/
public function __construct(ConnectionInterface $stream, ResponseInterface $response, RequestInterface $request) {
$this->_stream = $stream;
$this->response = $response;
$this->request = $request;
$self = $this;
$this->_close = function($code = null, $reason = null) use ($self) {
static $sent = false;
if ($sent) {
return;
}
$sent = true;
$self->emit('close', [$code, $reason, $self]);
};
$reusableUAException = new \UnderflowException;
$streamer = new MessageBuffer(
new CloseFrameChecker,
function(MessageInterface $msg) {
$this->emit('message', [$msg, $this]);
},
function(FrameInterface $frame) use (&$streamer) {
switch ($frame->getOpcode()) {
case Frame::OP_CLOSE:
$frameContents = $frame->getPayload();
$reason = '';
$code = unpack('n', substr($frameContents, 0, 2));
$code = reset($code);
if (($frameLen = strlen($frameContents)) > 2) {
$reason = substr($frameContents, 2, $frameLen);
}
$closeFn = $this->_close;
$closeFn($code, $reason);
return $this->_stream->end($streamer->newFrame($frame->getPayload(), true, Frame::OP_CLOSE)->maskPayload()->getContents());
case Frame::OP_PING:
$this->emit('ping', [$frame, $this]);
return $this->send($streamer->newFrame($frame->getPayload(), true, Frame::OP_PONG));
case Frame::OP_PONG:
return $this->emit('pong', [$frame, $this]);
default:
return $this->close(Frame::CLOSE_PROTOCOL);
}
},
false,
function() use ($reusableUAException) {
return $reusableUAException;
}
);
$stream->on('data', [$streamer, 'onData']);
$stream->on('close', function () {
$close = $this->_close;
$close(Frame::CLOSE_ABNORMAL, 'Underlying connection closed');
});
$stream->on('error', function($error) {
$this->emit('error', [$error, $this]);
});
$stream->on('drain', function () {
$this->emit('drain');
});
}
public function send($msg) {
if ($msg instanceof MessageInterface) {
foreach ($msg as $frame) {
$frame->maskPayload();
}
} else {
if (!($msg instanceof Frame)) {
$msg = new Frame($msg);
}
$msg->maskPayload();
}
return $this->_stream->write($msg->getContents());
}
public function close($code = 1000, $reason = '') {
$frame = new Frame(pack('n', $code) . $reason, true, Frame::OP_CLOSE);
$frame->maskPayload();
$this->_stream->write($frame->getContents());
$closeFn = $this->_close;
$closeFn($code, $reason);
$this->_stream->end();
}
public function pause()
{
$this->_stream->pause();
}
public function resume()
{
$this->_stream->resume();
}
}