请选择 进入手机版 | 继续访问电脑版
【技术分享】基于Swoole简单群聊小功能的实现
27094
10
0
2020-6-24 16:21
查看: 27094|回复: 10

【技术分享】基于Swoole简单群聊小功能的实现

[复制链接]
昨天开始学的Swoole


作为学习路上的一个记录吧


也给还没开始学Swoole的同学提供一个Swoole基础的小案例


基于Swoole的Websocket


swoole.png

Websocket部分
  1. <?php
  2. /**
  3. * Class SwooleWs
  4. * WebSocket Study
  5. */
  6. class SwooleWs{
  7.     protected $ws;

  8.     public function __construct()
  9.     {
  10.         $this->ws = new Swoole\WebSocket\Server("0.0.0.0", 9501);
  11.         //连接
  12.         $this->ws->on('open',[$this,'open']);
  13.         //消息发送
  14.         $this->ws->on('message',[$this,'message']);
  15.         //关闭
  16.         $this->ws->on('close',[$this,'close']);
  17.         //启动
  18.         $this->ws->start();
  19.     }

  20.     public function open($server, $request)
  21.     {
  22.         echo "客户端{$request->fd}已连接\n";
  23.     }

  24.     public function message($server, $frame)
  25.     {
  26.         foreach ($server->connections as $fd){
  27.             $frame->fd == $fd ? $oop="我:" : $oop = "用户".$frame->fd.":";
  28.             $server->push($fd, $oop.$frame->data);
  29.         }

  30.     }

  31.     public function close($ser, $fd)
  32.     {
  33.         echo "客户端{$fd} 退出\n";
  34.     }
  35. }
  36. new SwooleWs();
复制代码


JS部分
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Swoole NO 1</title>
  6. </head>
  7. <body>
  8.     <h1 id="state"></h1>
  9.     <input type="text" placeholder="请输入发送内容:" id="msg">
  10.     <input type="button" value="发送消息" onclick="send()">
  11.     <div id="chat">
  12.     </div>
  13. </body>
  14. </html>
  15. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  16. <script>
  17.     var wsServer = 'ws://你的外网IP:9501';
  18.     var websocket = new WebSocket(wsServer);
  19.     websocket.onopen = function (evt) {
  20.         $('#state').text('恭喜,服务器连接成功');
  21.     };

  22.     websocket.onclose = function (evt) {
  23.         console.log("Disconnected");
  24.     };

  25.     websocket.onmessage = function (evt) {
  26.         $('#chat').append("<p>"+evt.data+"</p>");
  27.     };

  28.     websocket.onerror = function (evt, e) {
  29.         $('#state').text('服务器连接失败');
  30.     };

  31.     function send(){
  32.         let msg = $('#msg').val();
  33.         websocket.send(msg);
  34.     }
  35. </script>
复制代码


发表于 2020-8-3 20:14:16 | 显示全部楼层
啥也不说了,感谢好麦麦源码分享哇!
发表于 2021-2-21 00:01:07 | 显示全部楼层

Test, just a test


Hello. And Bye.
发表于 2021-3-6 08:56:59 | 显示全部楼层

Test, just a test


Hello. And Bye.
发表于 2021-4-28 22:35:10 | 显示全部楼层

Test, just a test

Hello. And Bye.
发表于 2022-3-17 23:53:46 | 显示全部楼层

Test, just a test


Hello. And Bye.
发表于 2022-3-23 15:59:13 | 显示全部楼层

Test, just a test

Hello. And Bye.
发表于 2022-3-29 18:28:46 | 显示全部楼层

Test, just a test

Hello. And Bye.
发表于 2022-4-6 05:24:07 | 显示全部楼层

Test, just a test

Hello. And Bye.
发表于 2022-5-2 20:20:53 | 显示全部楼层

Test, just a test


Hello. And Bye.
活跃度排行榜
1
http://www.haomaim.cn/uc_server/avatar.php?uid=1&size=small
ruojiu
620
2
http://www.haomaim.cn/uc_server/avatar.php?uid=5938&size=small
it023
381
3
http://www.haomaim.cn/uc_server/avatar.php?uid=6125&size=small
柔情空似水
221
4
http://www.haomaim.cn/uc_server/avatar.php?uid=4014&size=small
XRumerTest
98
5
http://www.haomaim.cn/uc_server/avatar.php?uid=103&size=small
glhynet
76
6
http://www.haomaim.cn/uc_server/avatar.php?uid=6006&size=small
glovy
75
活跃度排行榜
1
http://www.haomaim.cn/uc_server/avatar.php?uid=1&size=small
ruojiu
620
2
http://www.haomaim.cn/uc_server/avatar.php?uid=5938&size=small
it023
381
3
http://www.haomaim.cn/uc_server/avatar.php?uid=6125&size=small
柔情空似水
221
4
http://www.haomaim.cn/uc_server/avatar.php?uid=4014&size=small
XRumerTest
98
5
http://www.haomaim.cn/uc_server/avatar.php?uid=103&size=small
glhynet
76
6
http://www.haomaim.cn/uc_server/avatar.php?uid=6006&size=small
glovy
75
广告

尊贵VIP购买