Node.js 和 web-socket-js 简单客户端服务器未打开

发布于 2024-11-05 09:14:53 字数 1116 浏览 3 评论 0原文

我的 server.js 似乎是正确的。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "mysite.com");
console.log('Server running at mysite.com:1337/');

我的客户端的内容如下。

function init() {

  // Connect to Web Socket.
  // Change host/port here to your own Web Socket server.
  ws = new WebSocket("ws://mysite.com:1337");

  // Set event handlers.
  ws.onopen = function() {
    output("onopen");
  };

}

如果我访问 http://www.mysite.com:1337 我会正确收到 Hello World!但是,当我尝试使用客户端连接并在 Fire bug 中进行调试时,我得到以下输出。

[WebSocket] connected
[WebSocket] request header: GET / HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: mysite.com:1337 Origin: http://www.mysite.com Cookie: Sec-WebSocket-Key1: 115 17 p^!x-93 IERc16 7 Sec-WebSocket-Key2: 1 75 7Z i `. 8u $l031 4j9
[WebSocket] sent key3: ±Ôñ<g
[WebSocket] closed

在我有机会做任何事情之前,WebSocket 会自动关闭。谁能解释一下我收到的错误以及您认为我应该做什么?

My server.js seems to be correct..

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "mysite.com");
console.log('Server running at mysite.com:1337/');

The meat of my client is below.

function init() {

  // Connect to Web Socket.
  // Change host/port here to your own Web Socket server.
  ws = new WebSocket("ws://mysite.com:1337");

  // Set event handlers.
  ws.onopen = function() {
    output("onopen");
  };

}

If I go to http://www.mysite.com:1337 I correctly receive Hello World! However, when I try to connect using my client, and debug in Fire bug, I get the following output.

[WebSocket] connected
[WebSocket] request header: GET / HTTP/1.1 Upgrade: WebSocket Connection: Upgrade Host: mysite.com:1337 Origin: http://www.mysite.com Cookie: Sec-WebSocket-Key1: 115 17 p^!x-93 IERc16 7 Sec-WebSocket-Key2: 1 75 7Z i `. 8u $l031 4j9
[WebSocket] sent key3: ±Ôñ<g
[WebSocket] closed

And the WebSocket is automatically closed before I have any chance to do anything. Can anyone please shed some light on the error I am receiving and what do you think I should do?

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

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

发布评论

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

评论(1

渡你暖光 2024-11-12 09:14:53

您已经创建了一个 node.js HTTP 服务器,但您正在尝试将其作为 WebSockets 服务器连接。 WebSockets 协议不是普通的套接字,也不是普通的 HTTP 请求。 WebSocket 具有类似于 HTTP 的握手,但之后您将拥有一个全双工连接(类似于套接字,但与 HTTP 不同),该连接具有少量消息帧(与普通套接字不同)。

尝试使用 Socket.IOnode-websocket-server 如果你想创建一个node.js WebSockets服务器。另一方面,如果您想从 Javascript 连接到常规 HTTP 服务器,请使用具有 AJAX 支持的优秀 Javascript 库之一,例如 jQuery

You have created a node.js HTTP server but you are trying to connect to it as a WebSockets server. The WebSockets protocol is not plain sockets and it is not plain HTTP request. WebSockets have an HTTP like handshake but after that you have a full-duplex connection (like sockets and unlike HTTP) that has a small amount of message framing (unlike plain sockets).

Try using Socket.IO or node-websocket-server if you want to create a node.js WebSockets server. On the other hand if you are wanting to connect from Javascript to a regular HTTP server then use one of the great Javascript libraries with AJAX support such as jQuery.

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