当前位置:网站首页>swoole-WebSocket服务
swoole-WebSocket服务
2022-07-27 22:38:00 【生命不止、战斗不息】
什么是WebSocket
WebSocker协议是基于TCP的一种新的网络协议。它实现了浏览器与服务器全双工(full-duplex)通信-允许服务器主动发送信息给客户端。
为什么需要WebSocket
缺陷:HTTP的通信只能由客户端发起
WebSocker特点
- 建立在TCP协议之上
- 性能开销小通信高效
- 客户端可以与任意服务器通信
- 协议标识符ws wss
- 持久化网络通信协议
ws_server.php
//创建WebSocket Server对象,监听0.0.0.0:9502端口
$ws = new Swoole\WebSocket\Server('0.0.0.0', 9502);
//监听WebSocket连接打开事件
$ws->on('Open', function ($ws, $request) {
$ws->push($request->fd, "hello, welcome\n");
});
//监听WebSocket消息事件
$ws->on('Message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
});
//监听WebSocket连接关闭事件
$ws->on('Close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();- 客户端向服务器端发送信息时,服务器端触发
onMessage事件回调 - 服务器端可以调用
$server->push()向某个客户端(使用 $fd 标识符)发送消息
运行程序
php ws_server.php可以使用 Chrome 浏览器进行测试,JS 代码为:
var wsServer = 'ws://127.0.0.1:9502';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {
console.log("Connected to WebSocket server.");
};
websocket.onclose = function (evt) {
console.log("Disconnected");
};
websocket.onmessage = function (evt) {
console.log('Retrieved data from server: ' + evt.data);
};
websocket.onerror = function (evt, e) {
console.log('Error occured: ' + evt.data);
};
WebSocket 服务器除了提供 WebSocket 功能之外,实际上也可以处理 HTTP 长连接。只需要增加 onRequest 事件监听即可实现 Comet 方案 HTTP 长轮询。
边栏推荐
- 【原】【爬虫系列】简要获取一下知乎的最热门话题相关主题及描述信息
- uni-app进阶之样式框架/生产环境
- 110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe
- LeetCode - 寻找两个正序数组的中位数
- 代码随想录笔记_哈希_1002查找共用字符
- Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
- Resolved Unicode decodeerror: 'UTF-8' codec can't decode byte 0xa1 in position 0: invalid start byte
- [original] [crawler series] briefly get the most popular topics, related topics and description information
- 【C语言入门】ZZULIOJ 1026-1030
- C language programming | explanation and Simulation of offsetof macro
猜你喜欢
![[BuildRelease Management]Parabuild](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[BuildRelease Management]Parabuild
![Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning](/img/1c/5b9726b16f67dfc2016a0c2035baae.png)
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning

Examples of application of JMeter in performance testing

Interface test practical project 02: read interface test documents and practice

网络安全漏洞分析与漏洞复现
![Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]](/img/e3/ea7278eb855cca2a1af4f96772bcb1.png)
Jericho will make a popping sound when turning on, broadcasting a prompt tone, and turning off [chapter]

Multithreading and multithreaded programming

数组相关知识

KMP review + AC automata Prequel

Retinanet网络结构详解
随机推荐
Examples of application of JMeter in performance testing
How to clearly understand and express IAAs, PAAS and SaaS?
DEMO:测试接口短时间内接收重复数据创建单据
Network equipment hard core technology insider firewall and security gateway (V) security double repair method
小波变换学习笔记
Use of postman
At least 42 employees are infected with novel coronavirus! Nokia announces closure of telecom equipment plant in India
《KMP复习 + AC自动机》前传
Analysis and recurrence of network security vulnerabilities
学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
7. Typescript part Foundation
推荐系统-模型:wide&deep 模型
Border width border fillet border color
网络设备硬核技术内幕 防火墙与安全网关篇 (十)
Postman 的使用
Red team killer behinder_ V4.0 (ice scorpion 4.0)
Point divide and conquer analysis
Syntaxerror resolved: positive argument follows keyword argument
Postman下载、使用教程
Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore