简体   繁体   English

如果不是localhost,则无法使浏览器连接到nodejs WS服务器

[英]Can't get browser to connect to nodejs WS server if not localhost

I've been struggling to figure out what I don't understand: I have a NodeJS websocket server running on a computer on my local network. 我一直在努力弄清楚我不理解的东西:我在本地网络上的计算机上运行了一个NodeJS websocket服务器。 From that computer's browser, I can communicate with the websocket server. 从该计算机的浏览器,我可以与websocket服务器进行通信。 However, from any other computer on the same network, I get a forced closeEvent with error code 1006. 但是,从同一网络上的任何其他计算机,我得到一个强制closeEvent,错误代码为1006。

Attached are both server and client files. 附件是服务器和客户端文件。 The client file is also being served from the same place. 客户端文件也在同一个地方提供。 Any help in broadening my understanding here would be most greatly appreciated. 任何有助于扩大我的理解的人都将非常感激。

Thanks! 谢谢!


ws_server.js ws_server.js

var fs = require('fs');
var path = require('path');
var httpServer = require('http').createServer(
    function(request, response) {
        if(request.url != ''){//request.url is the file being requested by the client
            var filePath = '.' + request.url;
            if (filePath == './'){filePath = './ws_client.html';} // Serve index.html if ./ was requested
            var filename = path.basename(filePath);
            var extname = path.extname(filePath);
            var contentType = 'text/html';
            fs.readFile(filePath, function(error, content) {
                response.writeHead(200, { 'Content-Type': contentType });
                response.end(content, 'utf-8');
            });
        }
    }
).listen(8080);

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({server:httpServer});
wss.on('connection', function(ws) {
    ws.on('message', function(message) {
        console.log('received: %s', message);
        ws.send("You said: "+ message);
    });
});

ws_client.html ws_client.html

<html>
<head>
    <title>WS</title>
    <script>
        var connection = new WebSocket('ws://127.0.0.1:8080');
        connection.onopen = function () {connection.send("The time is " + new Date().getTime());};
        connection.onmessage = function (e) {document.getElementById("capture").innerHTML = e.data;};
        connection.onclose = function(event) {console.log(event);};
    </script>
</head>
<body>

<textarea id="capture"></textarea>

</body>
</html>

Maybe it's just a typo in the code, but did you change the address in ws://127.0.0.1:8080 when attempting from the other computer ? 也许这只是代码中的拼写错误,但是当您从另一台计算机上尝试时,您是否更改了ws://127.0.0.1:8080的地址?

127.0.0.1 always refer to the local host, which is not what you want here. 127.0.0.1总是引用本地主机,这不是你想要的。

You should put the local address instead (something like 192.168.0.5 ), or better: new WebSocket('/'); 你应该改为使用本地地址(比如192.168.0.5 ),或者更好: new WebSocket('/'); , which will connect to the same host and port as the page. ,它将连接到与页面相同的主机和端口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM