Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue with faye-websocket-node as client #229

Closed
quantjin opened this issue Apr 20, 2017 · 3 comments
Closed

issue with faye-websocket-node as client #229

quantjin opened this issue Apr 20, 2017 · 3 comments

Comments

@quantjin
Copy link

quantjin commented Apr 20, 2017

I'm trying to use faye-websocket-node ^0.11.0 as client, sockjs-node 0.3.18 as server, please refer to below client/server code

Issue:

  • if client and server prefix matchs, like below code prefix = /hello, client will throw Error during WebSocket handshake: Unexpected response code: 200

  • I tried using prefix = / on the server, this time there won't be any error message, but there's no connection open, no message printed from either client or server on open or connection code

Question:

How to use faye-websocket-node as client, sockjs-node as server, and have them communicate via websock using node.js?

Thanks!

client code:

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);
});

server code:

var sockjs = require('sockjs');
var http = require('http');

var sockjsServer = sockjs.createServer({
  sockjs_url: '//d1fxtkz8shb9d2.cloudfront.net/sockjs-0.3.min.js'
});

sockjsServer.on('connection', function(conn) {
  console.log('on connection');
  conn.on('data', function(msg) {
    console.log('\nRECV:'+msg);
  });
});

var server = http.createServer();
sockjsServer.installHandlers(server, {prefix:'/hello'});
server.listen(8888, '0.0.0.0');
@brycekahle
Copy link
Contributor

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.

@quantjin
Copy link
Author

@brycekahle , thank you for the update, I have to use faye-websocket-client, I'll think of something else

@brycekahle
Copy link
Contributor

@JerryYangJin The node.js version of sockjs-client uses faye-websocket-client underneath if that helps at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants