这一上机实验的开发环境用的是jetbrain公司的phpstorm

上级步骤如下:

  • 创建websocket服务端
  1. <?php
  2. $server = new swoole_websocket_server('127.0.0.1',);
  3. function onopen($serv,$request)
  4. {
  5. echo "成功开启,已经为标示符".$request->fd."打开连接\n";
  6. }
  7. $server->on('open','onopen');
  8. $server->on('message',function(swoole_websocket_server $sw,$frame)
  9. {
  10. echo "opcode操作符---{$frame->opcode};操作数据---{$frame->data};fd数据来自句柄
    ---{$frame->fd};finish结束符号{$frame->finish}.\n";
  11. $sw->push($frame->fd,"推送的数据".$frame->data);//数据大小不过2M
  12. });
  13. $server->on('close',function($serv,$fd)
  14. {
  15. echo "来自{$fd}的通信结束\n";
  16. });
  17. $server->start();
  18. ?>
  • 准备一个html页面,并在页面中附上websocket协议脚本
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <meta name="description" content="">
  7. <meta name="author" content="">
  8. <link rel="icon" href="../../../../favicon.ico">
  9.  
  10. <title>websocket协议</title>
  11.  
  12. <!-- Bootstrap core CSS -->
  13. <link href="../css/bootstrap.min.css" rel="stylesheet">
  14.  
  15. <!-- Custom styles for this template -->
  16. <link href="../mycss/sticky-footer-navbar.css" rel="stylesheet">
  17. </head>
  18.  
  19. <body>
  20.  
  21. <header>
  22. <!-- Fixed navbar -->
  23. <nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
  24. <a class="navbar-brand" href="#">Fixed navbar</a>
  25. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
  26. <span class="navbar-toggler-icon"></span>
  27. </button>
  28. <div class="collapse navbar-collapse" id="navbarCollapse">
  29. <ul class="navbar-nav mr-auto">
  30. <li class="nav-item active">
  31. <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
  32. </li>
  33. <li class="nav-item">
  34. <a class="nav-link" href="#">Link</a>
  35. </li>
  36. <li class="nav-item">
  37. <a class="nav-link disabled" href="#">Disabled</a>
  38. </li>
  39. </ul>
  40. <form class="form-inline mt-2 mt-md-0">
  41. <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
  42. <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  43. </form>
  44. </div>
  45. </nav>
  46. </header>
  47.  
  48. <!-- Begin page content -->
  49. <main role="main" class="container">
  50. <h1 class="mt-5">Sticky footer with fixed navbar</h1>
  51. <p class="lead">Pin a fixed-height footer to the bottom of the viewport in desktop browsers with this custom HTML and CSS. A fixed navbar has been added with <code>padding-top: 60px;</code> on the <code>body &gt; .container</code>.</p>
  52. <p>Back to <a href="../sticky-footer">the default sticky footer</a> minus the navbar.</p>
  53. </main>
  54.  
  55. <footer class="footer">
  56. <div class="container">
  57. <span class="text-muted">Place sticky footer content here.</span>
  58. </div>
  59. </footer>
  60.  
  61. <!-- Bootstrap core JavaScript
  62. ================================================== -->
  63. <!-- Placed at the end of the document so the pages load faster -->
  64. <script src="../js/jquery-3.4.1.min.js"></script>
  65. <script>
  66. var myurl = "ws://swsocks.com:9502";
  67. var ws = new WebSocket(myurl);
  68. ws.onopen = function (evt) {
  69. ws.send("hello server");
  70. console.log("向服务器打了个招呼")
  71. };
  72. ws.onmessage = function (evt) {
  73. console.log("哟吼~服务器返回了数据"+evt.data);
  74. };
  75. ws.onclose = function (evt) {
  76. console.log("关闭了面向服务器的连接");
  77. }
  78.  
  79. </script>
  80. </body>
  81. </html>
  1. #精简---websocket部分
    <script>
  2. var myurl = "ws://swsocks.com:9502";
  3. var ws = new WebSocket(myurl);
  4. ws.onopen = function (evt) {
  5. ws.send("hello server");
  6. console.log("向服务器打了个招呼")
  7. };
  8. ws.onmessage = function (evt) {
  9. console.log("哟吼~服务器返回了数据"+evt.data);
  10. };
  11. ws.onclose = function (evt) {
  12. console.log("关闭了面向服务器的连接");
  13. }
  14.  
  15. </script>
  • 在phpstorm中运行写好的前端页面----右键点击要运行的html标签页,运行Run index.html

  • 运行结果

  • 如果运行多个前端页面实例将会显示更多句柄
  1. 成功开启,已经为标示符1打开连接
  2. opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
  3. 成功开启,已经为标示符2打开连接
  4. opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
  5. 成功开启,已经为标示符3打开连接
  6. opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
  7. 成功开启,已经为标示符4打开连接
  8. opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
  • 优化,把服务端封装成类
  1. class myswserver
  2. {
  3. const myip = '127.0.0.1';
  4. const myport = ;
  5. public $serv = null;
  6. public function __construct()
  7. {
  8. $this->serv = new swoole_websocket_server('127.0.0.1',);
  9. $this->serv->on('open',[$this,'onopen']);#可能是swoole特有回调函数形式,可以对比php魔术方法call_user_func
  10. $this->serv->on('message',[$this,'onmessage']);
  11. $this->serv->on('close',[$this,'onclose']);
  12. $this->serv->start();
  13. }
  14.  
  15. public function onopen($serv,$request)
  16. {
  17. echo "连接{$request->fd}已经建立\n";
  18. }
  19. public function onmessage($serv,$frame)
  20. {
  21. echo "已经为{$frame->fd}建立了连接,并发送了数据{$frame->data},其退出标示符为{$frame->finish},其opcode为{$frame->opcode}\n";
  22. $serv->push($frame->fd,$frame->data);//向客户端推送数据
  23. }
  24. public function onclose($serv,$fd)
  25. {
  26. echo "关闭了{$fd}的连接\n";
  27. }
  28. }
  29.  
  30. $serv = new myswserver();
  31.  
  32. $serv->start();

