2016-06-22 32 views
0

Node.jsスクリプトを実行すると、localhost:8083を使用してアクセスできますが、別のデバイスからのマシンのIPアドレスを使用すると「このサイトはできません達成される」。Node.js - Socket.ioはlocalhost経由でしかアクセスできない

<script src = "http://code.jquery.com/jquery-1.10.1.min.js" > </script> 
<script src="/socket.io/socket.io.js "></script> 
<script> 
    // Connecting to socket.io 
    var socket = io.connect("10.254.17.115:8083"); 

    // The username is requested, sent to the server and displayed in the title 
    var username = prompt('What\'s your username?'); 
    socket.emit('new_client', username); 
    document.title = username + ' - ' + document.title; 

    // When a message is received it's inserted in the page 
    socket.on('message', function(data) {insertMessage(data.username, data.message)}) 

    // When a new client connects, the information is displayed 
    socket.on('new_client', function(username) { 
    $('#chat_zone').prepend(
     '<p><audio class="background" autoplay><source src="new-user.mp3" type="audio/wav"></audio><em>' + username + ' has joined the chat!</em></p>'); 
    }) 

    // When the form is sent, the message is sent and displayed on the page 
    $('#chat_form').submit(function() { 
     var message = $('#message').val(); 
     socket.emit('message', message); // Sends the message to the others 
     insertMessage(username, message); // Also displays the message on our page 
     $('#message').val('').focus(); // Empties the chat form and puts the focus back on it 
     return false; // Blocks 'classic' sending of the form 
    }); 

    // Adds a message to the page 
    function insertMessage(username, message) { 
     $('#chat_zone').prepend('<p><strong>' + username + '</strong> ' + message + '</p>'); 
    } 
</script> 

編集:ここでは

var app = require('express')(), 
    server = require('http').createServer(app), 
    io = require('socket.io').listen(server), 
    ent = require('ent'), // Blocks HTML characters (security equivalent to htmlentities in PHP) 
    fs = require('fs'); 

// Loading the page index.html 
app.get('/', function (req, res) { 
    res.sendfile(__dirname + '/index.html'); 
}); 

io.sockets.on('connection', function (socket, username) { 
    // When the username is received it’s stored as a session variable and informs the other people 
    socket.on('new_client', function(username) { 
     username = ent.encode(username); 
     socket.username = username; 
     socket.broadcast.emit('new_client', username);(err) {}); 
    }); 

    // When a message is received, the client’s username is retrieved and sent to the other people 
    socket.on('message', function (message) { 
     message = ent.encode(message); 
     socket.broadcast.emit('message', {username: socket.username, message: message}); 
    }); 
}); 
console.log('Chat Socket.io running on port 8083'); 
server.listen(8083); 

クライアントスクリプトです:改訂ポートが、彼らは問題がなかった私は、次のNode.js サーバスクリプトを使用しています。 var socket = io.connectio()に変更しました。

+0

Socket.IOサーバを実行するマシンは、そのポートでの着信接続を許可していますか?ファイアウォールがこれらの要求をブロックしている可能性があります。 – arthurakay

+1

サーバが8083で動作している場合、クライアントスクリプトがポート8081に接続しようとしているのを待ちますか? – arthurakay

+0

''スペースを使用していますか? –

答えて

1

あなたはそれがファイルが提供されたサーバーに自動的に接続しようと、クライアントはsocket.io.jsを提供されている場合、クライアント側でconnectを呼び出す必要はありません、io();

編集を呼び出して:

var socket = io(); 

クライアントがsocket.io.jsを務めてきたならば、これはあなたが呼び出すために必要なすべてのです。この例では、socketがサーバーに接続されているソケットオブジェクトになります。

単純な例をthisで見てください。クライアント側の例では、サーバーに接続するのにはvar socket = io();しかないことがわかります。

1

これは、ノードではなくサーバー(および/またはネットワーク)のファイアウォールに問題がある可能性があります。サーバー上で新しいポートを使用する必要があるときはいつでも、そのポートへのトラフィックを許可するためにファイアウォールを開く必要があります。

開始するには、接続しようとしているクライアントにポートスキャンユーティリティ(nmapなど)をダウンロードすることをお勧めします。その後、ユーティリティをサーバーに向けて、開いているポートを確認します。

ポートの問題であることが確認できたら、マシンのファイアウォールでそのポートを開くように作業します。その後でもマシンに接続できない場合、ネットワークファイアウォールによってトラフィックがサーバーに到達しない可能性があります。

まだ理解できない場合は、ネットワーク専門家(私の専門分野ではない)からアドバイスを受けることをお勧めします。Network Engineeringに質問を投稿することを検討してください。また、上記のコードにいくつかのエラー処理を追加することを検討してください。ファイアウォールの問題ではない場合、問題の正確な内容を追跡するのに役立ちます。助け

希望、 〜ネイト

1

クライアントとサーバーのポートが(8081と8083のdont)と一致する必要があります:)

関連する問題