当前位置:网站首页>Swoole websocket service
Swoole websocket service
2022-07-28 01:11:00 【Life goes on and battles go on】
What is? WebSocket
WebSocker The protocol is based on TCP A new network protocol . It realizes full duplex between browser and server (full-duplex) signal communication - Allow the server to actively send information to the client .
Why WebSocket
defects :HTTP Communication can only be initiated by the client
WebSocker characteristic
- Based on the TCP The agreement above
- Low performance overhead and efficient communication
- The client can communicate with any server
- Protocol identifier ws wss
- Persistent network communication protocol
ws_server.php
// establish WebSocket Server object , monitor 0.0.0.0:9502 port
$ws = new Swoole\WebSocket\Server('0.0.0.0', 9502);
// monitor WebSocket Connection open event
$ws->on('Open', function ($ws, $request) {
$ws->push($request->fd, "hello, welcome\n");
});
// monitor WebSocket News events
$ws->on('Message', function ($ws, $frame) {
echo "Message: {$frame->data}\n";
$ws->push($frame->fd, "server: {$frame->data}");
});
// monitor WebSocket Connection close event
$ws->on('Close', function ($ws, $fd) {
echo "client-{$fd} is closed\n";
});
$ws->start();- When the client sends information to the server , Server side trigger
onMessageEvent callback - The server side can call
$server->push()To a client ( Use $fd identifier ) Send a message
Run the program
php ws_server.phphave access to Chrome Browser to test ,JS The code is :
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 In addition to providing WebSocket Besides the function , In fact, it can also handle HTTP A long connection . Just add onRequest Event monitoring can be realized Comet programme HTTP Long polling .
边栏推荐
- 接口测试实战项目02:读懂接口测试文档,上手操练
- 论文赏析[ICLR18]联合句法和词汇学习的神经语言模型
- 安全检测风险
- Basic use of calculation attributes
- Wavelet transform learning notes
- Jointly create a new chapter in cultural tourism | xinqidian signs a strategic cooperation agreement with Guohua cultural tourism
- 共创文旅新篇章|新起典与国华文旅签订战略合作协议
- Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
- 程序员成长第三十篇:你真的懂反馈吗?
- When Jerry made a phone call, recording to SD card /u disk was not enough [article]
猜你喜欢

Operator depth anatomy

网络安全漏洞分析与漏洞复现

Wavelet transform learning notes

Ink wheel salon | Li Wenjie, Peking University: a graph database system for knowledge atlas application gstore

Swoole定时器

How to clearly understand and express IAAs, PAAS and SaaS?

Database daily question --- day 22: last login

深度刨析数据在内存中的存储

跨桌面端Web容器演进

If asynchronous processing is implemented according to the framework
随机推荐
Database daily question --- day 22: last login
Shell系统学习之循环结构
学习笔记12:Eratosthenes筛选法求素数(100以内) 和 魔方阵
Thesis appreciation [iclr18] a neural language model combining syntax and vocabulary learning
Sign up now | cloud native technology exchange meetup Guangzhou station has been opened, and I will meet you on August 6!
推荐系统-模型:dssm双塔模型做embedding的召回
接口测试实战项目02:读懂接口测试文档,上手操练
红队大杀器 Behinder_v4.0(冰蝎4.0)
Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
Network device hard core technology insider firewall and security gateway (10)
LeetCode - 寻找两个正序数组的中位数
Scrollview, tableview nested solutions
Recommendation system model: wide & deep model
推荐系统-指标:ctr、cvr
怎么清晰地理解、表达 IaaS 、 PaaS 、 SaaS ?
Uniapp display rich text effect demo (organize)
Tear the source code of gateway by hand, and tear the source code of workflow and load balancing today
Understanding of cap
Recommended system model (III): fine scheduling model [LR, gbdt, wide & deep, DCN, DIN, Dien, MMOE, ple]
C语言程序设计 | offsetof宏的讲解及其模拟实现