简体   繁体   English

WebSocket 无法通过 Internet 工作

[英]WebSocket not working over the Internet

I currently have a WebSocket server (C#) and a client (JavaScript / HTML5).我目前有一个WebSocket服务器(C#)和一个客户端(JavaScript/HTML5)。 This works great on my local network, however when I listen to and make the client connect to my 'real' IP (which is the IP of my router identifiable worldwide, ie instead of 192.168.xx ), it doesn't work.这在我的本地网络上效果很好,但是当我收听并使客户端连接到我的“真实”IP(这是我的路由器在全球范围内可识别的 IP,即而不是192.168.xx )时,它不起作用。 The WebSocket class does get a readyState of 1 , but when I send anything from the client to the server (or the other way round), the message doesn't arrive. WebSocket 类的readyState1 ,但是当我从客户端向服务器发送任何内容时(或相反),消息没有到达。

I already set my router to enable port forwarding, otherwise it wouldn't work anyway (I tried not enabling this and the WebSocket immediately got a readyState of 2 ).我已经将我的路由器设置为启用端口转发,否则无论如何它都无法工作(我尝试不启用此功能, WebSocket立即获得了2readyState )。

JavaScript code: JavaScript 代码:

var ws = new WebSocket("ws://some.real.ip.address:myport");
// For privacy reasons I'd like to keep the address and port private

C# code (I'm using the Nugget library to implement a socket server): C# 代码(我使用 Nugget 库来实现套接字服务器):

var nugget = new WebSocketServer(myport, "*", "ws://some.real.ip.address:myport");

Obviously, the address and port are the same.显然,地址和端口是相同的。

What could be the cause of it?可能是什么原因造成的? Is it possible that my ISP doesn't allow socket connections when my computer is the server?当我的计算机是服务器时,我的 ISP 是否可能不允许套接字连接? If so, how come the WebSocket does get a readyState of 1 ?如果是这样,为什么WebSocketreadyState1

Thanks a lot.非常感谢。

这是我的错 - 不确定这个答案是否对任何人有帮助,但我正在专门听/bke ,而且我没有在 JavaScript WebSocket构造函数中传递它......

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

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