Javascript NodeJS、Express、Sockets.io:无法在服务器上启动(“连接”…上的.on

Javascript NodeJS、Express、Sockets.io:无法在服务器上启动(“连接”…上的.on,javascript,node.js,express,websocket,socket.io,Javascript,Node.js,Express,Websocket,Socket.io,我正在尝试将sockets.io集成到我的express应用程序中。我使用了express generator来格式化我的应用程序,因此我必须在bin/www中设置我的websocket。我发现了一个类似的问题,并应用了找到的答案,指示我在app.js中创建套接字对象,将其附加到app对象,然后通过bin/www中的导出将其访问到应用程序将其连接到服务器 以下是我当前的app.js文件: var express = require('express'), path = require('

我正在尝试将sockets.io集成到我的express应用程序中。我使用了express generator来格式化我的应用程序,因此我必须在bin/www中设置我的websocket。我发现了一个类似的问题,并应用了找到的答案,指示我在app.js中创建套接字对象,将其附加到app对象,然后通过bin/www中的导出将其访问到应用程序将其连接到服务器

以下是我当前的app.js文件:

var express = require('express'),
    path = require('path'),
    favicon = require('serve-favicon'),
    bodyParser = require('body-parser'),
    sanitizer = require('sanitizer'),
    socket_io = require('socket.io'),

    config = require('./lib/config'),

    db = require('./lib/db'),
    fs = require('fs'),

    app = express(),
    io = socket_io();

app.io = io;

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

app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json({strict: false}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use(function(req,res,next){
    req.io = io;
    next();
});

app.io.sockets.on('connection', function(socket){
    console.log('connected!');
});

app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.json({
        code: err.status,
        err: err.message
    }).end();
});

module.exports = app;
这是我的bin/www文件,关于socket.io和http服务器:

var app = require('../app');
var debug = require('debug')('fresco:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

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

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Socket.io
 */

app.io.attach(server);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
我试图通过
ws://localhost:3000/
访问本地运行的服务器,但在客户端没有收到响应。我在express中实现websockets是否有问题,导致websockets服务器崩溃?我无法想象这为什么不起作用,因为在app.js中创建的对象正在成功地运行assed to bin/www。将console.log放在bin/www的末尾显示以下内容:

{ nsps:
{ '/':
  { name: '/',
    server: [Circular],
    sockets: [],
    connected: {},
    fns: [],
    ids: 0,
    acks: {},
    adapter: [Object],
    _events: [Object] } },
_path: '/socket.io',
_serveClient: true,
_adapter: [Function: Adapter],
_origins: '*:*',
sockets:
{ name: '/',
 server: [Circular],
 sockets: [],
 connected: {},
 fns: [],
 ids: 0,
 acks: {},
 adapter: { nsp: [Circular], rooms: {}, sids: {}, encoder: {} },
 _events: { connection: [Function] } },
eio:
{ clients: {},
 clientsCount: 0,
 pingTimeout: 60000,
 pingInterval: 25000,
 upgradeTimeout: 10000,
 maxHttpBufferSize: 100000000,
 transports: [ 'polling', 'websocket' ],
 allowUpgrades: true,
 allowRequest: [Function],
 cookie: 'io',
 ws:
  { domain: null,
    _events: {},
    _maxListeners: undefined,
    options: [Object],
    path: null,
    clients: [] },
 _events: { connection: [Function] } },
 httpServer:
 { domain: null,
 _events:
  { connection: [Function: connectionListener],
    clientError: [Function],
    close: [Function],
    upgrade: [Function],
    request: [Function],
    error: [Function: onError],
    listening: [Function: onListening] },
 _maxListeners: undefined,
 _connections: 0,
 _handle:
  { fd: undefined,
    reading: false,
    owner: [Circular],
    onread: null,
    onconnection: [Function: onconnection],
    writeQueueSize: 0 },
 _usingSlaves: false,
 _slaves: [],
 allowHalfOpen: true,
 pauseOnConnect: false,
 httpAllowHalfOpen: false,
 timeout: 120000,
 _connectionKey: '4:null:3000' },
 engine:
 { clients: {},
 clientsCount: 0,
 pingTimeout: 60000,
 pingInterval: 25000,
 upgradeTimeout: 10000,
 maxHttpBufferSize: 100000000,
 transports: [ 'polling', 'websocket' ],
 allowUpgrades: true,
 allowRequest: [Function],
 cookie: 'io',
 ws:
  { domain: null,
    _events: {},
    _maxListeners: undefined,
    options: [Object],
    path: null,
    clients: [] },
 _events: { connection: [Function] } } }

尝试从
app.io.sockets.on
上删除套接字。在
app.io=io()
上。因此,io.sockets不是一个函数,而是一个
io.on('connection',function(){})
是。看起来像是一个打字错误