2

我正在尝试使用 和 构建一个简单的应用express@3.0.0rc4程序socket.io@0.9.10

我已经使用 express 生成器生成了我的应用程序的骨架:

express --sessions --css stylus --ejs myapp

然后我编辑了 package.json 文件以包含 socket.io:

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app"
  },
  "dependencies": {
    "express": "3.0.0rc4",
    "ejs": "*",
    "stylus": "*",
    "socket.io": "*"
  }
}

跑了npm install。Socket.io 安装正确。

我打开了 app.js 文件,需要 socket.io,将其设置为侦听服务器,但是在添加事件处理程序时使用

io.sockets.on('connection', callback.. bla bla bla);

我明白了

io.sockets.on('connection', function(socket) {
           ^
TypeError: Cannot call method 'on' of undefined

我明白了

   info  - socket.io started

运行时,socket.io 正在初始化,但似乎没有 io.sockets 模块。

我跑去console.log(io);探索这个物体,我得到了:

{ version: '0.9.10',
  protocol: 1,
  clientVersion: '0.9.10',
  listen: [Function],
  Manager: 
   { [Function: Manager]
     defaultTransports: [ 'websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling' ] },
  Transport: [Function: Transport],
  Socket: [Function: Socket],
  Static: [Function: Static],
  Store: { [Function: Store] Client: [Function] },
  MemoryStore: { [Function: Memory] Client: [Function: Client] },
  RedisStore: { [Function: Redis] Client: [Function: Client] },
  parser: 
   { packets: 
      { disconnect: 0,
        connect: 1,
        heartbeat: 2,
        message: 3,
        json: 4,
        event: 5,
        ack: 6,
        error: 7,
        noop: 8 },
     reasons: 
      { 'transport not supported': 0,
        'client not handshaken': 1,
        unauthorized: 2 },
     advice: { reconnect: 0 },
     encodePacket: [Function],
     encodePayload: [Function],
     decodePacket: [Function],
     decodePayload: [Function] } }

那么sockets方法在哪里呢?无处。但是看,有一个Socket函数:

Socket: [Function: Socket]

所以我想也许文档已被弃用,套接字变成了 Socket。所以我做console.log(io.Socket);了并且得到了

  this.id = id;
  this.namespace = nsp;
  this.manager = manager;
  this.disconnected = false;
  this.ackPackets = 0;
  this.acks = {};
  this.setFlags();
  this.readable = readable;
  this.store = this.manager.store.client(this.id);
  this.on('error', defaultError);

但是那个 Socket.on 似乎不是我正在寻找的功能。

那么如何设置处理程序呢?在网页上,github 和那里的资源都使用io.sockets.on().

请注意,我也尝试npm install socket.io单独运行

我会发布我app.js的以防万一,但我的代码似乎没有问题:

var express = require('express')
  , routes = require('./routes')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path')
  , io = require('socket.io');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'ejs');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(express.static(path.join(__dirname, 'public')));
});

app.configure('development', function(){
  app.use(express.errorHandler());
});

app.get('/', routes.index);
app.get('/users', user.list);

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

io.listen(server);

console.log(io.Socket);


io.sockets.on('connection', function(socket) {
  socket.emit('init', { msg: 'Welcome'});
  socket.on('roll', function(data) {
    console.log(data);
  })
});
4

1 回答 1

7
var app = require('express')()
  , http = require('http')
  , server = http.createServer(app)
  , io = require('socket.io').listen(server)

server.listen(3000)    

io.on('connection',function(socket){
  console.log('connection..')
})
于 2012-09-30T20:18:28.120 回答