天天看点

jwchat代码解释

最近研究JWChat这个基于jabber协议的聊天工具,搞了几个星期了,有点了解的地方在这里记录一下,先要说一声感谢本网站的:mircle 一篇文章:<a href="http://www.iteye.com/topic/154697" target="_blank" rel="external nofollow" target="_balnk">http://www.iteye.com/topic/154697</a>,写的非常详细.入门配置是没有什么问题了。下面是我最近看代码的一点平时记录:

JWChat的jabber连接是面向对象的继承操作,核心类全部在jsjac.js里面,后面我看了,这个是一个独立的基于javascript 连接jabber服务器的组件,JWChat只是应用了他。

jwchat.html的init()方法里面有二种连接方法,分别是:JSJaCHttpBindingConnection和JSJaCHttpPollingConnection,这二种都继承了JSJaCConnection,JSJaCConnection是和jabber服务器连接的类, JSJaCHttpBindingConnection中 connect 是JSJaCHBCConnect.  JSJaCCHBCConnect是把客户端的各种信息组合成jabber协议的xml请求文档,它的oCon就是 JSJaCConnection的实例, 

JSJaCConnection里面的 JSJaCHBCDisconnect

在jwchat项目中,chat.html是发送信息的页面,function submitClicked是发送按钮的事件,这里的aMessage是发送信息对象,其中jwcMain就是chat.html的上级页面:jwchat.html对象。

  var aMessage = new JSJaCMessage();

  aMessage.setType('chat');

  aMessage.setTo(user.jid);

  aMessage.setBody(body);

  jwcMain.con.send(aMessage);

jsjac.js里面, JSJaCConnection.prototype._sendRaw 发送信息的方法。

jsjac 里面的 JSJaCConnection.prototype._handleEven 是触发事件的方法。

shared.js 是JWChat项目中,js共用方法类,里面有许多共用的方法让其它function 调用 : for example:cutResource

roster.js 是针对聊天好友和信息的管理js 文件。

roster.js里面的 RosterOpenChat,是打开聊天窗口 chat.html页面,并且加载收到的信息

JWChat 信息发送过程研究总结:ClientA 发送信息给 ClientB,先发送给 JHBServlet,然后JHBservlet发送给 openfire jabber服务器,openfire jabber服务器分析接收信息,查找接收信息ClientB,通过ClientB和服务器建立的xml流直接把信息发送给ClientB.

JWChat 里面请求加为好友的代码:subscription.thml 15行:

function sendSub() {

  var aPresence = new JSJaCPresence();

  aPresence.setType('subscribe');

  if (!document.forms[0].to.value || document.forms[0].to.value == '') {

    alert("JID missing");

    document.forms[0].to.focus();

    return false;

  }

  var to = document.forms[0].to.value;

  if (to.indexOf('@') == -1)

    to += '@' + srcW.JABBERSERVER;

  aPresence.setTo(to);

  if (document.forms[0].msg.value && document.forms[0].msg.value != '')

    aPresence.setStatus(document.forms[0].msg.value);

alert(aPresence.getDoc().xml);

  srcW.con.send(aPresence);

  window.close();

}

JWchat里面的 function handlePresence 是接收和发送用户增加好友请求处理的函数

继续阅读