socket.io连接事件无法正常工作

时间:2014-07-18 09:32:22

标签: node.js sockets socket.io

我是nodejs的新手,并且表达和尝试socket.io只是一个简单的聊天应用程序 它非常简单的代码一切都很好,但节点控制台上没有事件触发日志。连接事件代码无效。 此外,浏览器端的socket.io.js文件给出200 ok。

var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');    

var app = express();    
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
        err.status = 404;
        next(err);
    });

    /// error handlers

    // development error handler
    // will print stacktraces
    if (app.get('env') === 'development') {
        app.use(function(err, req, res, next) {
            res.status(err.status || 500);
            res.render('error', {
                message: err.message,
                error: err
            });
        });
    }

    // production error handler
    // no stacktraces leaked to user
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: {}
        });
    });

        var http  = require('http');

    app.set('port', process.env.PORT || 3000);

    var server = http.createServer(app);
    var io = require('socket.io').listen(server);
    server.listen(app.get('port'), function(){
      console.log('Express server listening on port ' + app.get('port'));
    });

    io.on('connection', function(socket){  
      console.log('a user connected');  ///***this is not working
      socket.on('disconnect', function(){
        console.log('user disconnected'); 
      });
    });
    module.exports = app;

使用jade模板的客户端代码

extends layout  
block content
    h1 Socket.io
    ul#messages
    form(action='')
        input#m(autocomplete='off')
        button Send
    script(type='text/javascript', src='/socket.io/socket.io.js')
    script(type="text/javascript")
        var socket = io();

3 个答案:

答案 0 :(得分:0)

如果您提供客户端代码,可能会更好地回答这个问题;但是你可以在这里找到一些与socket.ok连接的简单例子https://github.com/dcorns/exploreSocketIO至少你需要或等同于客户端的方法,因为它会启动握手。如果按照链接,一切都应该清楚。

答案 1 :(得分:0)

您是否使用过客户端代码?  示例代码可能如下所示。

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
   });
</script>

答案 2 :(得分:0)

使用这样的:

  io.sockets.on('connection', function(socket){  
        console.log('a user connected');  
        socket.on('disconnect', function(){
           console.log('user disconnected'); 
        });
  });