1
var WebSocketServer = require('D:/0.ProgramFiles/2.Devel/4.NodeJs/node_modules/ws').Server;
var wss = new WebSocketServer({port: 8888});
var userList = [];

wss.on('connection', function (ws,req) {
    console.log("连接",req.connection.remoteAddress);
    // console.log(getUUID());

    ws.on('message', function (message) {
        console.log("收到消息:" + message);
        wss.clients.forEach(function each(client) {
            client.send(message);
        });
    });

    ws.on('close', function (message) {
        console.log("断开",req.connection.remoteAddress);
    });

});

如上代码,自己电脑上浏览器访问正常,同一局域网其它ip访问页面只显示“upgrade required”

请教大家该怎么解决呢?谢谢

(邀请了几位大佬,邀请完我才想到可能会打扰到大家,如有打扰,真的很抱歉~~~)

1个回答

1

https://stackoverflow.com/que...
你需要创建一个http server

var express = require('express')
var expressWs = require('express-ws')

var app = express()
expressWs(app)

app.ws('/echo', (ws, req) => {

    ws.on('connection', function (connection) {
        //...
    })

    ws.on('close', function () {
        //...
    })
})

app.use(express.static('public'))
app.listen(3000, function () {
    console.log('Example app listening on port 3000!')
})

撰写答案