作者:shihuaping0918@163.com,轉(zhuǎn)載請(qǐng)注明作者
connector是一個(gè)重量級(jí)組件,它依賴connection/server/pushScheduler/session組件。
對(duì)session組件的依賴是對(duì)連接創(chuàng)建一個(gè)session。
對(duì)connection的依賴是連接數(shù)的增加減少辐脖,以及登錄用戶uid的管理。
對(duì)pushScheduler的依賴是皆愉,當(dāng)異步發(fā)送時(shí)嗜价,把發(fā)送任務(wù)放到scheduler里艇抠。
對(duì)server的依賴就是取一些配置,以及server本身的信息久锥。
下面依次對(duì)它們進(jìn)行介紹家淤。首先從session開始,session和connection的依賴代碼有一部分是在一起的。
/**
* get session for current connection
*/
var getSession = function(self, socket) {
var app = self.app,
sid = socket.id;
var session = self.session.get(sid); //sid
if (session) {
return session;
}
//這里創(chuàng)建一個(gè)session,
session = self.session.create(sid, app.getServerId(), socket);
logger.debug('[%s] getSession session is created with session id: %s', app.getServerId(), sid);
// bind events for session
socket.on('disconnect', session.closed.bind(session));
socket.on('error', session.closed.bind(session));
session.on('closed', onSessionClose.bind(null, app));
session.on('bind', function(uid) {
logger.debug('session on [%s] bind with uid: %s', self.app.serverId, uid);
// update connection statistics if necessary
if (self.connection) {
//添加用戶到connection中
self.connection.addLoginedUser(uid, {
loginTime: Date.now(),
uid: uid,
address: socket.remoteAddress.ip + ':' + socket.remoteAddress.port
});
}
self.app.event.emit(events.BIND_SESSION, session);
});
session.on('unbind', function(uid) {
if (self.connection) {
//從connection中移除用戶
self.connection.removeLoginedUser(uid);
}
self.app.event.emit(events.UNBIND_SESSION, session);
});
return session;
};
getSession這個(gè)函數(shù)是在 var bindEvents = function(self, socket)
這個(gè)函數(shù)里調(diào)用的。它的主要工作就是給事件綁定回調(diào)函數(shù)碴开。和session有關(guān)的就是創(chuàng)建session了。和connection有關(guān)的就是添加用戶和移除用戶青伤。session真正的實(shí)現(xiàn)都在service/sessionService.js里面。component/session.js只是一個(gè)抽象殴瘦。對(duì)connection也是同理狠角。
再來看一下pushScheduler是怎么被調(diào)用的,這里不涉及pushScheduler的內(nèi)部機(jī)制蚪腋,只看connector調(diào)用了它哪些接口丰歌。
pro.send = function(reqId, route, msg, recvs, opts, cb) {
logger.debug('[%s] send message reqId: %s, route: %s, msg: %j, receivers: %j, opts: %j', this.app.serverId, reqId, route, msg, recvs, opts);
if (this.useAsyncCoder) {
return this.sendAsync(reqId, route, msg, recvs, opts, cb);
}
var emsg = msg;
if (this.encode) {
// use costumized encode
emsg = this.encode.call(this, reqId, route, msg);
} else if (this.connector.encode) {
// use connector default encode
emsg = this.connector.encode(reqId, route, msg);
}
this.doSend(reqId, route, emsg, recvs, opts, cb);
};
pro.sendAsync = function(reqId, route, msg, recvs, opts, cb) {
var emsg = msg;
var self = this;
if (this.encode) {
// use costumized encode
this.encode(reqId, route, msg, function(err, encodeMsg) {
if (err) {
return cb(err);
}
emsg = encodeMsg;
self.doSend(reqId, route, emsg, recvs, opts, cb);
});
} else if (this.connector.encode) {
// use connector default encode
this.connector.encode(reqId, route, msg, function(err, encodeMsg) {
if (err) {
return cb(err);
}
emsg = encodeMsg;
self.doSend(reqId, route, emsg, recvs, opts, cb);
});
}
}
pro.doSend = function(reqId, route, emsg, recvs, opts, cb) {
if (!emsg) {
process.nextTick(function() {
return cb && cb(new Error('fail to send message for encode result is empty.'));
});
}
//只需要關(guān)注這里就可以了
this.app.components.__pushScheduler__.schedule(reqId, route, emsg,
recvs, opts, cb);
}
send和sendAsync的區(qū)別不是表現(xiàn)在網(wǎng)絡(luò)發(fā)送上,而是在編碼上屉凯,代碼中最終網(wǎng)絡(luò)發(fā)送調(diào)的都是doSend立帖。把要發(fā)的內(nèi)容放到sheduler里面去,在里面進(jìn)行真正的發(fā)送操作神得。
最后看一下對(duì)server的依賴厘惦,connector對(duì)server的依賴表現(xiàn)在取一些配置信息上。比如要綁定的host/port哩簿。
var getConnector = function(app, opts) {
var connector = opts.connector;
if (!connector) {
return getDefaultConnector(app, opts);
}
if (typeof connector !== 'function') {
return connector;
}
//就是這里了
var curServer = app.getCurServer();
//取clientPort,host
return connector(curServer.clientPort, curServer.host, opts);
};
另外還有一個(gè)依賴就是要調(diào)用server中的handler對(duì)消息進(jìn)行處理。
self.server.globalHandle(msg, session.toFrontendSession(), function(err, resp, opts) {
對(duì)connector本身的分析到這里就算是基本完成了酝静,但是又留下了更多的疑問节榜。其中session/connection還比較好理解。而scheduler和server理解起來就是有一定困難的别智。后續(xù)會(huì)慢慢講到宗苍。