forked from zr0128/broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Broker.php
105 lines (83 loc) · 3.1 KB
/
Broker.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
#!/usr/bin/php
<?php
set_time_limit(0);
include __DIR__ . '/vendor/autoload.php';
include __DIR__ . '/Server.php';
use Dotenv\Dotenv;
/**
* 在线用户服务
* @package app\commands
*/
class Broker
{
public static $config = [];
/**
* @var \swoole_table
*/
public static $users = null;
/**
* @var swoole_table
*/
private static $_idTable = null; // 存储用户id和fd对应关系的内存表
/**
* @var swoole_table
*/
private static $_fdTable = null; // 存储连接fd相关信息的内存表
/**
* @var \protocols\WebsocketProtocol
*/
private static $_protocol = null;
public static function run() { // ws服务器启动
self::_initEnv();
self::_initMemTable();
new Server();
}
public static function saveLinkInfo(int $fd, $id, string $protocol = '') { // 保存用户连接信息
$row = ['fd' => $fd, 'id' => $id, 'protocol' => $protocol];
if ( ! $protocol) {
unset($row['protocol']);
}
self::$_fdTable->set($fd, $row);
self::$_idTable->set($id, ['fd' => $fd]);
}
public static function getId(int $fd) { // 获取当前连接用户的id
return self::$_fdTable->get($fd)['id'] ?? 0;
}
public static function getProtocol(int $fd) { // 获取当前连接使用的解析协议
$protocol = self::$_fdTable->get($fd)['protocol'] ?? '';
if ( ! $protocol) {
return null;
}
if (is_null(self::$_protocol) || get_class(self::$_protocol) != $protocol) {
self::$_protocol = new $protocol();
}
return self::$_protocol;
}
public static function getFd($id) { // 根据用户id获取fd
return self::$_idTable->get($id)['fd'] ?? '';
}
public static function idExists($id) { // 根据用户id判断当前服务区是否有该用户
return self::$_idTable->exist($id);
}
private static function _initEnv() { // 初始化环境变量
$dotEnv = new Dotenv(__DIR__ . '/config');
$dotEnv->load();
$dotEnv->required(['REDIS_HOST', 'REDIS_PORT', 'REDIS_TIMEOUT']);
self::$config = include __DIR__ . '/config/config.php';
}
private static function _initMemTable() { // 初始化内存表和redis
self::$_idTable = new \swoole_table(getenv('BACKLOG') * 2);
self::$_idTable->column('fd', swoole_table::TYPE_INT);
self::$_fdTable = new \swoole_table(getenv('BACKLOG') * 2);
self::$_fdTable->column('fd', \swoole_table::TYPE_INT);
self::$_fdTable->column('id', \swoole_table::TYPE_STRING, getenv('MAX_ID_LENGTH'));
self::$_fdTable->column('protocol', \swoole_table::TYPE_STRING, getenv('MAX_PROTOCOL_CLASS_LEN'));
// 用户信息内存表
self::$users = new \swoole_table(getenv('MAX_ONLINE'));
self::$users->column('user', \swoole_table::TYPE_STRING, getenv('MAX_USER_SERIALIZE'));
if ( ! self::$_fdTable->create() || ! self::$_idTable->create() || ! self::$users->create()) {
die('创建内存表失败');
}
}
}
Broker::run();