-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.php
116 lines (89 loc) · 3.83 KB
/
websocket.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
<?php
require_once 'autoload.php';
use Swoole\WebSocket\Server;
Co::set(['hook_flags'=> SWOOLE_HOOK_TCP | SWOOLE_HOOK_SLEEP | SWOOLE_HOOK_CURL]);
$Lite->config->load(['websocket']);
$LiSket = new LiWebSocket($Lite->config->get('websocket'));
$LiSket->start();
class LiWebSocket {
public $server, $Lite;
public $commTable;
protected $host = '0.0.0.0';
protected $port = 9899;
protected $taskName = 'LiWebSocket';
protected $options = [
'log_file' => __DIR__.'/log/wsket.log',
'pid_file' => __DIR__.'/websocket.pid',
];
public function __construct($param = []) {
$this->port = $param['port'] ?? 9898;
if(isset($param['SSL']) && $param['SSL']==true){
$this->server = new Swoole\WebSocket\Server($this->host, $this->port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL );
}else{
$this->server = new Swoole\WebSocket\Server($this->host, $this->port, SWOOLE_PROCESS, SWOOLE_SOCK_TCP );
}
if (!empty($param)) {
$this->taskName = $param['taskName'];
$options = $param['options'];
$this->options = array_merge($this->options, $options);
}
$this->server -> set($this->options);
LiteApi\LiApiCommVar::$commTable = new Swoole\Table(1024);
LiteApi\LiApiCommVar::$commTable->column('num', Swoole\Table::TYPE_INT);
LiteApi\LiApiCommVar::$commTable->create();
$this->server->on("open", [$this, 'onOpen']);
$this->server->on("message", [$this, 'onMessage']);
$this->server->on("close", [$this, 'onClose']);
$this->server->on("Task", [$this, 'onTask']);
$this->server->on("Finish", [$this, 'onTaskFinish']);
$this->server->on('WorkerStart', function ($serv, $worker_id){
//$this->Lite = new LiteApi\LiteApi();
$this->Lite = new LiteApi\WebSketRepo($this->server);
$this->Lite->config->load(['websocket']);
$wSket = new LiteApi\WebSocket($this->Lite, $this->server);
$wSket->onWorkerStart($serv, $worker_id);
unset($wSket);
});
$this->server->on('Start', function($serv){
swoole_set_process_name($this->taskName);
});
$this->server->on('shutdown',function ($serv) {
$wSket = new LiteApi\WebSocket(new LiteApi\LiteApi($this->server), $this->server);
$wSket->onShutdown($serv);
unset($wSket);
});
}
public function __destruct() {
}
public function onOpen(Swoole\WebSocket\Server $serv, $request) {
$wSket = new LiteApi\WebSocket($this->Lite, $this->server);
$wSket->onOpen($request);
unset($wSket);
}
public function onMessage(Swoole\WebSocket\Server $serv, $frame) {
$wSket = new LiteApi\WebSocket($this->Lite, $this->server);
$wSket->onMessage($frame);
unset($wSket);
}
public function onTask($serv, $task_id, $from_id, $data) {
$wSket = new LiteApi\WebSocket($this->Lite, $this->server);
$fine = $wSket->onTask($serv, $task_id, $from_id, $data);
if(!empty($fine) && count($fine)){
$serv->finish($fine);
}
unset($wSket);
}
public function onTaskFinish($serv, $task_id, $data) {
$wSket = new LiteApi\WebSocket($this->Lite, $this->server);
$wSket->onTaskFinish($serv, $task_id, $data);
unset($wSket);
}
public function onClose($ser, $fd) {
$wSket = new LiteApi\WebSocket($this->Lite, $ser);
$wSket->offline($fd);
unset($wSket);
}
public function start(){
$this->server->start();
}
}