码迷,mamicode.com
首页 > Web开发 > 详细

websocket服务器

时间:2019-05-28 12:58:50      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:浏览器   网络协议   UNC   opcode   websocket   sed   hand   nis   OLE   

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服务器

标签:浏览器   网络协议   UNC   opcode   websocket   sed   hand   nis   OLE   

原文地址:https://www.cnblogs.com/hanmengya/p/10936317.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!