Reputation: 3871
I installed nodejs and socket.io, and when I run the demo code at http://socket.io/#how-to-use, I always get this on server:
info - socket.io started
debug - served static content /socket.io.js
On my browser, nothing output. It seems not working well. I use Chrome 16 beta.
I have a nginx installed on my server, too. So I changed the socket.io listening port to 8001.
Server code:
var app = require('express').createServer()
, io = require('socket.io').listen(app);
app.listen(8001);
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
Client code:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:8001');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
Upvotes: 2
Views: 4307
Reputation: 27323
Code works for me, can you try this in your client code:
var socket = io.connect(document.location.href);
to make sure that the URL is 100% accurate.
Upvotes: 4