forked from sy-records/game-ddz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebSocketController.php
204 lines (191 loc) · 6.72 KB
/
WebSocketController.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
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
<?php
declare(strict_types=1);
namespace App\Controller;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Swoole\Http\Request;
use Swoole\Server;
use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
use Psr\Container\ContainerInterface;
use App\Game\Core\Packet;
use App\Game\Core\Dispatch;
use App\Game\Core\Log;
use App\Game\Conf\MainCmd;
use App\Game\Conf\SubCmd;
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
{
/**
* @var ContainerInterface
*/
private $container;
// 通过在构造函数的参数上声明参数类型完成自动注入
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function onMessage(WebSocketServer $server, Frame $frame): void
{
Log::show(" Message: client #{$frame->fd} push success Mete: \n{");
$data = Packet::packDecode($frame->data);
if(isset($data['code']) && $data['code'] == 0 && isset($data['msg']) && $data['msg'] == 'OK') {
Log::show('Recv <<< cmd='.$data['cmd'].' scmd='.$data['scmd'].' len='.$data['len'].' data='.json_encode($data['data']));
//转发请求,代理模式处理,websocket路由到相关逻辑
$data['serv'] = $server;
//用户登陆信息
$game_conf = config('game');
$user_info_key = sprintf($game_conf['user_info_key'], $frame->fd);
$uinfo = redis()->get($user_info_key);
if($uinfo) {
$data['userinfo'] = json_decode($uinfo, true);
} else {
$data['userinfo'] = array();
}
$obj = new Dispatch($data, $this->container);
$back = "<center><h1>404 Not Found</h1></center><hr><center>Swoole</center>\n";
if(!empty($obj->getStrategy())) {
$back = $obj->exec();
if($back) {
$server->push($frame->fd, $back, WEBSOCKET_OPCODE_BINARY);
}
}
Log::show('Tcp Strategy <<< data='.$back);
} else {
Log::show($data['msg']);
}
Log::split('}');
}
public function onClose(Server $server, int $fd, int $reactorId): void
{
//清除登陆信息变量
$this->loginFail($fd, '3');
}
public function onOpen(WebSocketServer $server, Request $request): void
{
$fd = $request->fd;
$game_conf = config('game');
$query = $request->get;
$cookie = $request->cookie;
$token = '';
if (isset($cookie['USER_INFO'])) {
$token = $cookie['USER_INFO'];
} elseif (isset($query['token'])) {
$token = $query['token'];
}
if ($token) {
$uinfo = json_decode($token, true);
//允许连接, 并记录用户信息
$uinfo['fd'] = $fd;
$redis = redis();
$user_bind_key = sprintf($game_conf['user_bind_key'], $uinfo['account']);
$last_fd = (int)$redis->get($user_bind_key);
//之前信息存在,清除之前的连接
if ($last_fd) {
if ($server->exist($last_fd) && $server->isEstablished($last_fd)) {
//处理双开的情况
$this->loginFail($last_fd, '1');
$server->disconnect($last_fd);
}
//清理redis
$redis->del($user_bind_key); //清除上一个绑定关系
$redis->del(sprintf($game_conf['user_info_key'], $last_fd)); //清除上一个用户信息
}
//保存登陆信息
$redis->set($user_bind_key, $fd, $game_conf['expire']);
//设置绑定关系
$redis->set(sprintf($game_conf['user_info_key'], $fd), json_encode($uinfo), $game_conf['expire']);
$this->loginSuccess($server, $fd, $uinfo['account']); //登陆成功
} else {
if ($server->exist($fd) && $server->isEstablished($fd)) {
$this->loginFail($fd, '2');
$server->disconnect($fd);
}
}
}
/**
* 登陆成功下发协议
* @param $server
* @param $fd
* @param $account
*/
private function loginSuccess($server, $fd, $account)
{
//原封不动发回去
if ($server->getClientInfo($fd) !== false) {
//查询用户是否在房间里面
$info = $this->getRoomData($account);
$data = array('status' => 'success');
if (!empty($info)) {
$data['is_room'] = 1;
} else {
$data['is_room'] = 0;
}
$data = Packet::packFormat('OK', 0, $data);
$back = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::LOGIN_SUCCESS_RESP);
$server->push($fd, $back, WEBSOCKET_OPCODE_BINARY);
}
}
/**
* 发送登陆失败请求到客户端
* @param $server
* @param $fd
* @param string $msg
*/
private function loginFail($fd, $msg = '')
{
//原封不动发回去
$server = server();
if ($server->getClientInfo($fd) !== false) {
$data = Packet::packFormat('OK', 0, array('data' => 'login fail' . $msg));
$back = Packet::packEncode($data, MainCmd::CMD_SYS, SubCmd::LOGIN_FAIL_RESP);
if ($server->exist($fd) && $server->isEstablished($fd)) {
$server->push($fd, $back, WEBSOCKET_OPCODE_BINARY);
}
}
}
/**
* 获取房间信息
* @param $account
* @return array
*/
protected function getRoomData($account)
{
$user_room_data = array();
//获取用户房间号
$room_no = $this->getRoomNo($account);
//房间信息
$game_key = $this->getGameConf('user_room_data');
if($game_key) {
$user_room_key = sprintf($game_key, $room_no);
$user_room_data = redis()->hGetAll($user_room_key);
}
return $user_room_data;
}
/**
* 获取用户房间号
* @param $account
* @return mixed
*/
protected function getRoomNo($account)
{
$game_key = $this->getGameConf('user_room');
//获取用户房间号
$room_key = sprintf($game_key, $account);
$room_no = redis()->get($room_key);
return $room_no ? $room_no : 0;
}
/**
* 返回游戏配置
* @param string $key
* @return string
*/
protected function getGameConf($key = '') {
$conf = config('game');
if(isset($conf[$key])) {
return $conf[$key];
} else {
return '';
}
}
}