2017-11-26 6 views
0

私はUbuntu 14.04 LTSマシンに単純なnodejsを持っています。 クライアントがlocalhost:3000にいる場合でも、nginxまたはpassengerをws:// localhost:8443/helloworldへの呼び出しをlistenするように設定するにはどうすればよいですか?NGINX listen websocketクライアントの電話

server.js次のようになります。これは私のなど/ nginxの/サイトが有効である

<!-- ======================================= --> 
<!-- ==========  CSS  =========== --> 
<!-- ======================================= --> 
    <style> 
    p { line-height:18px; } 
    div { width:500px; margin-left:auto; margin-right:auto;} 
    #content { padding:5px; background:#ddd; border-radius:5px; 
       overflow-y: scroll; border:1px solid #CCC;margin-top:10px; height: 160px; } 
    #input_hello { border-radius:2px; border:1px solid #ccc; margin:10px; padding:5px; width:50%; float:left;} 
    #status { width:88px;display:block;float:left;margin-top:15px; } 
    </style> 


<!-- ======================================= --> 
<!-- ========== JS FOR WEBSOCKET =========== --> 
<!-- ======================================= --> 
<script type="text/javascript"> 
    $(document).ready(function() { 
    //-- ======================================= --// 

      // ======================== // 
      // ====== CONNECTION ====== // 
      // ======================== // 

      var content = $('#content'); 
      var message = $('#input_hello input[type="text"]').val(); 
      var ws = new WebSocket('ws://localhost:8443/helloworld'); 

      ws.onopen = function() { 
       alert("YOU ARE CONNECTED WITH WSS!!!!"); 
      }; 

      // ======================== // 
      // ====== ON KEY PRESS ==== // 
      // ======================== // 

      $("#input_hello").keydown(function(event) { 
       // ======= IF IS ENTER ===== // 
       if (event.keyCode == 13) { 
        ws.send(message); 
        return false; 
       } 
       // ======================== // 
      }); 

      // ======================== // 
      // ====== WS MESSAGE ====== // 
      // ======================== // 
      ws.onmessage = function(msg) { 
       alert("RECEIVED MESSAGE WITH WSS!!!!"); 
       addMessage(msg.data); 
      } 

      // ======================== // 
      // ==== RENDER MESSAGE ==== // 
      // ======================== // 
      function addMessage(message) { 
       content.prepend('<p><span>' + message + '</span></p>'); 
       $("#input_hello").val(""); 
      } 
    //-- ======================================= --// 
    }); 
</script> 





<!-- ======================================== --> 
<!-- ======================================== --> 
<!-- ========   HTML   ======= --> 
<!-- ======================================== --> 
<!-- ======================================== --> 
<h1>HELLO FROM EXPRESS</h1> 
<div id="content"></div> 
<div> 
    <input type="text" id="input_hello" /> 
</div> 
<!-- ======================================== --> 

:私は私の静的/ index.htmlをファイルで

var path = require('path'); 
var ws = require('ws'); 
var express = require('express'); 
var minimist = require('minimist'); 
var url = require('url'); 
var fs = require('fs'); 
var http = require('http'); 
var app = express(); 

app.use(express.static(path.join(__dirname, 'static'))); 

/* ======================================== */ 
/* =======       ======== */ 
/* ======= WEBSOCKETS SERVER ======== */ 
/* =======       ======== */ 
/* ======================================== */ 

var asUrl = url.parse("http://localhost:8443/"); 
var port = asUrl.port; 
var server = http.createServer(app).listen(port, function() { 
    console.log('********** WS HTTP SERVER STARTED********** '); 
}); 

var wss = new ws.Server({ 
    server : server, 
    path : '/helloworld' 
}); 

/* ======================================== */ 
/* ========= WSS ON CONNETION ======== */ 
/* ======================================== */ 
wss.on('connection', function(ws) { 

    console.log('**** CONNECTION RECEIVED =D'); 
    ws.send(JSON.stringify("WELCOME FROM SERVER =D")); 

    ws.on('message', function incoming(data) { 
     console.log('********** RECEIVED MSG '+data); 
     ws.send(JSON.stringify(data)); 
    }); 
}); 
/* ======================================== */ 

/デフォルトファイル:

#===================# 
#==== SERVER::80 ===# 
#===================# 
server { 
     listen 80 default_server; 
     listen [::]:80 default_server; 
     #========================# 
     #==== FOR ROOT =====# 
     #========================# 
      root /home/deploy/Desktop/NODE_APP/static; 
     #========================# 
     #==== FOR PASSENGER =====# 
     #========================# 
      passenger_enabled on; 
      passenger_app_env development; 
      passenger_app_type node; 
      passenger_app_root /home/deploy/Desktop/NODE_APP; 
      passenger_startup_file /home/deploy/Desktop/NODE_APP/server.js; 
     location/{ 
       try_files $uri $uri/ =404; 
     } 
} 

#===================# 
#=== SERVER 8443 ===# 
#===================# 
server { 
     listen 8443; 
     listen [::]:8443; 

     #========================# 
     #==== FOR ROOT =====# 
     #========================# 
      root /nowhere; #========== NO NEED STATIC FILES..(ONLY WS!) 
     #========================# 
     #==== FOR WEBSOCKETS ====# 
     #========================# 
     location/{ 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Host $http_host; 
      proxy_redirect off; 
      proxy_pass http://localhost:8443/helloworld; 
     } 
} 

#=======================================# 
#=======================================# 

node /home/deploy/Desktop/NODE_APP/server.jsを実行すると、localhost:3000またはlocalhost:8443にアクセスし、ws:// localhost:8443/helloworldのWebSocketによって8443で実行されているnode.jsサーバーに接続できます。

しかし、私はsudo service nginx startを実行したとき、私はもう交換することはできませんwsが8443上で動作しているのNode.jsサーバーとのWebSocket:// localhostを:私はへの呼び出しを聞くためにnginxのか乗客を設定することができますどのように8443/helloworldに

クライアントがlocalhost:3000にいる場合でも、 ws:// localhost:8443/helloworld?

+0

は、あなたの明示アプリは唯一のWebSocket上の要求にサービスを提供するか、それはまた、いくつかの取扱いですポート80のサーバー?これは少し単純化することができるように見えますが、私はチェックしたかったのです。 –

+0

答えは@CamdenNarztありがとうございます。誰かが見ることができるように私は書くつもりです:私はポート3000で走っている1つのメインサーバーを持っています。そして、このEXPRESS SERVERをスタティックファイルとして使用して、ポート8443(HTTPとして)で走り、ここでWS接続を作成しますON 8443/helloworld。 –

+0

NGINXをどのように設定すれば、この2番目のサーバーをポート8443/helloworldで聞くことができますか?WS DATAにのみ、レンダリングするのはHTMLページなどではありません。メインサーバービューを保持します。 –

答えて

0

あなたのアプリとnginxを同じポートでリッスンすることはできません。別のポートでwebsocket接続をリッスンし、nginxを8443からそのポートに転送するには、アプリケーションが必要です。

また、あなたのアプリで複数のポートを使用している場合、あなたは1を「主」1である旅客を伝える必要があります:https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html#caveat-multiple-http-server-objects-error-http-server-listen-was-called-more-than-once

+0

パーフェクト!大変ありがとう@CamdenNarzt !!あなたは共有することを念頭に置いていますか? –

関連する問題