在session创建的时候,就调用了createSocket函数
org.omus._Session.prototype.createSocket = function () {
this.socket = new XMLSocket();
this.socket.active = true;
this.socket.onConnect = function (success) {
if (!success) {
var ses = org.omus.session;
if (ses.loginMode == "reconnect") ses.connectionBroken();
else ses.cancelConnect("ses-012");
}
};
this.socket.onClose = function () {
if (this.active) org.omus.session.handleClosedSocket();
};
this.socket.onData = function (str) {
org.omus.session.handleData(str);
};
}
在里面注册了接受数据的 onData事件,调用handelData函数
org.omus._Session.prototype.handleData = function (str) {
if (substring(str,1,13) == "#msgkeepAlive") {
if (org.omus.iLog.infoEnabled()) org.omus.iLog.logLocal("INFO","clj-009","");
this.socket.send(str);
return;
}
var env = org.omus.envFactory.getIncoming(str);
if (env == null) return;
if (org.omus.iLog.infoEnabled()) org.omus.iLog.logLocal("INFO","clj-010","str = " + str + "/nenvelope = " + env);
org.omus.msgRouter.handleIncoming(env);
}
env是个对象,由enFactory创建出来
var env = org.omus.envFactory.getIncoming(str);
代码如下
org.omus.EnvelopeFactory.prototype.getIncoming = function (str) {
// split header:
var head = substring(str,1,4);
var type = this.trimStr(substring(str,5,12));
var msgID = parseInt(this.trimStr(substring(str,17,4)));
var senderLen = parseInt(this.trimStr(substring(str,21,3)));
var subjLen = parseInt(this.trimStr(substring(str,24,3)));
var attachLen = parseInt(this.trimStr(substring(str,27,6)));
// error check
if (head != "#msg") {
org.omus.iLog.error("clj-052","message = " + str);
return null;
}
if (isNaN(msgID)) {
org.omus.iLog.error("clj-053","message = " + str);
return null;
}
if (isNaN(senderLen)) {
org.omus.iLog.error("clj-054","message = " + str);
return null;
}
if (isNaN(subjLen)) {
org.omus.iLog.error("clj-055","message = " + str);
return null;
}
if (isNaN(attachLen)) {
org.omus.iLog.error("clj-056","message = " + str);
return null;
}
// extract remaining parts:
var idx = 33;
var sender = substring(str,idx,senderLen);
idx += senderLen;
var subject = substring(str,idx,subjLen);
idx += subjLen - 1;
// check length of all parts
if (sender.length != senderLen) {
org.omus.iLog.error("clj-057","message = " + str);
return null;
}
if (subject.length != subjLen) {
org.omus.iLog.error("clj-058","message = " + str);
return null;
}
if (str.length - idx != attachLen) {
org.omus.iLog.error("clj-059","message = " + str);
return null;
}
// create envelope
var msg = new org.omus.Message(subject);
msg.setSender(sender);
msg.setMarshalledAttachment(str,idx);
return new org.omus.Envelope(msg,type,null,msgID);
}
然后是消息路由分发消息
org.omus.MessageRouter.prototype.handleIncoming = function (env) {
var ch = this.cache["m" + env.getID()];
var isCached = false;
var target;
if (ch != undefined) {
target = ch.src;
isCached = true;
} else {
target = this.typeMap[env.getType()];
}
if (typeof(target) == "undefined") {
this.unknownMsgType(env);
return;
}
target.handleMessage(env);
if (isCached) delete this.cache["m" + env.getID()];
if (this.cnt >= 5) {
// check timeouts
var now = getTimer();
for (var each in this.cache) {
var c = this.cache[each];
if ((now - c.time) > 30000) {
org.omus.iLog.warn("clj-061","cache = " + org.omus._Log.formatObject(c,0));
delete this.cache[each];
}
}
}
}
对于分发路由的这个函数中 的
target = this.typeMap[env.getType()];
target是事先登记好的单个实体,代码如下
org.omus.MessageRouter.prototype.init = function (env) {
var tm = this.typeMap;
tm.group = org.omus.group;
tm.setPassword = org.omus.user;
tm.setPerm = org.omus.user;
tm.setEmail = org.omus.user;
tm.customMsg = org.omus.messenger;
tm["msg.toServer"] = org.omus.messenger;
tm["adminMsg"] = org.omus.messenger;
tm["msg.subscr"] = org.omus.messenger;
tm["msg.unsubscr"] = org.omus.messenger;
tm["msg.unsubAll"] = org.omus.messenger;
tm["buddies"] = org.omus.user.getBuddies();
tm["blackList"] = org.omus.user.getBlackList();
tm.info = org.omus.info;
var lockObj = org.omus.user.getLocks();
tm["locks.acq"] = lockObj;
tm["locks.rel"] = lockObj;
tm["locks.relAll"] = lockObj;
}