PHP swoole websocket协议上机指南
这一上机实验的开发环境用的是jetbrain公司的phpstorm
上级步骤如下:
- 创建websocket服务端
 
<?php
$server = new swoole_websocket_server('127.0.0.1',);
function onopen($serv,$request)
{
echo "成功开启,已经为标示符".$request->fd."打开连接\n";
}
$server->on('open','onopen');
$server->on('message',function(swoole_websocket_server $sw,$frame)
{
echo "opcode操作符---{$frame->opcode};操作数据---{$frame->data};fd数据来自句柄
---{$frame->fd};finish结束符号{$frame->finish}.\n";
$sw->push($frame->fd,"推送的数据".$frame->data);//数据大小不过2M
});
$server->on('close',function($serv,$fd)
{
echo "来自{$fd}的通信结束\n";
});
$server->start();
?>
- 准备一个html页面,并在页面中附上websocket协议脚本
 
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico"> <title>websocket协议</title> <!-- Bootstrap core CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet"> <!-- Custom styles for this template -->
<link href="../mycss/sticky-footer-navbar.css" rel="stylesheet">
</head> <body> <header>
<!-- Fixed navbar -->
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
<a class="navbar-brand" href="#">Fixed navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarCollapse" aria-controls="navbarCollapse" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarCollapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
</ul>
<form class="form-inline mt-2 mt-md-0">
<input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</nav>
</header> <!-- Begin page content -->
<main role="main" class="container">
<h1 class="mt-5">Sticky footer with fixed navbar</h1>
<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 > .container</code>.</p>
<p>Back to <a href="../sticky-footer">the default sticky footer</a> minus the navbar.</p>
</main> <footer class="footer">
<div class="container">
<span class="text-muted">Place sticky footer content here.</span>
</div>
</footer> <!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="../js/jquery-3.4.1.min.js"></script>
<script>
var myurl = "ws://swsocks.com:9502";
var ws = new WebSocket(myurl);
ws.onopen = function (evt) {
ws.send("hello server");
console.log("向服务器打了个招呼")
};
ws.onmessage = function (evt) {
console.log("哟吼~服务器返回了数据"+evt.data);
};
ws.onclose = function (evt) {
console.log("关闭了面向服务器的连接");
} </script>
</body>
</html>
#精简---websocket部分
<script>
var myurl = "ws://swsocks.com:9502";
var ws = new WebSocket(myurl);
ws.onopen = function (evt) {
ws.send("hello server");
console.log("向服务器打了个招呼")
};
ws.onmessage = function (evt) {
console.log("哟吼~服务器返回了数据"+evt.data);
};
ws.onclose = function (evt) {
console.log("关闭了面向服务器的连接");
} </script>
- 在phpstorm中运行写好的前端页面----右键点击要运行的html标签页,运行Run index.html
 

- 运行结果
 

- 如果运行多个前端页面实例将会显示更多句柄
 
成功开启,已经为标示符1打开连接
opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
成功开启,已经为标示符2打开连接
opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
成功开启,已经为标示符3打开连接
opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
成功开启,已经为标示符4打开连接
opcode操作符---;操作数据---hello server;fd数据来自句柄---;finish结束符号1.
- 优化,把服务端封装成类
 
class myswserver
{
const myip = '127.0.0.1';
const myport = ;
public $serv = null;
public function __construct()
{
$this->serv = new swoole_websocket_server('127.0.0.1',);
$this->serv->on('open',[$this,'onopen']);#可能是swoole特有回调函数形式,可以对比php魔术方法call_user_func
$this->serv->on('message',[$this,'onmessage']);
$this->serv->on('close',[$this,'onclose']);
$this->serv->start();
} public function onopen($serv,$request)
{
echo "连接{$request->fd}已经建立\n";
}
public function onmessage($serv,$frame)
{
echo "已经为{$frame->fd}建立了连接,并发送了数据{$frame->data},其退出标示符为{$frame->finish},其opcode为{$frame->opcode}\n";
$serv->push($frame->fd,$frame->data);//向客户端推送数据
}
public function onclose($serv,$fd)
{
echo "关闭了{$fd}的连接\n";
}
} $serv = new myswserver(); $serv->start();
PHP swoole websocket协议上机指南的更多相关文章
- PHP Swoole websocket协议实现
 - Spring WebSocket踩坑指南
		
