-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.message.php
174 lines (142 loc) · 3.28 KB
/
websocket.message.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
*
* Interface for incoming and outgoing messages
* @author Chris
*
*/
interface IWebSocketMessage{
/**
* Retreive an array of frames of which this message is composed
*
* @return WebSocketFrame[]
*/
public function getFrames();
/**
* Set the body of the message
* This should recompile the array of frames
* @param string $data
*/
public function setData($data);
/**
* Retreive the body of the message
* @return string
*/
public function getData();
/**
* Create a new message
* @param string $data Content of the message to be created
*/
public static function create($data);
/**
* Check if we have received the last frame of the message
*
* @return bool
*/
public function isFinalised();
/**
* Create a message from it's first frame
* @param IWebSocketFrame $frame
* @throws Exception
*/
public static function fromFrame(IWebSocketFrame $frame);
}
/**
* WebSocketMessage compatible with the Hixie Draft #76
* Used for backwards compatibility with older versions of Chrome and
* several Flash fallback solutions
*
* @author Chris
*/
class WebSocketMessage76 implements IWebSocketMessage{
protected $data = '';
protected $frame = null;
public static function create($data){
$o = new self();
$o->setData($data);
return $o;
}
public function getFrames(){
$arr = array();
$arr[] = $this->frame;
return $arr;
}
public function setData($data){
$this->data = $data;
$this->frame = WebSocketFrame76::create(WebSocketOpcode::TextFrame, $data);
}
public function getData(){
return $this->frame->getData();
}
public function isFinalised(){
return true;
}
/**
* Creates a new WebSocketMessage76 from a IWebSocketFrame
* @param IWebSocketFrame $frame
*
* @return WebSocketMessage76 Message composed of the frame provided
*/
public static function fromFrame(IWebSocketFrame $frame){
$o = new self();
$o->frame = $frame;
return $o;
}
}
/**
* WebSocketMessage compatible with the latest draft.
* Should be updated to keep up with the latest changes.
*
* @author Chris
*
*/
class WebSocketMessage implements IWebSocketMessage{
/**
*
* Enter description here ...
* @var WebSocketFrame[];
*/
protected $frames = array();
protected $data = '';
public function setData($data){
$this->data = $data;
$this->createFrames();
}
public static function create($data){
$o = new self();
$o->setData($data);
return $o;
}
public function getData(){
if($this->isFinalised() == false)
throw new WebSocketMessageNotFinalised($this);
$data = '';
foreach($this->frames as $frame){
$data .= $frame->getData();
}
return $data;
}
public static function fromFrame(IWebSocketFrame $frame){
$o = new self();
$o->takeFrame($frame);
return $o;
}
protected function createFrames(){
$this->frames = array(WebSocketFrame::create(WebSocketOpcode::TextFrame, $this->data));
}
public function getFrames(){
return $this->frames;
}
public function isFinalised(){
if(count($this->frames) == 0)
return false;
return $this->frames[count($this->frames)-1]->isFinal();
}
/**
* Append a frame to the message
* @param unknown_type $frame
*/
public function takeFrame(WebSocketFrame $frame){
$this->frames[] = $frame;
}
}