rctfan1999 rctfan1999 - 5 months ago 25
Node.js Question

Node.js - Socket.io only accessible through localhost

When I run my Node.js script, I can only access it by using localhost:8083, while using the machine's IP address from another device results in a "This site cannot be reached". I am using the following Node.js server 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);


Here is the client script:

<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>

pay pay
Answer

You don't need to call connect on the client side, when the client is served socket.io.js, it will attempt to connect automatically to the server from which the file was served, with a call to io();

Edit:

var socket = io();

Once the client has been served the socket.io.js, this is all you need to call. socket will be your socket object that is connected to the server, in this example.

Take a look at this simple example. You can see that in their client side examples, they only do var socket = io(); to connect back to the server.