当前位置:网站首页>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 .
边栏推荐
- oracle分组取最大值
- Syntaxerror resolved: positive argument follows keyword argument
- Scrollview, tableview nested solutions
- uniapp显示富文本效果demo(整理)
- Go 语言变量
- 跨桌面端Web容器演进
- Network device hard core technology insider firewall and security gateway (VIII) virtualization artifact (middle)
- Function related knowledge
- Branch and loop sentence exercises
- Ford SUV "Mustang" officially went offline, safe and comfortable
猜你喜欢
随机推荐
c# 反射之Type使用
Syntaxerror resolved: positive argument follows keyword argument
[CruiseControl]Build Result JSP
Leetcode:1997. the first day after visiting all rooms [jump DP]
[300 opencv routines] 241. Scale invariant feature transformation (SIFT)
Database daily question --- day 22: last login
Recommended system - indicators: CTR, CVR
Add a picture in front of the cell
Swoole协程
[CruiseControl]Build Result JSP
如果某个表有近千万数据,CRUD比较慢,如何优化
[proteus simulation] 51 single chip microcomputer washing machine simulation control program
Postman 的使用
Can TSMC Samsung build a production line without American equipment for Huawei?
推荐系统-模型(三):精排模型【LR、GBDT、Wide&Deep、DCN、DIN、DIEN、MMOE、PLE】
Tear the source code of gateway by hand, and tear the source code of workflow and load balancing today
《KMP复习 + AC自动机》前传
函数相关知识
Operators in MySQL
SRv6初登场




![[BuildRelease Management]Parabuild](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)




