-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.users.php
148 lines (108 loc) · 2.92 KB
/
websocket.users.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
<?php
interface IWebSocketUser{
public function __construct($socket, WebSocketServer $server);
public function getId();
public function getProtocolVersion();
public function setProtocolVersion($version);
public function getResource();
public function setResource($resource);
public function isAdmin();
public function hasHandshaked();
public function setHandshaked();
public function getHeaders();
public function setHeaders($headers);
public function getLastMessage();
public function addMessage(IWebSocketMessage $msg);
public function getSocket();
public function getServerInstance();
}
/**
* TODO: Getters and setters
*
* @author Chris
*
*/
class WebSocketUser implements IWebSocketUser{
protected $id;
private $socket;
private $resource;
private $ip;
private $handshaked = false;
private $protocol = 0;
/**
*
* Enter description here ...
* @var WebSocketMessage
*/
private $message;
private $headers = array();
private $cookies = array();
private $isAdmin = false;
private $_server;
public function __construct($socket, WebSocketServer $server){
$this->socket = $socket;
$address = '';
socket_getpeername($socket, &$address);
$this->ip = $address;
$this->id = uniqid("u-");
$this->_server = $server;
}
public function getId(){
return $this->id;
}
public function getHeaders(){
return $this->headers;
}
public function setHeaders($headers){
$this->headers = $headers;
if(array_key_exists("Cookie", $this->headers)){
$this->_cookies = WebSocketFunctions::cookie_parse($this->headers['Cookie']);
}else $this->_cookies = array();
$this->isAdmin = array_key_exists('Admin-Key', $this->headers)
&& $this->headers['Admin-Key'] == $this->getServerInstance()->getAdminKey();
// Incorrect admin-key
if($this->isAdmin == false && array_key_exists('Admin-Key', $this->headers))
throw new WebSocketNotAuthorizedException($this);
}
public function createMessage($data){
if($this->protocol == WebSocketProtocolVersions::HIXIE_76)
return WebSocketMessage76::create($data);
else return WebSocketMessage::create($data);
}
public function hasHandshaked(){
return $this->handshaked;
}
public function setHandshaked(){
$this->handshaked = true;
}
public function getProtocolVersion(){
return $this->protocol;
}
public function getCookies(){
return $this->_cookies;
}
public function setProtocolVersion($version){
$this->protocol = $version;
}
public function getResource(){
return $this->resource;
}
public function setResource($resource){
$this->resource = $resource;
}
public function isAdmin(){
return $this->isAdmin;
}
public function getLastMessage(){
return $this->message;
}
public function getSocket(){
return $this->socket;
}
public function addMessage(IWebSocketMessage $msg){
$this->message = $msg;
}
public function getServerInstance(){
return $this->_server;
}
}