PHP swoole websocket协议上机指南的更多相关文章

  1. PHP Swoole websocket协议实现

  2. Spring WebSocket踩坑指南

    Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...

  3. Swoole WebSocket 的应用

    目录 概述 代码 小结 概述 这是关于 Swoole 学习的第三篇文章:Swoole WebSocket 的应用. 第二篇:Swoole Task 的应用 第一篇:Swoole Timer 的应用 什 ...

  4. WebSocket协议探究(二)

    一 复习和目标 1 复习 协议概述: WebSocket内置消息定界并且全双工通信 WebSocket使用HTTP进行协议协商,协商成功使用TCP连接进行传输数据 WebScoket数据格式支持二进制 ...

  5. WebSocket协议探究(一)

    一 复习和目标 1 复习 上一节使用wireshark抓包分析了WebSocket流量 包含连接的建立:HTTP协议升级WebSocket协议 使用建立完成的WebSocket协议发送数据 2 目标 ...

  6. WebSocket协议探究(序章)

    一 WebSocket协议基于HTTP和TCP协议 与往常一样,进入WebSocket协议学习之前,先进行WebSocket协议抓包,来一个第一印象. WebSocket能实现客户端和服务器间双向.基 ...

  7. 阿里云全站加速DCDN全面支持WebSocket协议

    WebSocket协议可以为网站和应用提供真正的双向通信,具有控制开销.保持连接状态.更强实时性.更好的压缩效果等优点,是当下低延时应用最常采用的一种技术协议.为了更好的满足客户在实时通讯场景下的加速 ...

  8. WebSocket协议 与 IO多路复用

    最近在把 Facebook Message 接入客服系统,由于与 Facebook Message 对接的收发消息都是通过调用 http 接口来实现的,如果想实现即时通讯,还需要在中间加一个 WebS ...

  9. WebSocket协议中文版

    WebSocket协议中文版 摘要 WebSocket协议实现在受控环境中运行不受信任代码的一个客户端到一个从该代码已经选择加入通信的远程主机之间的全双工通信.用于这个安全模型是通常由web浏览器使用 ...

随机推荐

  1. IDEA安装maven

    1.先到maven的官网下载安装包:http://maven.apache.org/download.cgi 解压安装包 2.配置环境变量 新建变量名MAVEN_HOME 变量值    D:\Soft ...

  2. 【计数】Simple Addition Expression

    [来源] 2008年哈尔滨区域赛 [题目链接]: http://acm.hdu.edu.cn/showproblem.php?pid=2451 [参考博客]: HDU 2451 Simple Addi ...

  3. oracle修改TNSLSNR的端口

    oracle 服务一启动 TNSLSNR.exe 会占用8080端口,这时,如果我们其他程序需要使用8080端口就会比较麻烦,所以需要改一下端口: 用dba账户登录 CMD>sqlplus sy ...

  4. Codeforces1263D-Secret Passwords

    题意 给n个字符串,两个字符串之间如果有相同的字符,那么两个就等价,等价关系可以传递,问最后有多少个等价类. 分析 考虑并查集或者dfs联通块,如果是并查集的话,对于当前字符串的某个字符,肯定要和这个 ...

  5. mysql 8.x 集群出现:Last_IO_Error: error connecting to master 'repl@xxx:3306' - retry-time: 60 retries: 1

    网上的经验:网络不同,账号密码不对,密码太长,密码由 # 字符:检查MASTER_HOST,MASTER_USER,MASTER_PASSWORD(不知道 MASTER_LOG_FILE 有没有影响) ...

  6. JS基础_基本数据类型和引用数据类型

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. 和 Python 2.x 说再见!项目移到python3

    如果你仍在使用 2.x,那么是时候将你的代码移植到 Python 3 了. 在技术的长河中,软件.工具.系统等版本的迭代本是常事,但由于使用习惯.版本的兼容性.易用性等因素,很多用户及开发者在使用或做 ...

  8. centos7配置rsync+inotify数据实时共享

    关于centos7版本上面搭建rsync服务并且实现实时同步之前一直是在6版本上面搭建rsync服务,在7版本上面折腾了半天.此处总结下inotify下载地址:http://github.com/do ...

  9. vue进阶:vue-router之导航守卫、路由元信息、路由懒加载

    1.导航被触发 2.在失活的组件里调用离开守卫:beforeRouteLeave —— 组件内守卫(离开组件). 3.调用全局的beforeEach守卫 —— 全局守卫(进入组件). 4.在重用组件里 ...

  10. vue 登录 + 记住密码 + 密码加密解密

    <template> <el-form :model="ruleForm"> <h3 class="title">系统登录& ...