• hyperf框架WebSocket 服务


    1:安装

    composer require hyperf/websocket-server
    

    2:配置 Server   修改 config/autoload/server.php,增加以下配置。

    1. return [
    2. 'servers' => [
    3. [
    4. 'name' => 'ws',
    5. 'type' => Server::SERVER_WEBSOCKET,
    6. 'host' => '0.0.0.0',
    7. 'port' => 9502,
    8. 'sock_type' => SWOOLE_SOCK_TCP,
    9. 'callbacks' => [
    10. Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
    11. Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
    12. Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
    13. ],
    14. ],
    15. ],
    16. ];

    3:配置路由 在 config/routes.php 文件内增加对应 ws 的 Server 的路由配置,这里的 ws 值取决于您在 config/autoload/server.php 内配置的 WebSocket Server 的 name 值。

    1. Router::addServer('ws', function () {
    2. Router::get('/', 'App\Controller\WebSocketController');
    3. });

    4:创建控制器

    1. declare(strict_types=1);
    2. namespace App\Controller;
    3. use Hyperf\Contract\OnCloseInterface;
    4. use Hyperf\Contract\OnMessageInterface;
    5. use Hyperf\Contract\OnOpenInterface;
    6. use Hyperf\WebSocketServer\Constant\Opcode;
    7. use Swoole\Server;
    8. use Swoole\WebSocket\Server as WebSocketServer;
    9. class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
    10. {
    11. public function onMessage($server, $frame): void
    12. {
    13. // $frame->data 前端发过来的数据
    14. $data = [
    15. "小敏!",
    16. "小明",
    17. "小米",
    18. "华维",
    19. "苹果"
    20. ];
    21. $rand = rand(0,4);
    22. $server->push($frame->fd, '后端: ' . $data[$rand]);
    23. }
    24. public function onClose($server, int $fd, int $reactorId): void
    25. {
    26. echo $fd . '断开连接,处理线程是 ' . $reactorId . PHP_EOL;
    27. }
    28. public function onOpen($server, $request): void
    29. {
    30. $server->push($request->fd, $request->fd . ' 号客户端已连接!' . PHP_EOL);
    31. }
    32. }

    5:前端页面测试

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport"
    6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    8. <title>websockettitle>
    9. head>
    10. <body>
    11. <div>
    12. <textarea name="content" id="content" cols="30" rows="10">textarea>
    13. <button onclick="send();">发送button>
    14. div>
    15. <ul id="messageList">ul>
    16. <script>
    17. let ws = new WebSocket('ws://127.0.0.1:9101')
    18. ws.onopen = event => {
    19. console.log('连接服务器成功');
    20. }
    21. ws.onmessage = event => {
    22. let data = event.data;
    23. let ul = document.getElementById('messageList');
    24. let li = document.createElement('li');
    25. li.innerHTML = data;
    26. ul.appendChild(li);
    27. }
    28. ws.onclose = event => {
    29. console.log('客户端连接关闭');
    30. }
    31. function send() {
    32. let obj = document.getElementById('content');
    33. let content = obj.value;
    34. let ul = document.getElementById('messageList');
    35. let li = document.createElement('li');
    36. li.innerHTML = "我:"+content;
    37. ul.appendChild(li);
    38. ws.send(content);
    39. obj.value = '';
    40. }
    41. script>
    42. body>
    43. html>

  • 相关阅读:
    .NET源码解读kestrel服务器及创建HttpContext对象流程
    1网络模型
    三维模型3DTile格式轻量化云端处理技术方法分析
    如何使用HTML和CSS创建动画条形图?
    使用gitee go将spring boot项目部署到云主机上并运行
    2021年研究生数学建模D题:抗胰腺癌候选药物的优化建模
    苹果与芯片巨头Arm达成20年新合作协议,将继续采用芯片技术
    深度学习论文: ISTDU-Net:Infrared Small-Target Detection U-Net及其PyTorch实现
    正则表达式绕过
    [论文阅读] Unpaired Image-to-Image Translation Using Adversarial Consistency Loss
  • 原文地址:https://blog.csdn.net/qq_33212312/article/details/133746816