issue with faye-websocket-node as client
quantjin opened this issue · 3 comments
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 codeprefix = /hello
, client will throwError 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 serveron 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');
You cannot use faye-websocket
directly as SockJS utilizes additional framing. You can do one of two things:
- Use the "raw" endpoint as documented in the README: https://github.com/sockjs/sockjs-node#connecting-to-sockjs-node-without-the-client
- Use
sockjs-client
from Node.js! Client versions >= 1.0 support usage directly from Node.
@brycekahle , thank you for the update, I have to use faye-websocket-client
, I'll think of something else
@JerryYangJin The node.js version of sockjs-client
uses faye-websocket-client
underneath if that helps at all.