WebSocket 无法在 Internet 上工作
我目前有一个 WebSocket
服务器(C#)和一个客户端(JavaScript / HTML5)。这在我的本地网络上效果很好,但是当我监听并使客户端连接到我的“真实”IP(这是我的路由器在全球范围内可识别的IP,即而不是192.168.xx
)时,它不起作用。 WebSocket 类确实获得了 readyState
为 1
,但是当我将任何内容从客户端发送到服务器(或相反)时,消息不会到达。
我已经将路由器设置为启用端口转发,否则它无论如何都无法工作(我尝试不启用此功能,并且 WebSocket
立即获得 readyState
为 2< /代码>)。
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# 代码(我正在使用 Nugget 库来实现套接字服务器):
var nugget = new WebSocketServer(myport, "*", "ws://some.real.ip.address:myport");
显然,地址和端口是相同的。
可能是什么原因造成的?当我的计算机作为服务器时,我的 ISP 是否可能不允许套接字连接?如果是这样,WebSocket
为何获得 readyState
为 1
?
多谢。
I currently have a WebSocket
server (C#) and a client (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, i.e. instead of 192.168.x.x
), it doesn't work. 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.
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
).
JavaScript code:
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):
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? If so, how come the WebSocket
does get a readyState
of 1
?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我的错 - 不确定这个答案是否会对任何人有帮助,但我正在专门监听
/bke
,并且我没有在 JavaScriptWebSocket
构造函数中传递它。 ..It was my fault - not sure if this answer will help anybody, but I'm listening to
/bke
specifically, and I did not pass this in the JavaScriptWebSocket
constructor...