forked from gaoqin31/websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswoole.php
70 lines (60 loc) · 1.82 KB
/
swoole.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
<?php
class Server{
private static $instance;
private $server;
//历史留言内容
private $allMsg = array('cmd'=>'allmsg', 'msg'=>array());
private function __construct() {}
private function __clone() {}
public static function getInstance(){
if(self::$instance == null){
self::$instance = new self();
}
return self::$instance;
}
public function run(){
$this->server = new swoole_websocket_server("0.0.0.0", 1027);
$this->server->on('open', function(swoole_websocket_server $server, $request){
$this->open($server, $request);
});
$this->server->on('message', function(swoole_websocket_server $server, $frame){
$this->message($server, $frame);
});
$this->server->on('close', function($server, $fd){
$this->close($server, $fd);
});
$this->server->start();
}
//握手
private function open(swoole_websocket_server $server, $request) {
$this->broadcast($this->allMsg);
//更新客户端在线人数
$this->broadcast(array('cmd'=>'getCnt', 'num'=>$this->getCnt()));
}
//客户端信息
private function message(swoole_websocket_server $server, $frame) {
if($frame->data){
$msg = json_decode($frame->data, true);
$msg['time'] = date('Y-m-d H:i:s');
array_unshift($this->allMsg['msg'], $msg);
if($msg['cmd'] == 'msg'){
$this->broadcast($msg);
}
}
}
//客户端关闭连接
private function close($ser, $fd) {
//更新客户端在线人数
$this->broadcast(array('cmd'=>'getCnt', 'num'=>$this->getCnt()));
}
//广播
private function broadcast(array $msg){
foreach ($this->server->connections as $fd) {
$this->server->push($fd, json_encode($msg));
}
}
private function getCnt(){
return count($this->server->connections);
}
}
Server::getInstance()->run();