<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
在上一篇《基于Mozilla Thunderbird的扩展开发(五)---进程间通信之Socket篇(上)》中开发了一个简单的TCP服务器,本文将介绍其对应的客户端。
客户端代码:
consttBirdBiffClientUi=
{
tBirdBiffClientOnLoad:function()
{
//removetoavoidduplicateinitialization
removeEventListener("load",tBirdBiffClientUi.tBirdBiffClientOnLoad,true);
//初始化客户端
varclient=Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
client.initialize();
varwindowCount=client.getWindowCollection().Count();
client.addWindow(window);
client=null;
tBirdBiffClientObserver.updateUi();//更新UI
//registerfornotifications
varservice=Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
service.addObserver(tBirdBiffClientObserver,"thunderbirdBiff.uiUpdate",false);//加入监控者
service=null;
},
tBirdBiffClientOnClose:function()
{
//removetoavoidduplicateinitialization
removeEventListener("close",tBirdBiffClientUi.tBirdBiffClientOnClose,true);
varservice=Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
service.removeObserver(tBirdBiffClientObserver,"thunderbirdBiff.uiUpdate");//移除监控者
service=null;
varclient=Components.classes["@phinecos.cnblogs.com/TBbiff/client;1"].getService(Components.interfaces.nsISupports).wrappedJSObject;
client.removeWindow(window);//移除窗口
client=null;
}
}
addEventListener("load",tBirdBiffClientUi.tBirdBiffClientOnLoad,true);
addEventListener("close",tBirdBiffClientUi.tBirdBiffClientOnClose,true);
客户类:
constCI=Components.interfaces,CC=Components.classes,CR=Components.results;
tBirdBiffClient.classID=Components.ID("{5b0bccd0-83b9-11db-9fe1-0800200c9a66}");
tBirdBiffClient.contractID="@phinecos.cnblogs.com/TBbiff/client;1";
tBirdBiffClient.classDescription="BiffClientService";
functiontBirdBiffClient()
{
this.timer=CC["@mozilla.org/timer;1"].getService(CI.nsITimer);//定时器
this.interval=null;//定时器时间间隔
this.socketTransport=null;
this.biffState=null;//邮箱状态
this.socket=null;//socket对象
this.input=null;//输入流
this.port=null;//服务器端口
this.hostname=null;//服务器主机名称
this.windowCollection=CC["@mozilla.org/supports-array;1"].createInstance(CI.nsICollection);//客户端窗口集合
this.initialized=false;//是否已经初始化
}
tBirdBiffClient.prototype=
{
getConnection:function()
{
this.closeSocket();//关闭先前的连接
this.socket=this.socketTransport.createTransport(null,0,this.hostname,this.port,null);//创建socket连接
if(!this.socket)
{
return;
}
varstream=this.socket.openInputStream(CI.nsITransport.OPEN_BLOCKING|CI.nsITransport.OPEN_UNBUFFERED,0,0);//打开输入流,类型为阻塞式,无缓冲
if(!stream)
{
this.closeSocket();
return;
}
this.input=CC["@mozilla.org/scriptableinputstream;1"].createInstance(CI.nsIScriptableInputStream);
if(!this.input)
{
this.closeSocket();
return;
}
this.input.init(stream);//初始化输入流
},
closeSocket:function()
{//关闭socket连接
if(this.input)
{
this.input.close(null);
this.input=null;
}
if(this.socket)
{
this.socket.close(null);
this.socket=null;
}
},
currentEventQueue:function()
{//当前事件队列
varEQS=CC["@mozilla.org/event-queue-service;1"].getService(CI.nsIEventQueueService);
returnEQS.getSpecialEventQueue(EQS.CURRENT_THREAD_EVENT_QUEUE);
},
initialize:function()
{//初始化客户端
if(this.initialized)
{
return;
}
this.getIntervalPref();//获取时间间隔
this.hostname=this.utility.getHostnamePref();//获取主机名称
this.port=this.utility.getPortPref();//获取端口
this.socketTransport=CC["@mozilla.org/network/socket-transport-service;1"].getService(CI.nsISocketTransportService);
this.getConnection();//创建socket连接
this.timer.initWithCallback(tBirdBiffCheckMailCallback,1000,this.timer.TYPE_ONE_SHOT);//启动定时器监听来自服务器的响应
this.initialized=true;
},
updateUi:function(result)
{//更新客户端UI
varstate;
switch(result)
{
case"":
{
state="offline";
break;
}
case"0":
{
state="noMail";
break;
}
case"1":
{
state="newMail";
break;
}
case"-1":
{
state="error";
break;
}
default:
{
state="weirdness";
break;
}
}
this.biffState=state;
state=null;
varservice=CC["@mozilla.org/observer-service;1"].getService(CI.nsIObserverService);
service.notifyObservers(null,"thunderbirdBiff.uiUpdate",null);
service=null;
},
checkForMail:function()
{//检查是否有数据到来
this.timer.initWithCallback(tBirdBiffReadFromServerCallback,1000,this.timer.TYPE_ONE_SHOT);
},
readFromServer:function()
{//从服务器读取数据
if(!this.input)
{//还未初始化输入流
this.getConnection();
}
try
{
varresult=this.input.read(1);//读取数据
if(result.length==1)
{
this.updateUi(result);//更新状态
}
result=null;
}
catch(e)
{
this.getConnection();
this.updateUi("");
}
this.timer.initWithCallback(tBirdBiffCheckMailCallback,this.interval,this.timer.TYPE_ONE_SHOT);//设置定时器
},
}
客户端监听者,负责监视邮箱的状态变化和读取来自服务器端的数据:
consttBirdBiffReadFromServerCallback=
{
notify:function(timer)
{//从服务器读取数据
varclient=CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject;
client.readFromServer();
client=null;
}
}
consttBirdBiffCheckMailCallback=
{
notify:function(timer)
{//检查邮箱状态
varclient=CC[tBirdBiffClient.contractID].getService(CI.nsISupports).wrappedJSObject;
client.checkForMail();
client=null;
}
}
为了测试服务器和客户端,我们使用firefox作为客户端的载体,thunderbird作为服务器端载体,可以看到thunderbird的邮箱状态会定时传给firefox,从而使得后者能随之更新其状态。
Reference:
1, https://addons.mozilla.org/en-US/thunderbird/addon/3788
分享到:
相关推荐
Thunderbird Setup 13.0.1.exe
Mozilla Thunderbird中文名“雷鸟”是从Mozilla Application Suite独立出来的电子邮件客户端软件。软件继承了Mozilla邮件组件的功能并行重新设计,可整合多个网络邮箱于一体,随时都能拉取邮件到本地处理,或者随时...
Mozext Mozilla Firefox和Mozilla Thunderbird的扩展/附加组件请检查我的专用网站以获取更多详细信息: → 目录WebExtensions / MailExtensions | 请检查每个文件夹中的特定自述文件以获取构建说明。雷鸟 火狐浏览器...
基于 Mozilla firefox 的扩展开发教程。如果你对firefox插件感兴趣,或许你也可以成为开发中的一员。
本分详细对Mozilla的结构和对其进行扩展的基本步骤。
XML语言语法 Javascript等在Mozilla的应用
Mozilla Thunderbird是由Mozilla浏览器的邮件功能部件所改造的邮件工具,使用 XUL 程序界面语言所设计,是专门为搭配 Mozilla Firefox 浏览器使用者所设计的邮件客户端软件,介面设计更简洁、而且免安装。
雷鸟Mozilla Thunderbird自动化设置理想情况下,它将其定位为Web服务器上的独立程序。 Thunder,根据其目录中的文件,访问Web服务器并接收配置。 并且根据后者的“指针”,它为用户设置了一个配置文件。 您不想将...
这个扩展是在线可用的Mozilla Thunderbird电子邮件客户端 Thunderbird online是一款Chrome扩展程序,提供功能齐全的电子邮件客户端,RSS和新闻组客户端。它支持不同的邮件帐户(POP,IMAP,Gmail)。它具有电子邮件...
mozilla_thunderbird_102
介绍了Mozilla扩展开发的基本知识和开发过程。
Stormhen(基于Mozilla Thunderbird)使用的便携式应用程序 :rocket: 。 可以在上找到文档和下载内容。 :warning_selector: 根据Mozilla的商标侵权报告( ),Thunderbird Portable在Portapps上被命名为Stormhen ...
mozilla-win32-1.3-installer.exe用于cygwin搭建环境。
Thunderbird-Tray是流行的电子邮件客户端Mozilla Thunderbird的系统托盘启动器工具。 您可以在不需要时使用TB托盘将Thunderbird隐藏在托盘中。 单击任务栏图标时,TB-Tray将还原Thunderbird。
Thunderbird Chrome文件夹-自定义Thunderbird V68 +的颜色,图标,间距和字体 安装将整个chrome文件夹放入您的个人资料中。 如果已经存在,请首先重命名。 文件userChrome.css是一个外壳,仅导入其他CSS文件即可...
Mozilla 3e 日历提供程序 (发音为“threee”)是一套客户端和服务器应用程序,它们共同提供...开发环境这个扩展依赖于 Mozilla Build System 和 Mozilla Thunderbird 以及 Mozilla Calendar 头文件。 不幸的是,Mozill
DKIM验证程序 这是Mozilla Thunderbird的附加组件,可根据RFC 6376验证DKIM签名。 Wiki中的更多信息,为 包含的第三方库 Joshua Tauberer的DNS库(Thunderbird发件人验证扩展的一部分)( ) dns.js-DNS库
继续跑 Mozilla Thunderbird的附加组件
雷鸟主题弧 灵感来自雷鸟的弧形主题。 安装 Mozilla附加组件(推荐) 手动的 ...unzip thunderbird-theme-arc.xpi && rm thunderbird-theme-arc.xpi zip -r thunderbird-theme-arc.xpi manifest.json
java javascript jsp 等调试工具