express@3.0.0rc4
、および を使用して単純なアプリケーションを構築しようとしていますsocket.io@0.9.10
。
Express Generator を使用してアプリのスケルを生成しました。
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]
したがって、ドキュメントが非推奨になり、ソケットがソケットになったのではないかと思いました。だから私はやっ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は、私が見ている機能ではないようです。
では、ハンドラーを設定するにはどうすればよいでしょうか。Webページ、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);
})
});