websocket服务器
Posted hanmengya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了websocket服务器相关的知识,希望对你有一定的参考价值。
1、WebSocket协议是基于tcp协议的一种新型的网络协议。它的实现了浏览器与服务器全双工通信-----允许服务器主动发送信息给客户端
2、http协议通信只能有客户端发送请求
服务端:
$server = new Swoole\WebSocket\Server("0.0.0.0", 9501); $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"; $server->push($frame->fd, "this is server"); }); $server->on(‘close‘, function ($ser, $fd) { echo "client {$fd} closed\n"; }); $server->start();
客户端:
<h1>swooler-websocket测试</h1> <script> var wsurl = "域名:端口号"; var websocket = new WebSocket(weurl); //实例对象onopen属性 websocket.ononnpen = function(evt){ console.log("connected-swoole-connect"); } websocket.onmessage = function (evt){ console.log("server-return-data:"+etv,data); } websocket.onclose = functin(evt){ console.log("close"); } websocket.onerror = function(evt,e){ console.log("server-error"_etv.data); } </script>
以上是关于websocket服务器的主要内容,如果未能解决你的问题,请参考以下文章