node.js - 将faye-websocket-node作为客户端和sockjs-node作为服务器发出问题

时间:2017-04-21 01:39:23

标签: node.js sockjs

我尝试使用faye-websocket-node ^0.11.0作为客户端,sockjs-node 0.3.18作为服务器,请参阅下面的客户端/服务器代码

问题:

  • 如果客户端和服务器prefix匹配,如下面的代码prefix = /hello,客户端将抛出Error during WebSocket handshake: Unexpected response code: 200

  • 我尝试在服务器上使用prefix = /,这次没有任何错误消息,但没有打开任何连接,也没有从客户端或服务器打印消息{ {1}}或on open代码

问题:

如何使用on connection作为客户端,faye-websocket-node作为服务器,并让他们使用node.js通过websock进行通信?

谢谢!

客户端代码

sockjs-node

服务器代码

var WebSocket = require('faye-websocket'),
    ws        = new WebSocket.Client('ws://127.0.0.1:8888/hello');

ws.on('open', function(event) {
  console.log('open');
  ws.send('Hello, world!');
});

ws.on('message', function(event) {
  console.log('message', event.data);
});

ws.on('close', function(event) {
  console.log('close', event.code, event.reason);
  ws = null;
});

ws.on('error', function(event){
  console.log('error', event.message);
});

1 个答案:

答案 0 :(得分:0)

我提出了机票#229,从sockjs-node回答:

You cannot use faye-websocket directly as SockJS utilizes additional framing. You can do one of two things:

1. Use the "raw" endpoint as documented in the README: https://github.com/sockjs/sockjs-node#connecting-to-sockjs-node-without-the-client
2. Use sockjs-client from Node.js! Client versions >= 1.0 support usage directly from Node.