-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathserver.php
executable file
·155 lines (140 loc) · 4.51 KB
/
server.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
#!/usr/bin/php
<?php
require 'func.php';
$config = require 'config.php';
$redis = new Redis();
$redis->pconnect($config['redis']['host'], $config['redis']['port']);
print "Connection to server sucessfully\n";
print "Server is running: " . $redis->ping()."\n";
if($config['redis']['pass']){
$redis->auth( $config['redis']['pass'] );
}
var_dump($config['redis']);
//if($config['redis']['db']){
// $redis->select( $config['redis']['db'] );
//}
$host = "0.0.0.0";
$ws_port = 8080;
$ws = new swoole_websocket_server($host, $ws_port);
$ws->on('open', function ($ws, $request) {
global $redis;
try {
$req = $request->server;
$uri = explode('/', $req['request_uri']);
$cb_name = $uri[1];
$cb_pass = $uri[2];
$hash = md5($cb_pass. $cb_name);
//if fd is 1, the server had been restarted
if($request->fd==1){
hash_clear($redis);
}
var_dump('open: '.$hash.', fd: '.$request->fd);
//save $fd => $hash
if($redis->hGet('fd.to.hash', $request->fd)!=$hash){
// login success
$redis->hSet('fd.to.hash', $request->fd, $hash);
}
//save $hash => $fd
// this list saves all client ids
// which have connected current clipboard.
$redis->lPush('publish_'.$hash, $request->fd);
// get messages from clipboard
$all = $redis->lRange($hash, 0, -1);
if (!$all) {
$all = [];
} else {
$all = array_reverse($all);
}
$result = array('type'=>'all', 'data'=>$all);
if (!$ws->isEstablished($request->fd)) {
var_dump('fd unset');
return;
}
$ws->push($request->fd, json_encode($result));
} catch(\Exception $e) {
var_dump('Error:'.$e->getMessage());
}
});
$ws->on('message', function ($ws, $frame) {
global $redis;
try {
$hash = $redis->hGet('fd.to.hash', $frame->fd);
if (!$hash) {
var_dump('cannot find hash by fd');
return;
}
$r = json_decode($frame->data, true);
if(empty($r))return;
$msg = $r['msg'];
if($msg==='')return;
switch ($r['type']) {
case 'message':
save_cb($redis, $hash, $msg);
publish($redis, $hash, $ws, $msg);
var_dump("receive msg on '{$hash}' with '{$msg}'");
break;
case 'ping':
publish($redis, $hash, $ws, [
'type' => 'pong', 'msg' => 'pone',
], true);
break;
default:
var_dump('unknown type: '.$frame->data);
break;
}
} catch (\Exception $e) {
var_dump('Error:'.$e->getMessage());
}
});
$ws->on('close', function ($ws, $fd) {
global $redis;
try {
$hash = $redis->hGet('fd.to.hash', $fd);
// remove $hash=>$fd
// remove the client from the push channel.
$redis->lRem('publish_'.$hash, $fd, 0);
// remove $fd=>$hash
// remove login info
$redis->hDel('fd.to.hash', $fd);
echo "client-{$fd} is closed\n";
} catch (\Exception $e) {
var_dump('Error:'.$e->getMessage());
}
});
$ws->on('request', function($request, $response) {
global $redis, $ws;
try {
$server = $request->server;
$pathInfo = $server['path_info'];
$path = explode('/', $pathInfo);
$cb_name = $path[1];
$cb_pass = $path[2];
$hash = md5($cb_pass. $cb_name);
$response->header('Content-Type', 'text/plain; charset=utf-8');
switch($server['request_method']) {
case 'POST':
$content = $request->rawcontent();
save_cb($redis, $hash, $content);
publish($redis, $hash, $ws, $content);
var_dump('content from cli');
break;
case 'GET':
if (count($path) == 3) {
$messages = $redis->lRange($hash, 0, 0);
$str = '';
foreach($messages as $k => $m) {
$m = htmlspecialchars_decode($m);
$str .= "{$m}\n\n";
}
$response->end($str);
} else {
$response->end('');
}
break;
default:
}
} catch (\Exception $e) {
var_dump('Error:'.$e->getMessage());
}
});
$ws->start();