2017-04-21 6 views
1

として、クライアントとsockjsノードとしてフェイ-のWebSocket-ノードとの問題私は、クライアントとサーバとしてsockjs-node 0.3.18faye-websocket-node ^0.11.0を使用しようとしているが、クライアント/サーバ・コードの下のNode.js - サーバー

問題を参照してください。 :

  • クライアントとサーバprefix matchs場合、コードprefix = /hello以下のように、クライアントは、私がトライError during WebSocket handshake: Unexpected response code: 200

  • がスローされますDサーバー上prefix = /を使用して、この時間は、すべてのエラーメッセージが表示されませんが、何も接続開いて、クライアントまたはサーバーon openまたはon connectionコードから印刷していないメッセージ

質問ありません:

は、

クライアントとしてfaye-websocket-nodeを使用する方法、サーバとしてsockjs-nodeを使用し、node.jsを使用してwebsock経由で通信する方法

ありがとうございます!

クライアントコード

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

サーバーコード

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

答えて

0

私はsockjs-nodeからチケット#229、答えを提起:

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. 
関連する問題