|
@@ -2,12 +2,14 @@
|
|
|
|
|
|
namespace App\Services;
|
|
namespace App\Services;
|
|
|
|
|
|
-use App\Model\DBCache;
|
|
|
|
use App\Module\Base;
|
|
use App\Module\Base;
|
|
use App\Module\Chat;
|
|
use App\Module\Chat;
|
|
use App\Module\Users;
|
|
use App\Module\Users;
|
|
|
|
+use App\Tasks\ChromeExtendTask;
|
|
|
|
+use App\Tasks\PushTask;
|
|
use Cache;
|
|
use Cache;
|
|
use DB;
|
|
use DB;
|
|
|
|
+use Hhxsv5\LaravelS\Swoole\Task\Task;
|
|
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
|
|
use Hhxsv5\LaravelS\Swoole\WebSocketHandlerInterface;
|
|
use Swoole\Http\Request;
|
|
use Swoole\Http\Request;
|
|
use Swoole\WebSocket\Frame;
|
|
use Swoole\WebSocket\Frame;
|
|
@@ -38,22 +40,22 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
$_A = [
|
|
$_A = [
|
|
'__static_langdata' => [],
|
|
'__static_langdata' => [],
|
|
];
|
|
];
|
|
- //
|
|
|
|
- $to = $request->fd;
|
|
|
|
|
|
+ //判断参数
|
|
|
|
+ $fd = $request->fd;
|
|
if (!isset($request->get['token'])) {
|
|
if (!isset($request->get['token'])) {
|
|
- $server->push($to, $this->formatMsgSend([
|
|
|
|
|
|
+ $server->push($fd, Chat::formatMsgSend([
|
|
'messageType' => 'error',
|
|
'messageType' => 'error',
|
|
- 'type' => 'user',
|
|
|
|
- 'content' => [
|
|
|
|
|
|
+ 'body' => [
|
|
'error' => '参数错误'
|
|
'error' => '参数错误'
|
|
],
|
|
],
|
|
]));
|
|
]));
|
|
- $server->close($to);
|
|
|
|
- $this->forgetUser($to);
|
|
|
|
|
|
+ $server->close($fd);
|
|
|
|
+ $this->deleteUser($fd);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- //
|
|
|
|
|
|
+ //判断token
|
|
$token = $request->get['token'];
|
|
$token = $request->get['token'];
|
|
|
|
+ $channel = $request->get['channel'] ?: '';
|
|
$cacheKey = "ws::token:" . md5($token);
|
|
$cacheKey = "ws::token:" . md5($token);
|
|
$username = Cache::remember($cacheKey, now()->addSeconds(1), function () use ($token) {
|
|
$username = Cache::remember($cacheKey, now()->addSeconds(1), function () use ($token) {
|
|
list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($token) . "@@@@");
|
|
list($id, $username, $encrypt, $timestamp) = explode("@", base64_decode($token) . "@@@@");
|
|
@@ -66,57 +68,66 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
});
|
|
});
|
|
if (empty($username)) {
|
|
if (empty($username)) {
|
|
Cache::forget($cacheKey);
|
|
Cache::forget($cacheKey);
|
|
- $server->push($to, $this->formatMsgSend([
|
|
|
|
|
|
+ $server->push($fd, Chat::formatMsgSend([
|
|
'messageType' => 'error',
|
|
'messageType' => 'error',
|
|
- 'type' => 'user',
|
|
|
|
- 'content' => [
|
|
|
|
|
|
+ 'channel' => $channel,
|
|
|
|
+ 'body' => [
|
|
'error' => '会员不存在',
|
|
'error' => '会员不存在',
|
|
],
|
|
],
|
|
]));
|
|
]));
|
|
- $server->close($to);
|
|
|
|
- $this->forgetUser($to);
|
|
|
|
|
|
+ $server->close($fd);
|
|
|
|
+ $this->deleteUser($fd);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- //
|
|
|
|
- $wsid = $this->name2fd($username);
|
|
|
|
- if ($wsid > 0) {
|
|
|
|
- $server->push($wsid, $this->formatMsgSend([
|
|
|
|
- 'messageType' => 'forced',
|
|
|
|
- 'type' => 'user',
|
|
|
|
- 'content' => [
|
|
|
|
- 'ip' => Base::getIp(),
|
|
|
|
- 'time' => time(),
|
|
|
|
- 'new_wsid' => $to,
|
|
|
|
- 'old_wsid' => $wsid,
|
|
|
|
- ],
|
|
|
|
- ]));
|
|
|
|
- }
|
|
|
|
- $this->saveUser($to, $username);
|
|
|
|
- $server->push($to, $this->formatMsgSend([
|
|
|
|
|
|
+ //踢下线
|
|
|
|
+ /*if ($channel != 'chromeExtend') {
|
|
|
|
+ $userLists = $this->getUser('', $channel, $username);
|
|
|
|
+ foreach ($userLists AS $user) {
|
|
|
|
+ $server->push($user['fd'], Chat::formatMsgSend([
|
|
|
|
+ 'messageType' => 'kick',
|
|
|
|
+ 'channel' => $channel,
|
|
|
|
+ 'body' => [
|
|
|
|
+ 'ip' => Base::getIp(),
|
|
|
|
+ 'time' => time(),
|
|
|
|
+ 'newfd' => $fd,
|
|
|
|
+ ],
|
|
|
|
+ ]));
|
|
|
|
+ $this->deleteUser($user['fd']);
|
|
|
|
+ }
|
|
|
|
+ }*/
|
|
|
|
+ //保存用户、发送open事件
|
|
|
|
+ $this->saveUser($fd, $channel, $username);
|
|
|
|
+ $server->push($fd, Chat::formatMsgSend([
|
|
'messageType' => 'open',
|
|
'messageType' => 'open',
|
|
- 'type' => 'user',
|
|
|
|
- 'content' => [
|
|
|
|
- 'swid' => $to,
|
|
|
|
|
|
+ 'channel' => $channel,
|
|
|
|
+ 'body' => [
|
|
|
|
+ 'fd' => $fd,
|
|
],
|
|
],
|
|
]));
|
|
]));
|
|
- //
|
|
|
|
|
|
+ //发送最后一条未发送的信息
|
|
$lastMsg = Base::DBC2A(DB::table('chat_msg')->where('receive', $username)->orderByDesc('indate')->first());
|
|
$lastMsg = Base::DBC2A(DB::table('chat_msg')->where('receive', $username)->orderByDesc('indate')->first());
|
|
if ($lastMsg && $lastMsg['roger'] === 0) {
|
|
if ($lastMsg && $lastMsg['roger'] === 0) {
|
|
- $content = Base::string2array($lastMsg['message']);
|
|
|
|
- $content['resend'] = 1;
|
|
|
|
- $content['id'] = $lastMsg['id'];
|
|
|
|
- $content['username'] = $lastMsg['username'];
|
|
|
|
- $content['userimg'] = Users::userimg($lastMsg['username']);
|
|
|
|
- $content['indate'] = $lastMsg['indate'];
|
|
|
|
- $server->push($to, $this->formatMsgSend([
|
|
|
|
- 'messageType' => 'send',
|
|
|
|
- 'contentId' => $lastMsg['id'],
|
|
|
|
- 'type' => 'user',
|
|
|
|
- 'sender' => $lastMsg['username'],
|
|
|
|
- 'target' => $lastMsg['receive'],
|
|
|
|
- 'content' => $content,
|
|
|
|
- 'time' => $lastMsg['indate'],
|
|
|
|
- ]));
|
|
|
|
|
|
+ $dialog = Chat::openDialog($lastMsg['username'], $lastMsg['receive']);
|
|
|
|
+ if (!Base::isError($dialog)) {
|
|
|
|
+ $dialog = $dialog['data'];
|
|
|
|
+ $unread = intval(DB::table('chat_dialog')->where('id', $dialog['id'])->value(($dialog['recField'] == 1 ? 'unread1' : 'unread2')));
|
|
|
|
+ $body = Base::string2array($lastMsg['message']);
|
|
|
|
+ $body['id'] = $lastMsg['id'];
|
|
|
|
+ $body['resend'] = 1;
|
|
|
|
+ $body['unread'] = $unread;
|
|
|
|
+ $body['username'] = $lastMsg['username'];
|
|
|
|
+ $body['userimg'] = Users::userimg($lastMsg['username']);
|
|
|
|
+ $body['indate'] = $lastMsg['indate'];
|
|
|
|
+ $server->push($fd, Chat::formatMsgSend([
|
|
|
|
+ 'messageType' => 'user',
|
|
|
|
+ 'contentId' => $lastMsg['id'],
|
|
|
|
+ 'channel' => $channel,
|
|
|
|
+ 'username' => $lastMsg['username'],
|
|
|
|
+ 'target' => $lastMsg['receive'],
|
|
|
|
+ 'body' => $body,
|
|
|
|
+ 'time' => $lastMsg['indate'],
|
|
|
|
+ ]));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -132,30 +143,37 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
'__static_langdata' => [],
|
|
'__static_langdata' => [],
|
|
];
|
|
];
|
|
//
|
|
//
|
|
- $data = $this->formatMsgReceive($frame->data);
|
|
|
|
- $feedback = [
|
|
|
|
|
|
+ $data = Chat::formatMsgReceive($frame->data);
|
|
|
|
+ $back = [
|
|
'status' => 1,
|
|
'status' => 1,
|
|
'message' => '',
|
|
'message' => '',
|
|
];
|
|
];
|
|
- switch ($data['type']) {
|
|
|
|
|
|
+ //
|
|
|
|
+ switch ($data['messageType']) {
|
|
/**
|
|
/**
|
|
* 刷新
|
|
* 刷新
|
|
*/
|
|
*/
|
|
case 'refresh':
|
|
case 'refresh':
|
|
- DB::table('users')->where('username', $this->fd2name($frame->fd))->update(['wsdate' => time()]);
|
|
|
|
|
|
+ DB::table('ws')->where([
|
|
|
|
+ 'fd' => $frame->fd,
|
|
|
|
+ 'channel' => $data['channel'],
|
|
|
|
+ ])->update(['update' => time()]);
|
|
break;
|
|
break;
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 未读消息总数
|
|
|
|
|
|
+ * 总未读消息数
|
|
*/
|
|
*/
|
|
case 'unread':
|
|
case 'unread':
|
|
- $from = $this->fd2name($frame->fd);
|
|
|
|
- if ($from) {
|
|
|
|
- $num = intval(DB::table('chat_dialog')->where('user1', $from)->sum('unread1'));
|
|
|
|
- $num+= intval(DB::table('chat_dialog')->where('user2', $from)->sum('unread2'));
|
|
|
|
- $feedback['message'] = $num;
|
|
|
|
|
|
+ $username = DB::table('ws')->where([
|
|
|
|
+ 'fd' => $frame->fd,
|
|
|
|
+ 'channel' => $data['channel'],
|
|
|
|
+ ])->value('username');
|
|
|
|
+ if ($username) {
|
|
|
|
+ $num = intval(DB::table('chat_dialog')->where('user1', $username)->sum('unread1'));
|
|
|
|
+ $num+= intval(DB::table('chat_dialog')->where('user2', $username)->sum('unread2'));
|
|
|
|
+ $back['message'] = $num;
|
|
} else {
|
|
} else {
|
|
- $feedback['message'] = 0;
|
|
|
|
|
|
+ $back['message'] = 0;
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
@@ -163,21 +181,24 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
* 已读会员消息
|
|
* 已读会员消息
|
|
*/
|
|
*/
|
|
case 'read':
|
|
case 'read':
|
|
- $to = $this->name2fd($data['target']);
|
|
|
|
- if ($to) {
|
|
|
|
- $dialog = Chat::openDialog($this->fd2name($frame->fd), $data['target']);
|
|
|
|
- if (!Base::isError($dialog)) {
|
|
|
|
- $dialog = $dialog['data'];
|
|
|
|
- $upArray = [];
|
|
|
|
- if ($dialog['user1'] == $dialog['user2']) {
|
|
|
|
- $upArray['unread1'] = 0;
|
|
|
|
- $upArray['unread2'] = 0;
|
|
|
|
- } else {
|
|
|
|
- $upArray[($dialog['recField'] == 1 ? 'unread2' : 'unread1')] = 0;
|
|
|
|
- }
|
|
|
|
- DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
|
|
|
|
|
|
+ $username = DB::table('ws')->where([
|
|
|
|
+ 'fd' => $frame->fd,
|
|
|
|
+ 'channel' => $data['channel'],
|
|
|
|
+ ])->value('username');
|
|
|
|
+ $dialog = Chat::openDialog($username, $data['target']);
|
|
|
|
+ if (!Base::isError($dialog)) {
|
|
|
|
+ $dialog = $dialog['data'];
|
|
|
|
+ $upArray = [];
|
|
|
|
+ if ($dialog['user1'] == $dialog['user2']) {
|
|
|
|
+ $upArray['unread1'] = 0;
|
|
|
|
+ $upArray['unread2'] = 0;
|
|
|
|
+ } else {
|
|
|
|
+ $upArray[($dialog['recField'] == 1 ? 'unread2' : 'unread1')] = 0;
|
|
}
|
|
}
|
|
|
|
+ DB::table('chat_dialog')->where('id', $dialog['id'])->update($upArray);
|
|
}
|
|
}
|
|
|
|
+ $chromeExtendTask = new ChromeExtendTask($username);
|
|
|
|
+ Task::deliver($chromeExtendTask);
|
|
break;
|
|
break;
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -185,9 +206,13 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
*/
|
|
*/
|
|
case 'roger':
|
|
case 'roger':
|
|
if ($data['contentId'] > 0) {
|
|
if ($data['contentId'] > 0) {
|
|
|
|
+ $username = DB::table('ws')->where([
|
|
|
|
+ 'fd' => $frame->fd,
|
|
|
|
+ 'channel' => $data['channel'],
|
|
|
|
+ ])->value('username');
|
|
DB::table('chat_msg')->where([
|
|
DB::table('chat_msg')->where([
|
|
'id' => $data['contentId'],
|
|
'id' => $data['contentId'],
|
|
- 'receive' => $this->fd2name($frame->fd),
|
|
|
|
|
|
+ 'receive' => $username,
|
|
])->update([
|
|
])->update([
|
|
'roger' => 1,
|
|
'roger' => 1,
|
|
]);
|
|
]);
|
|
@@ -198,21 +223,32 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
* 发给用户
|
|
* 发给用户
|
|
*/
|
|
*/
|
|
case 'user':
|
|
case 'user':
|
|
- $to = $this->name2fd($data['target']);
|
|
|
|
- $res = Chat::saveMessage($this->fd2name($frame->fd), $data['target'], $data['content']);
|
|
|
|
|
|
+ $username = DB::table('ws')->where([
|
|
|
|
+ 'fd' => $frame->fd,
|
|
|
|
+ 'channel' => $data['channel'],
|
|
|
|
+ ])->value('username');
|
|
|
|
+ $res = Chat::saveMessage($username, $data['target'], $data['body']);
|
|
if (Base::isError($res)) {
|
|
if (Base::isError($res)) {
|
|
- $feedback = [
|
|
|
|
|
|
+ $back = [
|
|
'status' => 0,
|
|
'status' => 0,
|
|
'message' => $res['msg'],
|
|
'message' => $res['msg'],
|
|
];
|
|
];
|
|
} else {
|
|
} else {
|
|
- $contentId = $res['data']['id'];
|
|
|
|
- $data['contentId'] = $contentId;
|
|
|
|
- $data['content']['id'] = $contentId;
|
|
|
|
- $feedback['message'] = $contentId;
|
|
|
|
- if ($to) {
|
|
|
|
- $server->push($to, $this->formatMsgSend($data));
|
|
|
|
|
|
+ $resData = $res['data'];
|
|
|
|
+ $back['message'] = $resData['id'];
|
|
|
|
+ $data['contentId'] = $resData['id'];
|
|
|
|
+ $data['body']['id'] = $resData['id'];
|
|
|
|
+ $data['body']['unread'] = $resData['unread'];
|
|
|
|
+ //
|
|
|
|
+ $pushLists = [];
|
|
|
|
+ foreach ($this->getUserOfName($data['target']) AS $item) {
|
|
|
|
+ $pushLists[] = [
|
|
|
|
+ 'fd' => $item['fd'],
|
|
|
|
+ 'msg' => $data
|
|
|
|
+ ];
|
|
}
|
|
}
|
|
|
|
+ $pushTask = new PushTask($pushLists);
|
|
|
|
+ Task::deliver($pushTask);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
|
|
|
|
@@ -220,27 +256,40 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
* 发给整个团队
|
|
* 发给整个团队
|
|
*/
|
|
*/
|
|
case 'team':
|
|
case 'team':
|
|
- if (Base::val($data['content'], 'type') === 'taskA') {
|
|
|
|
- $taskId = intval(Base::val($data['content'], 'taskDetail.id'));
|
|
|
|
|
|
+ if (Base::val($data['body'], 'type') === 'taskA') {
|
|
|
|
+ $taskId = intval(Base::val($data['body'], 'taskDetail.id'));
|
|
if ($taskId > 0) {
|
|
if ($taskId > 0) {
|
|
$userLists = $this->getTaskUsers($taskId);
|
|
$userLists = $this->getTaskUsers($taskId);
|
|
} else {
|
|
} else {
|
|
$userLists = $this->getTeamUsers();
|
|
$userLists = $this->getTeamUsers();
|
|
}
|
|
}
|
|
|
|
+ //
|
|
|
|
+ $pushLists = [];
|
|
foreach ($userLists as $user) {
|
|
foreach ($userLists as $user) {
|
|
|
|
+ $data['messageType'] = 'user';
|
|
$data['target'] = $user['username'];
|
|
$data['target'] = $user['username'];
|
|
- $server->push($user['wsid'], $this->formatMsgSend($data));
|
|
|
|
|
|
+ $pushLists[] = [
|
|
|
|
+ 'fd' => $user['fd'],
|
|
|
|
+ 'msg' => $data
|
|
|
|
+ ];
|
|
}
|
|
}
|
|
|
|
+ $pushTask = new PushTask($pushLists);
|
|
|
|
+ Task::deliver($pushTask);
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
if ($data['messageId']) {
|
|
if ($data['messageId']) {
|
|
- $server->push($frame->fd, $this->formatMsgSend([
|
|
|
|
- 'messageType' => 'feedback',
|
|
|
|
- 'messageId' => $data['messageId'],
|
|
|
|
- 'type' => 'user',
|
|
|
|
- 'content' => $feedback,
|
|
|
|
- ]));
|
|
|
|
|
|
+ $pushLists = [];
|
|
|
|
+ $pushLists[] = [
|
|
|
|
+ 'fd' => $frame->fd,
|
|
|
|
+ 'msg' => [
|
|
|
|
+ 'messageType' => 'back',
|
|
|
|
+ 'messageId' => $data['messageId'],
|
|
|
|
+ 'body' => $back,
|
|
|
|
+ ]
|
|
|
|
+ ];
|
|
|
|
+ $pushTask = new PushTask($pushLists);
|
|
|
|
+ Task::deliver($pushTask);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -252,7 +301,7 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
*/
|
|
*/
|
|
public function onClose(Server $server, $fd, $reactorId)
|
|
public function onClose(Server $server, $fd, $reactorId)
|
|
{
|
|
{
|
|
- $this->forgetUser($fd);
|
|
|
|
|
|
+ $this->deleteUser($fd);
|
|
}
|
|
}
|
|
|
|
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
@@ -260,83 +309,64 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
/** ****************************************************************************** */
|
|
/** ****************************************************************************** */
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 格式化信息(来自接收)
|
|
|
|
- * @param $data
|
|
|
|
- * @return array
|
|
|
|
- */
|
|
|
|
- private function formatMsgReceive($data) {
|
|
|
|
- return $this->formatMsgData(Base::json2array($data));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 格式化信息(用于发送)
|
|
|
|
- * @param $array
|
|
|
|
- * @return string
|
|
|
|
|
|
+ * 保存用户
|
|
|
|
+ * @param $fd
|
|
|
|
+ * @param $channel
|
|
|
|
+ * @param $username
|
|
*/
|
|
*/
|
|
- private function formatMsgSend($array) {
|
|
|
|
- return Base::array2json($this->formatMsgData($array));
|
|
|
|
- }
|
|
|
|
|
|
+ private function saveUser($fd, $channel, $username)
|
|
|
|
+ {
|
|
|
|
+ try {
|
|
|
|
+ DB::transaction(function () use ($username, $channel, $fd) {
|
|
|
|
+ $this->deleteUser($fd);
|
|
|
|
+ DB::table('ws')->updateOrInsert([
|
|
|
|
+ 'key' => md5($fd . '@' . $channel . '@' . $username)
|
|
|
|
+ ], [
|
|
|
|
+ 'fd' => $fd,
|
|
|
|
+ 'username' => $username,
|
|
|
|
+ 'channel' => $channel,
|
|
|
|
+ 'update' => time()
|
|
|
|
+ ]);
|
|
|
|
+ });
|
|
|
|
+ } catch (\Throwable $e) {
|
|
|
|
|
|
- /**
|
|
|
|
- * 格式化信息
|
|
|
|
- * @param array $array
|
|
|
|
- * @return array
|
|
|
|
- */
|
|
|
|
- private function formatMsgData($array = []) {
|
|
|
|
- if (!is_array($array)) {
|
|
|
|
- $array = [];
|
|
|
|
}
|
|
}
|
|
- if (!isset($array['messageType'])) $array['messageType'] = '';
|
|
|
|
- if (!isset($array['messageId'])) $array['messageId'] = '';
|
|
|
|
- if (!isset($array['contentId'])) $array['contentId'] = 0;
|
|
|
|
- if (!isset($array['type'])) $array['type'] = '';
|
|
|
|
- if (!isset($array['sender'])) $array['sender'] = null;
|
|
|
|
- if (!isset($array['target'])) $array['target'] = null;
|
|
|
|
- if (!isset($array['content'])) $array['content'] = [];
|
|
|
|
- if (!isset($array['time'])) $array['time'] = time();
|
|
|
|
- if (!is_array($array['content'])) $array['content'] = [];
|
|
|
|
- $array['contentId'] = intval($array['contentId']);
|
|
|
|
- return $array;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 保存用户wsid
|
|
|
|
|
|
+ * 清除用户
|
|
* @param $fd
|
|
* @param $fd
|
|
- * @param $username
|
|
|
|
*/
|
|
*/
|
|
- private function saveUser($fd, $username)
|
|
|
|
|
|
+ private function deleteUser($fd)
|
|
{
|
|
{
|
|
- $this->forgetUser($fd);
|
|
|
|
- $this->forgetName($username);
|
|
|
|
- DB::table('users')->where('username', $username)->update(['wsid' => $fd, 'wsdate' => time()]);
|
|
|
|
|
|
+ DB::table('ws')->where('fd', $fd)->delete();
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 清除用户wsid
|
|
|
|
- * @param $fd
|
|
|
|
|
|
+ * 获取用户
|
|
|
|
+ * @param string $fd
|
|
|
|
+ * @param string $channel
|
|
|
|
+ * @param string $username
|
|
|
|
+ * @return array
|
|
*/
|
|
*/
|
|
- private function forgetUser($fd)
|
|
|
|
|
|
+ private function getUser($fd = '', $channel = '', $username = '')
|
|
{
|
|
{
|
|
- $this->forgetFd($fd);
|
|
|
|
- DB::table('users')->where('wsid', $fd)->update(['wsid' => 0]);
|
|
|
|
|
|
+ $array = [];
|
|
|
|
+ if ($fd) $array['fd'] = $fd;
|
|
|
|
+ if ($channel) $array['channel'] = $channel;
|
|
|
|
+ if ($username) $array['username'] = $username;
|
|
|
|
+ if (empty($array)) {
|
|
|
|
+ return [];
|
|
|
|
+ }
|
|
|
|
+ return Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where($array)->get());
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 根据wsid清除缓存
|
|
|
|
- * @param $fd
|
|
|
|
- */
|
|
|
|
- private function forgetFd($fd) {
|
|
|
|
- Cache::forget('ws::name:' . $this->fd2name($fd));
|
|
|
|
- Cache::forget('ws::fd:' . $fd);
|
|
|
|
|
|
+ private function getUserOfFd($fd, $channel = '') {
|
|
|
|
+ return $this->getUser($fd, $channel);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 根据username清除缓存
|
|
|
|
- * @param $username
|
|
|
|
- */
|
|
|
|
- private function forgetName($username) {
|
|
|
|
- Cache::forget('ws::fd:' . $this->name2fd($username));
|
|
|
|
- Cache::forget('ws::name:' . $username);
|
|
|
|
|
|
+ private function getUserOfName($username, $channel = '') {
|
|
|
|
+ return $this->getUser('', $channel, $username);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -345,9 +375,8 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
*/
|
|
*/
|
|
private function getTeamUsers()
|
|
private function getTeamUsers()
|
|
{
|
|
{
|
|
- return Base::DBC2A(DB::table('users')->select(['wsid', 'username'])->where([
|
|
|
|
- ['wsid', '>', 0],
|
|
|
|
- ['wsdate', '>', time() - 600],
|
|
|
|
|
|
+ return Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where([
|
|
|
|
+ ['update', '>', time() - 600],
|
|
])->get());
|
|
])->get());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -376,29 +405,8 @@ class WebSocketService implements WebSocketHandlerInterface
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//
|
|
//
|
|
- return Base::DBC2A(DB::table('users')->select(['wsid', 'username'])->where([
|
|
|
|
- ['wsid', '>', 0],
|
|
|
|
- ['wsdate', '>', time() - 600],
|
|
|
|
|
|
+ return Base::DBC2A(DB::table('ws')->select(['fd', 'username', 'channel'])->where([
|
|
|
|
+ ['update', '>', time() - 600],
|
|
])->whereIn('username', array_values(array_unique($userArray)))->get());
|
|
])->whereIn('username', array_values(array_unique($userArray)))->get());
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * wsid获取用户名
|
|
|
|
- * @param $fd
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
- private function fd2name($fd)
|
|
|
|
- {
|
|
|
|
- return DBCache::table('users')->cacheKeyname('ws::fd:' . $fd)->select(['username'])->where('wsid', $fd)->value('username');
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 用户名获取wsid
|
|
|
|
- * @param $username
|
|
|
|
- * @return mixed
|
|
|
|
- */
|
|
|
|
- private function name2fd($username)
|
|
|
|
- {
|
|
|
|
- return DBCache::table('users')->cacheKeyname('ws::name:' . $username)->select(['wsid'])->where('username', $username)->value('wsid');
|
|
|
|
- }
|
|
|
|
}
|
|
}
|