美文网首页
node 实现WebSocket

node 实现WebSocket

作者: 会飞的猪l | 来源:发表于2018-09-13 09:49 被阅读27次
WebSocket是什么?

WebSocket是一种网络通信协议,RFC6455定义了它的通信标准。
WebSocket是HTML5可以提供的一种在单个TCP连接上进行全双工通讯的协议。

服务器代码
var express = require('express');
var http = require('http');
var WebSocket = require('ws');

var app = express();
app.use(express.static(__dirname));

var server = http.createServer(app);
var wss = new WebSocket.Server({server});

wss.on('connection', function connection(ws) {
    console.log('开始连接!');
    ws.on('message', function incoming(data) {
         console.log('接收到了消息!');
        /**
         * 把消息发送到所有的客户端
         * wss.clients获取所有链接的客户端
         */
        wss.clients.forEach(function each(client) {
            client.send(data);
        });
    });
});

server.listen(8000, function listening() {
    console.log('服务器启动成功!');
});
客户端代码
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
 content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>在线聊天</title>
</head>
<body>
<input type="text" onblur="wsServer.onopen(this.value)">
<script>
 var wsServer = new WebSocket('ws://127.0.0.1:8000');
 wsServer.onopen = function (e) {
     (typeof e == 'string') && wsServer.send(e);//向后台发送数据
 };
 wsServer.onclose = function (e) {//当链接关闭的时候触发

 };
 wsServer.onmessage = function (e) {//后台返回消息的时候触发
        console.log(e);
 };
 wsServer.onerror = function (e) {//错误情况触发

 }
</script>
</body>
</html>
image.png

相关文章

网友评论

      本文标题:node 实现WebSocket

      本文链接:https://www.haomeiwen.com/subject/dcoigftx.html