I am trying to get Socket.io to work. My code is below. I get the "Welcome to Socket.io." when I visit
http://localhost/index.html
but that is all I get. app.js looks like it will send back a message but I get nothing in my Javascript console and nothing on the page except "Welcome to Socket.io.". I tried visiting
http://localhost/chat
and
http://localhost/news
and it is the same results. I want to test a two way communication using Socket.io. Will this script do that for me and is a firewall possibly blocking the return message. This is an example for Socket.ip V0.9 page.
// app.js
var io = require('socket.io').listen(80);
var chat = io
.of('/chat')
.on('connection', function (socket) {
socket.emit('a message', {
that: 'only',
'/chat': 'will get'
});
chat.emit('a message', {
everyone: 'in'
, '/chat': 'will get'
});
});
var news = io
.of('/news')
.on('connection', function (socket) {
socket.emit('item', { news: 'item' });
});
The index.html
//index.html
<script>
var chat = io.connect('http://localhost/chat')
, news = io.connect('http://localhost/news');
chat.on('connect', function () {
chat.emit('hi!');
});
news.on('news', function () {
news.emit('woot');
});
</script>
Oh. Interesting. It does not look like you have a server even running, just io.
//app.js
var app = express();
var http = require('http');
var server = http.createServer(app);
app.use(express.static(__dirname + '/public'));
/* place index.html in /public */
var port = process.env.PORT || 5050;
var io = require('socket.io').listen(server, {
log : false
});
io.sockets.on('connection', function(socket) {
socket.on('hi', function(data) {
console.log('yay');
socket.emit('hello');
});
});
server.listen(port);
//for index.html do the same.
chat.on('connect', function () {
chat.emit('hi');
});
chat.on('hello',function(){
console.log('yay got hello');
}):
// I am just beginning on stackoverflow. Not that great at explaining. I am using express here because it is simplier. If this does not work, give me what version of node, express, and socket.io you are using. Also, I have not used namespaces yet for socket.io, so this could be a problem.