[英]Node.js - Socket.io only accessible through localhost
當我運行Node.js腳本時,只能使用localhost:8083來訪問它,而從另一台設備使用機器的IP地址會導致“無法訪問此站點”。 我正在使用以下Node.js 服務器腳本 :
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);
這是客戶端腳本 :
<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 socket = io.connect
為io()
它起作用了。
這可能是服務器(和/或網絡)防火牆的問題,而不是節點的問題。 任何時候需要使用服務器上的新端口時,都需要打開所有防火牆以允許流量通過該端口。
一個不錯的開始是將端口掃描實用程序(例如nmap )下載到嘗試連接的客戶端上。 然后將實用程序指向服務器,以查看打開了哪些端口。
確認是端口問題后,請在計算機的防火牆上打開該端口。 此后,如果仍然無法連接到計算機,則網絡防火牆可能會阻止流量到達服務器。
如果您仍然無法解決問題,則需要從網絡專家(而不是我的專業領域)那里獲得一些建議,因此請考慮在Network Engineering下發布問題。 另外,請考慮在上述代碼中添加一些錯誤處理; 如果不是防火牆問題,它可能會幫助您找出問題的根源。
希望能有所幫助,〜Nate
您無需在客戶端上調用connect
,當為客戶端提供socket.io.js
,它將通過調用io();
嘗試自動連接到為其提供文件的服務器io();
編輯:
var socket = io();
為客戶端提供socket.io.js后,這就是您需要調用的所有內容。 在此示例中, socket
將是連接到服務器的套接字對象。
看一下這個簡單的例子。 您可以在他們的客戶端示例中看到,他們僅執行var socket = io();
連接回服務器。
客戶端和服務器端口應匹配(8081和8083不匹配):)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.