From f66b474cc6dd3be06f70184d7d9d60630c277568 Mon Sep 17 00:00:00 2001 From: gzydong <837215079@qq.com> Date: Wed, 2 Dec 2020 15:14:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Command/RemoveWsCacheCommand.php | 2 +- app/Constants/SocketConstants.php | 22 ++++---- app/Controller/WebSocketController.php | 22 ++++++-- app/Service/ArticleService.php | 2 - app/Service/EmoticonService.php | 1 - app/Service/FriendService.php | 1 - app/Service/TalkService.php | 4 ++ app/Service/UploadService.php | 13 +++-- app/Support/SocketIOParser.php | 7 +-- bin/hyperf.php | 1 + config/autoload/mail.php | 76 ++++++++++++++++++++++++++ test.php | 51 +++++++++++++++++ 12 files changed, 171 insertions(+), 31 deletions(-) create mode 100644 config/autoload/mail.php create mode 100644 test.php diff --git a/app/Command/RemoveWsCacheCommand.php b/app/Command/RemoveWsCacheCommand.php index 0f6ea92..79a5e22 100644 --- a/app/Command/RemoveWsCacheCommand.php +++ b/app/Command/RemoveWsCacheCommand.php @@ -4,8 +4,8 @@ declare(strict_types=1); namespace App\Command; -use Hyperf\Command\Command as HyperfCommand; use Hyperf\Command\Annotation\Command; +use Hyperf\Command\Command as HyperfCommand; use Psr\Container\ContainerInterface; use App\Service\SocketClientService; diff --git a/app/Constants/SocketConstants.php b/app/Constants/SocketConstants.php index 02f5a99..091a1c8 100644 --- a/app/Constants/SocketConstants.php +++ b/app/Constants/SocketConstants.php @@ -2,43 +2,41 @@ namespace App\Constants; -use Hyperf\Constants\AbstractConstants; -use Hyperf\Constants\Annotation\Constants; - /** - * WebSocket 消息事件 + * WebSocket 消息事件枚举 * - * @Constants + * Class SocketConstants + * @package App\Constants */ -class SocketConstants extends AbstractConstants +class SocketConstants { /** - * @Message("对话消息通知 - 事件名) + * 对话消息通知 - 事件名 */ const EVENT_TALK = 'event_talk'; /** - * @Message("键盘输入事件通知 - 事件名") + * 键盘输入事件通知 - 事件名 */ const EVENT_KEYBOARD = 'event_keyboard'; /** - * @Message("用户在线状态通知 - 事件名") + * 用户在线状态通知 - 事件名 */ const EVENT_ONLINE_STATUS = 'event_online_status'; /** - * @Message("聊天消息撤销通知 - 事件名") + * 聊天消息撤销通知 - 事件名 */ const EVENT_REVOKE_TALK = 'event_revoke_talk'; /** - * @Message("好友申请消息通知 - 事件名") + * 好友申请消息通知 - 事件名 */ const EVENT_FRIEND_APPLY = 'event_friend_apply'; /** - * @Message("WebSocket 消息消费队列交换机名称") + * WebSocket 消息消费队列交换机名称 */ const CONSUMER_MESSAGE_EXCHANGE = 'im.message.fanout'; } diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 248e358..c57caf0 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -19,6 +19,7 @@ use App\Service\MessageHandleService; use App\Service\SocketRoomService; use App\Model\Group\UsersGroupMember; use App\Amqp\Producer\ChatMessageProducer; +use App\Support\SocketIOParser; /** * Class WebSocketController @@ -77,6 +78,11 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos // 判断是否存在异地登录 $isOnline = $this->socketClientService->isOnlineAll(intval($userInfo['user_id'])); + // 若开启单点登录,则主动关闭当前连接 + if ($isOnline) { + // ... 预留 + } + // 绑定fd与用户关系 $this->socketClientService->bindRelation($request->fd, $userInfo['user_id']); @@ -109,12 +115,20 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos // 判断是否为心跳检测 if ($frame->data == 'PING') return; - [$event, $data] = array_values(json_decode($frame->data, true)); - - if (isset(self::EVENTS[$event])) { - call_user_func_array([$this->messageHandleService, self::EVENTS[$event]], [$server, $frame, $data]); + $result = SocketIOParser::decode($frame->data); + if (!isset(self::EVENTS[$result['event']])) { return; } + + // 回调事件处理函数 + call_user_func_array([ + $this->messageHandleService, + self::EVENTS[$result['event']] + ], [ + $server, + $frame, + $result['data'] + ]); } /** diff --git a/app/Service/ArticleService.php b/app/Service/ArticleService.php index 14e698a..2562fad 100644 --- a/app/Service/ArticleService.php +++ b/app/Service/ArticleService.php @@ -429,8 +429,6 @@ class ArticleService extends BaseService return $res->id; } catch (Exception $e) { Db::rollBack(); - - var_dump($e->getMessage()); } return false; diff --git a/app/Service/EmoticonService.php b/app/Service/EmoticonService.php index 6c8334c..49fce21 100644 --- a/app/Service/EmoticonService.php +++ b/app/Service/EmoticonService.php @@ -122,7 +122,6 @@ class EmoticonService extends BaseService return [false, []]; } - $result = EmoticonDetail::where('user_id', $user_id)->where('url', $fileInfo->save_dir)->first(); if ($result) { return [false, []]; diff --git a/app/Service/FriendService.php b/app/Service/FriendService.php index 4fbd5a9..2ce8fdd 100644 --- a/app/Service/FriendService.php +++ b/app/Service/FriendService.php @@ -2,7 +2,6 @@ namespace App\Service; - use App\Model\User; use App\Model\UsersFriend; use App\Model\UsersFriendsApply; diff --git a/app/Service/TalkService.php b/app/Service/TalkService.php index 6add962..5117d0e 100644 --- a/app/Service/TalkService.php +++ b/app/Service/TalkService.php @@ -683,6 +683,7 @@ class TalkService extends BaseService * * @param $message * @param $fileInfo + * @return bool|int */ public function createImgMessage($message, $fileInfo) { @@ -715,6 +716,7 @@ class TalkService extends BaseService * * @param array $message * @param array $codeBlock + * @return bool|int */ public function createCodeMessage(array $message, array $codeBlock) { @@ -746,6 +748,7 @@ class TalkService extends BaseService * * @param array $message * @param array $emoticon + * @return bool|int */ public function createEmoticonMessage(array $message, array $emoticon) { @@ -777,6 +780,7 @@ class TalkService extends BaseService * * @param array $message * @param array $emoticon + * @return bool|int */ public function createFileMessage(array $message, array $emoticon) { diff --git a/app/Service/UploadService.php b/app/Service/UploadService.php index a9e0c6f..f4b6d7e 100644 --- a/app/Service/UploadService.php +++ b/app/Service/UploadService.php @@ -1,10 +1,8 @@ driver($dir); + $this->makeDirectory($save_dir); $file->moveTo(sprintf('%s/%s', $save_dir, $filename)); if ($file->isMoved()) { - // 修改文集权限 + // 修改文件权限 @chmod(sprintf('%s/%s', $save_dir, $filename), 0644); } diff --git a/app/Support/SocketIOParser.php b/app/Support/SocketIOParser.php index bf563a1..db7fd83 100644 --- a/app/Support/SocketIOParser.php +++ b/app/Support/SocketIOParser.php @@ -23,15 +23,14 @@ class SocketIOParser extends Packet /** * Decode message from websocket client. - * Define and return payload here. * - * @param \Swoole\Websocket\Frame $frame + * @param string $string * * @return array */ - public static function decode($frame) + public static function decode($string) { - $payload = Packet::getPayload($frame->data); + $payload = Packet::getPayload($string); return [ 'event' => $payload['event'] ?? null, diff --git a/bin/hyperf.php b/bin/hyperf.php index bb438e8..3c7e34f 100644 --- a/bin/hyperf.php +++ b/bin/hyperf.php @@ -10,6 +10,7 @@ date_default_timezone_set('Asia/Shanghai'); ! defined('BASE_PATH') && define('BASE_PATH', dirname(__DIR__, 1)); ! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL); +define('SWOOLE_SSL', 512); require BASE_PATH . '/vendor/autoload.php'; // 设置服务运行ID diff --git a/config/autoload/mail.php b/config/autoload/mail.php new file mode 100644 index 0000000..7ba4a59 --- /dev/null +++ b/config/autoload/mail.php @@ -0,0 +1,76 @@ + env('MAIL_HOST', 'smtp.mailgun.org'), + + /* + |-------------------------------------------------------------------------- + | SMTP Host Port + |-------------------------------------------------------------------------- + | + | This is the SMTP port used by your application to deliver e-mails to + | users of the application. Like the host we have set this value to + | stay compatible with the Mailgun e-mail application by default. + | + */ + + 'port' => env('MAIL_PORT', 587), + + /* + |-------------------------------------------------------------------------- + | SMTP Server Username + |-------------------------------------------------------------------------- + | + | If your SMTP server requires a username for authentication, you should + | set it here. This will get used to authenticate with your server on + | connection. You may also set the "password" value below this one. + | + */ + + 'username' => env('MAIL_USERNAME'), + + 'password' => env('MAIL_PASSWORD'), + + /* + |-------------------------------------------------------------------------- + | Global "From" Address + |-------------------------------------------------------------------------- + | + | You may wish for all e-mails sent by your application to be sent from + | the same address. Here, you may specify a name and address that is + | used globally for all e-mails that are sent by your application. + | + */ + + 'from' => [ + 'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), + 'name' => env('MAIL_FROM_NAME', 'Example'), + ], + + /* + |-------------------------------------------------------------------------- + | E-Mail Encryption Protocol + |-------------------------------------------------------------------------- + | + | Here you may specify the encryption protocol that should be used when + | the application send e-mail messages. A sensible default using the + | transport layer security protocol should provide great security. + | + */ + + 'encryption' => env('MAIL_ENCRYPTION', 'tls'), +]; diff --git a/test.php b/test.php new file mode 100644 index 0000000..173e240 --- /dev/null +++ b/test.php @@ -0,0 +1,51 @@ + 'smtp.163.com', + 'port' => 465, + 'username' => '18798276809@163.com', + 'password' => 'RYD18798276809', + 'from' => [ + 'address' => '18798276809@163.com', + 'name' => 'Lumen IM 在线聊天', + ], + 'encryption' => 'ssl', +]; + + +$mail = new PHPMailer(true); +try { + //Server settings + $mail->CharSet = 'UTF-8'; //设定邮件编码,默认ISO-8859-1,如果发中文此项必须设置,否则乱码 + $mail->IsSMTP(); // 设定使用SMTP服务 + $mail->SMTPDebug = 0; // 关闭SMTP调试功能 + $mail->SMTPAuth = true; // 启用 SMTP 验证功能 + $mail->SMTPAutoTLS = false; + $mail->Host = $config['host']; // Set the SMTP server to send through + $mail->Port = intval($config['port']); // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above + $mail->Username = $config['username']; // SMTP username + $mail->Password = $config['password']; // SMTP password + $mail->SMTPSecure = $config['encryption']; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged + + //Recipients + $mail->setFrom($config['from']['address'], $config['from']['name']); + + $mail->addAddress('837215079@qq.com'); // Name is optional + + // Content + $mail->isHTML(true); // Set email format to HTML + $mail->Subject = 'Here is the subject'; + $mail->Body = 'This is the HTML message body in bold!'; + + $mail->send(); + echo 'Message has been sent'; +} catch (Exception $e) { + echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; +}