i downloaded this very simple socket.io example from: https://github.com/shapeshed/nodejsbook.io.examples/tree/master/hour12/example02
package.json:
{ "name" : "socketio_example"
, "version" : "0.0.1"
, "private" : "true"
, "dependencies" : { "socket.io" : "0.8.7" }
}
app.js:
var http = require('http') ;
var fs = require('fs') ;
var count = 0;
var server = http.createServer(function (req, res) {
fs.readFile('./index.html' , function(error, data) {
res.writeHead(200, { 'Content-Type' : 'text/html'});
res.end(data, 'utf-8');
});
}).listen(3000, "1xx.2xx.1xx.26");
console.log('Server is running');
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket) {
count++;
console.log('User connected; ' + count + ' user(s) present.' );
socket.emit ('users' , { number : count }) ;
socket.broadcast.emit ('users' , { number : count }) ;
socket.on('disconnect', function() {
count--;
console.log('User disconnected; ' + count + ' user(s) present.' );
socket.broadcast.emit('users' , { number : count }) ;
});
});
index.html:
<!DOCTIME html>
<html lang='en'>
<head>
<title>Socket.IO Example</title>
</head>
<body>
<h1>Socket.IO Example</h1>
<p id='count'></p>
<script src="https://stackoverflow.com/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://1xx.2xx.1xx.26:3000') ;
var count = document.getElementById('count');
socket.on('users', function(data) {
console.log('Got update from the server!');
console.log('There are ' + data.number + ' users!');
count.innerHTML = data.number;
});
<script>
</body>
<html>
and then did:
node install ;
and finally:
node app.js &
then when i tried this using localhost (127.0.0.1), i can see my html code by doing:
curl http://127.0.0.1:3000 ;
then i changed the IP number from 127.0.0.1 to my own. and restarted the app. this command:
curl http://1xx.2xx.1xx.26:3000 ;
once again shows me the html code.
this project is supposed to display a count of the number of connections, but i cannot seem to get it working properly. however, i am not getting any errors either. the webpage is coming up when i browse to http://1xx.2xx.1xx.26:3000/ and the title appears but nothing else, no user count.
when a webpage connects i do this this message on the server:
debug - served static content /socket.io.js
any suggestions or thoughts what i might be doing wrong?
thank you all!