-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocket.php
More file actions
227 lines (208 loc) · 7.65 KB
/
Copy pathWebSocket.php
File metadata and controls
227 lines (208 loc) · 7.65 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
namespace Base\Tool;
/**
* ▂▃╬▄▄▃▂▁▁
* ●●●█〓████████████▇▇▇▅▅▅▅▅▅▅▅▅▇▅▅ BUG
* ▄▅█████☆█☆█☆███████▄▄▃▂
* ███████████████████████████
* ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤
*
* WebSocket工具类,PHP跟websocket交互咋就这么泪奔呢?
* 客户端在Public/V/Base
* 调用示例:
* new WebSocket('127.0.0.1',2416);
* @param $address 连接地址
* @param $port 端口
* @author 路漫漫
* @link [email protected]
* @version
* v2017/04/12 增加对客户端信息的转发处理以及过滤
* v2017/04/08 增加发送到指定客户端
* 改进CPU占用99%的问题
* 单例模式,感觉用处不大
* v2017/01/17 初版
*/
class WebSocket {
private $sockets;//socket数组
private $users;
private $master;
private static $obj = null;
//关闭clone
private function __clone() {}
private function __construct($address, $port){
// $cfg = Config('socket');后期读取配置host,端口
//开启端口,并监听
$this->master=$this->openSocket($address, $port);
$this->sockets=['s'=>$this->master];
$this->run();
}
public static function ins($address, $port){
if (self::$obj===null){
self::$obj = new self($address, $port);
}
return self::$obj;
}
private function run(){
while(true){
//拿所有的连接
$changes=$this->sockets;
$null = null;
socket_select($changes,$null,$null,0);
//遍历连接
foreach($changes as $sock){
if($sock==$this->master){
//接受一个Socket连接
$client=socket_accept($this->master);
//把该连接存到数组
$this->sockets[]=$client;
//给该连接一个识别符
$this->users[]=[
'socket'=>$client,
'isShakeHand'=>false
];
}else{
//从连接里拿出请求信息
$len = socket_recv($sock,$buffer,2048,0);
$user = $this->search($sock);
if($len<7){
$this->close($sock,$user);
continue;
}
if(!$this->users[$user]['isShakeHand']){
//先握手
$this->woshou($user,$buffer);
}else{
//握手后,处理请求数据
$buffer = $this->uncode($buffer);
$result = $this->notify($buffer);
if ($result === -233){
$this->close($sock,$user);
}else{
if (!$result) $result = $buffer;
$this->e($result);
//返给指定客户端
$this->send($result,$user);
//返回给所有客户端
//$this->send($result);
}
}
}
}
//避免对锁,cpu占用99%等
usleep(100);
}
}
//关闭连接
private function close($sock,$user){
// $k=array_search($sock, $this->sockets);
socket_close($sock);
unset($this->sockets[$user]);
unset($this->users[$user]);
$this->e("连接:$user 关闭");
}
//获取发送socket的客户端
private function search($sock){
foreach ($this->users as $k=>$v){
if($sock==$v['socket'])
return $k;
}
return false;
}
//建立端口监听
private function openSocket($address,$port){
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//阻塞模式
// socket_set_block($server);
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
socket_bind($server, $address, $port);
socket_listen($server);
$this->e('socket服务已开启 : '.date('Y-m-d H:i:s'));
$this->e('开始监听 : '.$address.' ,端口 : '.$port);
return $server;
}
//php的websocket特殊处理,比起node.js哎,说多了都是泪
private function woshou($k,$buffer){
$buf = substr($buffer,strpos($buffer,'Sec-WebSocket-Key:')+18);
$key = trim(substr($buf,0,strpos($buf,"\r\n")));
$new_key = base64_encode(sha1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true));
$new_message = "HTTP/1.1 101 Switching Protocols\r\n";
$new_message .= "Upgrade: websocket\r\n";
$new_message .= "Sec-WebSocket-Version: 13\r\n";
$new_message .= "Connection: Upgrade\r\n";
$new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";
//告诉客户端握手成功
socket_write($this->users[$k]['socket'],$new_message,strlen($new_message));
$this->users[$k]['isShakeHand']=true;
return true;
}
//帧数据解码,php在流泪
private function uncode($str){
$mask = array();
$data = '';
$msg = unpack('H*',$str);
$head = substr($msg[1],0,2);
if (hexdec($head{1}) === 8) {
$data = false;
}else if (hexdec($head{1}) === 1){
$mask[] = hexdec(substr($msg[1],4,2));
$mask[] = hexdec(substr($msg[1],6,2));
$mask[] = hexdec(substr($msg[1],8,2));
$mask[] = hexdec(substr($msg[1],10,2));
$s = 12;
$e = strlen($msg[1])-2;
$n = 0;
for ($i=$s; $i<= $e; $i+= 2) {
$data .= chr($mask[$n%4]^hexdec(substr($msg[1],$i,2)));
$n++;
}
}
return $data;
}
//帧数据编码(发回客户端用),php还是在流泪
private function code($msg){
$msg = json_encode($msg);
$msg = preg_replace(array('/\r$/','/\n$/','/\r\n$/',), '', $msg);
$frame = array();
$frame[0] = '81';
$len = strlen($msg);
$frame[1] = $len<16?'0'.dechex($len):dechex($len);
$frame[2] = $this->ord_hex($msg);
$data = implode('',$frame);
return pack("H*", $data);
}
private function ord_hex($data) {
$msg = '';
$l = strlen($data);
for ($i= 0; $i<$l; $i++) {
$msg .= dechex(ord($data{$i}));
}
return $msg;
}
//编码后发回到客户端
//$user为客户端识别
private function send($msg,$user=null){
$msg = $this->code($msg);
if (is_null($user)){
foreach($this->users as $v){
//发送给所有客户端
socket_write($v['socket'],$msg,strlen($msg));
}
}else{
//发送给指定客户端
socket_write($this->users[$user]['socket'],$msg,strlen($msg));
}
}
//通知对应控制器处理
private function notify($msg=''){
$info = json_decode($msg,true);
if (!isset($info['wl']) || !GetCache($info['wl'])) return -233;
if (!isset($info['data'])) return false;
$call = explode('.',$info['service']);
$parmas = $info['data'];
return eval('$c = new App\C\Home\\'.$call[0].'();return $c->'.$call[1].'($parmas);');
}
//记录到log
private function e($str){
MFLog($str,'socket');
}
}