websocket-Demo

燕建中
2023-12-01

server.js

var ws = require('nodejs-websocket');
var count = 1;
var server = ws.createServer(function(socket) { 
    socket.on('text', function(str) {   
        var data = JSON.parse(str);
        if (data) {
            server.connections.forEach(function(item) {
                var info = JSON.parse(item.frameBuffer);
                if (info) {
                    console.log("groupId-----", item["groupId"])
                    if ((info.group == data.group)) {
                        item["groupId"] = data.group
                        item.sendText(data.message);
                    }
                } else {
                    // console.log(item)
                }
            })
        }
    });
}).listen(3000);

html

<script>
    // 打开一个 web socket  这里端口号和上面监听的需一致
    var ws = new WebSocket('
 类似资料: