node创建websocket服务端

//引入ws模块
var WebSocket = require('ws');
//创建websocket
var wss = new WebSocket.Server({
    port: 12345
})
//声明客户端客户ID默认为0
var 客户ID = 0;
//服务端连接
wss.on('connection', (ws)=> {
    //每连接一个用户,客户端ID+1
    客户ID++;
    //为客户端添加自定义属性:客户ID
    ws.客户ID = 客户ID;
    //收到了数据
    ws.on('message', function(message) {
        let 收到的数据 = ArrayBufferUTF8ToStr(message);
    });
 
    //每隔固定的时间 服务器给全部客户端发送ping消息
    let 定时ping = setInterval(() => {
        wss.clients.forEach(当前客户 => {
            当前客户.ping("ping",false,()=>{
                console.log("已发送ping")
            })
        });
    }, 10000);
 
    ws.on('close', ()=> {
        clearInterval(定时ping);//取消当前客户的定时ping
    });

});

//编码转换
const ArrayBufferUTF8ToStr = (array)=> {
    var out,i,len,c;
    var char2,char3;
    if (array instanceof ArrayBuffer) {
        array = new Uint8Array(array);
    }
    out = "";
    len = array.length;
    i = 0;
    while(i < len) {
        c = array[i++];
        switch(c >> 4) {
            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                // 0xxxxxxx
                out += String.fromCharCode(c);
                break;
            case 12: case 13:
                // 110x xxxx   10xx xxxx
                char2 = array[i++];
                out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                break;
            case 14:
                // 1110 xxxx  10xx xxxx  10xx xxxx
                char2 = array[i++];
                char3 = array[i++];
                out += String.fromCharCode(((c & 0x0F) << 12) |
                    ((char2 & 0x3F) << 6) |
                    ((char3 & 0x3F) << 0));
                break;
        }
    }
    return out;
}

参考api:使用ws模块实现Node版WebSocket服务器 - 知乎

你可能感兴趣的:(websocket,javascript,网络协议)