我嘗試用[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);
})
});