socket.io连接事件不起作用,为什么?

时间:2016-01-21 10:51:07

标签: javascript node.js express websocket socket.io

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

app.js

var express = require('express'),
    app = require('express')(),
    http = require('http').Server(app),
    io = require('socket.io')(http),
    start = require('./routes/index.js');

app.use('/', start);

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

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;
});

module.exports = app;

layout.jade

doctype html
html(lang="en")
head
    script(type="text/javascript" src="https://cdn.socket.io/socket.io-1.4.3.js")
    script.
        var socket = io();

body
    block content

1 个答案:

答案 0 :(得分:2)

尝试收听 http 而非应用。像这样:

var server = http.listen(3000, function () {
...