WebSocket 只接收“断开连接”消息

发布于 2024-12-06 15:21:01 字数 683 浏览 0 评论 0原文

我正在 Google Chrome 上尝试这个简单的 websocket 示例:

var wsUri = "ws://echo.websocket.org/"; 
var output;  

function init() { 
  output = document.getElementById("output"); 
  testWebSocket(); 
}  

function testWebSocket() { 
  websocket = new WebSocket(wsUri);

 websocket.onopen = function(evt) { 
   onOpen(evt) 
 }; 

 ..............
 ..............

 function onOpen(evt) { 
   writeToScreen("CONNECTED"); 
   doSend("WebSocket rocks"); 
 }  

 function onClose(evt) {
    writeToScreen("DISCONNECTED"); 
 }  

 window.addEventListener("load", init, false);  

但我总是只收到 DISCONNECT!

有什么问题吗?

我是否必须在本地 Apache 中启用 WebSockets 协议?如果是的话怎么办?

I'm trying this simple websocket example on Google Chrome:

var wsUri = "ws://echo.websocket.org/"; 
var output;  

function init() { 
  output = document.getElementById("output"); 
  testWebSocket(); 
}  

function testWebSocket() { 
  websocket = new WebSocket(wsUri);

 websocket.onopen = function(evt) { 
   onOpen(evt) 
 }; 

 ..............
 ..............

 function onOpen(evt) { 
   writeToScreen("CONNECTED"); 
   doSend("WebSocket rocks"); 
 }  

 function onClose(evt) {
    writeToScreen("DISCONNECTED"); 
 }  

 window.addEventListener("load", init, false);  

But i always receive only DISCONNECT!

There is something wrong?

Do I have to enable WebSockets protocol in my local Apache? If yes how to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

じее 2024-12-13 15:21:01

该服务器不可靠。它甚至在 Chrome 14 的自己的演示页面上失败。Chrome

14 的 WebSockets 请求的响应是这显然是不正确的:

HTTP/1.1 200 OK
Server: Kaazing Gateway
Date: Tue, 27 Sep 2011 14:07:53 GMT
Content-Length: 0

请注意,Chrome 刚刚切换到 WebSockets 协议的新草案,这是一次彻底的改革。这意味着服务器必须返回不同的握手响应,并且还必须对发送的消息进行解码,而之前的草案并非如此。可能只是他们还没有升级服务器。

您可能想要的是设置自己的符合新草案的服务器并在该服务器上进行测试。

到处都有很多用于 WebSockets 服务器的库;您可以查看此处并选择您选择的服务器语言。

This server is not reliable. It even fails on their own demo page for Chrome 14.

The response for a WebSockets request of Chrome 14 is this, which is obviously not correct:

HTTP/1.1 200 OK
Server: Kaazing Gateway
Date: Tue, 27 Sep 2011 14:07:53 GMT
Content-Length: 0

Note that Chrome just switched to a new draft of the WebSockets protocol, which is a complete overhaul. This means that the server has to return a different handshake response and also has to decode messages that are sent, which was not the case with the previous draft. It might just be that they did not upgrade their server yet.

What you probably want is setting up your own server which is compliant with the new draft and test it on that server.

There are a lot of libraries for WebSockets servers popping up everywhere; you can have a look here and pick the server language of your choice.

风追烟花雨 2024-12-13 15:21:01

您需要指定 websocket 是一个变量。将此行:更改

websocket = new WebSocket(wsUri); 

为:

var websocket = new WebSocket(wsUri);

希望有帮助。这为我解决了一些问题。

You need to specify that websocket is a variable. Change this line:

websocket = new WebSocket(wsUri); 

to this:

var websocket = new WebSocket(wsUri);

Hope it helps. This solved some problems for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文