2012-09-30 88 views
2

我尝试用[email protected],并[email protected]构建一个简单的应用程序。Socket.io和快递上的NodeJS

伊夫产生我和快递发电机应用的skel目录:

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] } } 

那么,是插座的方法?无处。但看,有一个套接字功能:

Socket: [Function: 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 separatly

我会后我的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); 
    }) 
}); 

回答

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..') 
})