I have a small application which doesn’t seem to work. Please help.
Server:
var app = require('express').createServer();
var io = require('socket.io').listen(app);
app.listen(9002);
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
console.log("listening on port 9002");
app.get('/',function(req,res){
res.sendfile('index.html');
});
io.sockets.on('connection',function(socket){
socket.emit('serverevent',{ name: 'server' });
socket.on('firstevent',function(data){
console.log(data);
});
socket.on('disconnect', function () {
console.log("one client disconnected");
});
});
Client
<html>
<head>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>
var socket=io.connect('http://localhost:9002');
socket.on('serverevent', function(data){
console.log(data);
alert(data);
socket.emit('firstevent', { name: 'manu mehrotra' });
});
$(function(){
socket.emit('firstevent', { name: 'manu mehrotra' });
});
</script>
</head>
<body>
</body>
</html>
Nothing is printed in server log or console on connection and nothing is returned back by the server to the client to be shown in an alert box.
I am running this stuff on amazon ec2.
Thanks
Maybe it is because you’re listening for localhost on your client.