简体   繁体   English

node.js和socket.io应用程序未运行

[英]node.js and socket.io application not running

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. 我在亚马逊ec2上运行这个东西。

Thanks 谢谢

也许是因为你在客户端正在监听localhost。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM