swoole深入学习 7. WebSocket
标签(空格分隔): swoole
swoole也是支持WebSocket的。websocket也是基于单个TCP 连接上进行全双工通讯的协议,简单的说它是支持长连接。全双工通讯异味这信息的实时性。在websocket之前一直用long poll 和 ajax轮询的方式来实现实时通讯。
websocket既然是基于TCP,那么也分服务端,和客户端。
websocket 例子
先直接来看下,swoole如何实现一个websocket服务端吧:
<?php
/**
* WebSocket
* User: yangyi
* Date: 2017/5/4
* Time: 11:54
*/
$server = new Swoole\WebSocket\Server("0.0.0.0", 9502);
$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
echo "server: handshake success with fd{$request->fd}\n";
});
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n