2013-12-23 88 views
0

有誰知道如何創建sockjs-node(SockJS - WebSocket仿真)或Shoe獨立服務器?如何創建sockjs(SockJS - WebSocket仿真)或Shoe獨立服務器(node.js)?

是否有可能沒有http服務器打包?

只需要與dnode交換數據,並且不需要靜態頁面。

舉例來說,當我做節點服務器端:

var shoe = require('shoe'); 
var dnode = require('dnode'); 
var http = require('http'); 


var server = http 
    .createServer() 
    .listen(9999); 

var echo = shoe 
    .createServer() 
    .on('connection', function(c) 
    { 
     var d = dnode(
     { 
      test: function() 
      { 
       console.log('--------'); 
      } 
     }); 

     c 
      .on('data', function(message) 
      { 
       c.write(message); 
      }) 
      .on('close', function() {}); 

     c 
      .pipe(d) 
      .pipe(c); 

     c.on('close', function() {}); 
    }) 
    .installHandlers(server, 
    { 
     prefix: '/dnode' 
    }); 

下面的代碼(客戶端)

var d = dnode() 
    .on('remote', function(remote) 
    { 
     remote.test(); 
    }); 

d 
    .pipe(shoe('http://localhost/dnode')) 
    .pipe(d); 

,出現以下錯誤失敗:(鉻)

GET http://localhost/dnode/info app.bundle.js:14681 
AbstractXHRObject._start app.bundle.js:14681 
(anonymous function) 

回答

0

找不到SockJS或Shoe的方法,所以我換了websocket-stream

這裏有一個工作代碼:

節點服務器

'use strict'; 

var ws = require('ws'); 
var websocketStream = require('websocket-stream'); 
var dnode = require('dnode'); 
var http = require('http'); 

var HTTPserver = http 
    .createServer() 
    .listen(9999); 

var WSserver = new ws.Server(
{ 
    server: HTTPserver 
}); 

WSserver 
    .on('connection', function(s) 
    { 
     var c = websocketStream(s); 
     var d = dnode(
     { 
      test: function(f) 
      { 
       console.log('--------'); 
       f('hello'); 
      } 
     }); 

     c 
      .pipe(d) 
      .pipe(c); 
    }); 

瀏覽器客戶端

'use strict'; 

var websocketStream = require('websocket-stream'); 
var dnode = require('dnode'); 

var c = websocketStream('ws://localhost:9999'); 
var d = dnode() 
    .on('remote', function(remote) 
    { 
     remote.test(function(msg) 
     { 
      alert(msg); 
     }); 
    }); 

c 
    .pipe(d) 
    .pipe(c); 
相關問題