Spring WebSocket踩坑指南 本次公司项目中需要在后台与安卓App间建立一个长连接,这里采用了Spring的WebSocket,协议为Stomp. 关于Stomp协议这里就不多介绍了,网上 ...
 - Swoole WebSocket 的应用
		
目录 概述 代码 小结 概述 这是关于 Swoole 学习的第三篇文章:Swoole WebSocket 的应用. 第二篇:Swoole Task 的应用 第一篇:Swoole Timer 的应用 什 ...
 - WebSocket协议探究(二)
		
一 复习和目标 1 复习 协议概述: WebSocket内置消息定界并且全双工通信 WebSocket使用HTTP进行协议协商,协商成功使用TCP连接进行传输数据 WebScoket数据格式支持二进制 ...
 - WebSocket协议探究(一)
		
一 复习和目标 1 复习 上一节使用wireshark抓包分析了WebSocket流量 包含连接的建立:HTTP协议升级WebSocket协议 使用建立完成的WebSocket协议发送数据 2 目标 ...
 - WebSocket协议探究(序章)
		
一 WebSocket协议基于HTTP和TCP协议 与往常一样,进入WebSocket协议学习之前,先进行WebSocket协议抓包,来一个第一印象. WebSocket能实现客户端和服务器间双向.基 ...
 - 阿里云全站加速DCDN全面支持WebSocket协议
		
WebSocket协议可以为网站和应用提供真正的双向通信,具有控制开销.保持连接状态.更强实时性.更好的压缩效果等优点,是当下低延时应用最常采用的一种技术协议.为了更好的满足客户在实时通讯场景下的加速 ...
 - WebSocket协议 与 IO多路复用
		
最近在把 Facebook Message 接入客服系统,由于与 Facebook Message 对接的收发消息都是通过调用 http 接口来实现的,如果想实现即时通讯,还需要在中间加一个 WebS ...
 - WebSocket协议中文版
		
WebSocket协议中文版 摘要 WebSocket协议实现在受控环境中运行不受信任代码的一个客户端到一个从该代码已经选择加入通信的远程主机之间的全双工通信.用于这个安全模型是通常由web浏览器使用 ...
 
随机推荐
- Second largest node in the BST
			
Find the second largest node in the BST 分析: 如果root有右节点,很明显第二大的node有可能在右子树里.唯一不满足的条件就是右子树只有一个node. 这个 ...
 - 【Python】【demo实验32】【回文数的确认】
			
原题: 我的代码: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- #判断一个数字是否为回文数 即 12345654321 x = ...
 - [转帖]Linux 下实践 VxLAN:虚拟机和 Docker 场景
			
Linux 下实践 VxLAN:虚拟机和 Docker 场景 https://www.cnblogs.com/bakari/p/11264520.html 实践了下 没问题 作者写的很perfect ...
 - 我的第一个Java博客
			
1.2019 11.23 Alone in Beijing;
 - ES简介及特点
			
1.ES是什么? ES是一个高度可伸缩的开源的全文检索和分析引擎,它允许你以近实时的方式快速存储.搜索.分析大量数据,ES是基于Lucence开发,隐藏其复杂性,提供了简单易用的restful api ...
 - java 模拟http请求,通过流(stream)的方式,发送json数据和文件
			
发送端: /** * 以流的方式 * 发送文件和json对象 * * @return */ public static String doPostFileStreamAndJsonObj(String ...
 - win32多线程: 线程创建与结束等待
			
#include<Windows.h> #include<iostream> using namespace std; /*1.在启动一个线程之前,必须为线程编写一个全局的线程 ...
 - 20-Perl 正则表达式
			
1.Perl 正则表达式正则表达式(regular expression)描述了一种字符串匹配的模式,可以用来检查一个串是否含有某种子串.将匹配的子串做替换或者从某个串中取出符合某个条件的子串等.Pe ...
 - Struts2 流程原理
			
一.流程图 (转) 二.流程详解 1.服务器传递来的请求,通过ActionContextClearUp.other filters.最后到达StrutsPrepareAndExecuteFilter ...
 - 【原创】Linux基础之logrotate
			
logrotate logrotate ‐ rotates, compresses, and mails system logs logrotate is designed to ease admin ...