2016-11-23 44 views
0

我有一個節點WebServer能夠通過Websockets與瀏覽器(比如browserInstance)和linux終端(比如ProxyInstance)通信。 Web服務器的工作是將數據從終端切換到WebBrowser,然後是副本。請看以下server.js代碼:節點服務器不能與express-ws工作

var express = require('express'); 
var expressWs = require('express-ws'); 


var expressWs = expressWs(express()); 
var app = expressWs.app; 
var appForpage = express(); 


var browserInstance; 
var ProxyInstance; 
var browserCounter = 0; 
var ProxyCounter = 0; 


app.ws('/fromBrowser', function(ws, req, next) { 

    console.log("~~~~~~~~~~~~BROWSER"); 

    if(browserCounter == 1){ 
      ws.on('message', function(msg) { 
        console.log("Messagae from Browser :", msg); 
        ProxyInstance.send(msg); 
      }); 
    }else{ 
      browserInstance = ws; 
      ws.on('message', function(msg) { 
        console.log("Message from Browser :", msg); 
        ProxyInstance.send(msg); 
      }); 

      browserCounter = 1; 
    } 


    ws.on('close', function(){ 
      console.log("Ws Connection closed"); 
    }); 

    //next(); 
}); 

app.ws('/fromProxy', function(ws, req, next) { 
    console.log("~~~~~~~~~~~~PROXY"); 

    if(ProxyCounter == 0){ 
      ProxyInstance = ws; 
      ProxyCounter = 1; 
    }else if(browserCounter == 1){ 
      ws.on('message', function(msg) { 
        console.log("Message from Proxy: ", msg); 
        browserInstance.send(msg); 
      }); 
    } 

    ws.on('close', function(){ 
      console.log("Ws Connection closed"); 
    }); 
    //next(); 
}); 

appForpage.use(express.static(__dirname + '/public/')); // index.html resides in public directory 

appForpage.listen(5000) 
app.listen(3000) 

首先我創建從代理服務器WS連接(/ fromProxy),然後從瀏覽器(/ fromBrowser)。連接已成功創建。當我嘗試通過網絡服務器將數據從瀏覽器發送到代理時,它工作正常。在嘗試通過WebServer與瀏覽器進行通信時,作爲代理服務器端的第一條消息返回,此失敗。我還沒有收到代理的任何消息。我需要以相同的順序運行相應的代碼(代理優先,然後瀏覽器..)。

我只是初學者的節點。我沒有找到任何通過互聯網爲我的例子的例子。我在這裏錯過了什麼?

回答

0

這是非常愚蠢的。我:(沒有註冊代理,從我的訊息話題回調。請看以下工作代碼。

var express = require('express'); 
var expressWs = require('express-ws'); 


var expressWs = expressWs(express()); 
var app = expressWs.app; 
var appForpage = express(); 


var browserInstance; 
var ProxyInstance; 
var browserCounter = 0; 
var ProxyCounter = 0; 


app.ws('/fromBrowser', function(ws, req, next) { 

    console.log("~~~~~~~~~~~~BROWSER"); 

    if(browserCounter == 1){ 
      ws.on('message', function(msg) { 
        console.log("Messagae from Browser :", msg); 
        ProxyInstance.send(msg); 
      }); 
    }else{ 
      browserInstance = ws; 
      ws.on('message', function(msg) { 
        console.log("Message from Browser :", msg); 
        ProxyInstance.send(msg); 
      }); 

      browserCounter = 1; 
    } 


    ws.on('close', function(){ 
      console.log("Ws Connection closed"); 
    }); 

    //next(); 
}); 

app.ws('/fromProxy', function(ws, req, next) { 
    console.log("~~~~~~~~~~~~PROXY"); 

    if(ProxyCounter == 0){ 
      ProxyInstance = ws; 
      ProxyCounter = 1; 
    } 

/*This is the place where i went wrong. damnn..*/ 
    ws.on('message', function(msg) { 
      console.log("Message from Proxy: ", msg); 
      browserInstance.send(msg); 
    }); 

    ws.on('close', function(){ 
      console.log("Ws Connection closed"); 
    }); 
    //next(); 
}); 

appForpage.use(express.static(__dirname + '/public/')); // index.html resides in public directory 

appForpage.listen(5000) 
app.listen(3000) 
相關問題