基于开源WebSocket服务器宝贝鱼(CshBBrain)的应用横空出世

  开源WebSocket服务器 宝贝鱼(CshBBrain)发布有些日子了,很多人研究 宝贝鱼(CshBBrain) 仅仅是为了学习其中所使用到的WebSocket协议,或是其使用到的NIO,AIO技术,是学习多线程,并发包的使用。很多人也只是学习学习,研究研究,没有人想过将宝贝鱼(CshBBrain)用于项目中,因为没有看到有人使用,担心风险。

  下面我就秀一下采用服务器 宝贝鱼(CshBBrain) 作为后台业务服务器,CshBBrainJS 作为前台开发数据通信框架的移动互联网应用。数码快看(http://211.100.41.186:8989/mcms/ws/index_ws_tom.html),如果你喜欢数码快看产品请到www.qook.com.cn下载iOS或Android的安装程序。由于知识产权的关系,提供的实例屏蔽了部分功能,混淆了前台代码,只支持高版本的Chrome浏览器。下面上几张图片吧:








  使用CshBBrainJS 作为前台数据通信的框架与后台基于宝贝鱼(CshBBrain)的业务服务器进行数据交换。用户在使用系统的整个过程中客户端程序始终保持与业务服务器一个连接,所有请求的发送,数据的接收都通过这个连接。这就避免了采用ajax进行数据通信时,每次都必须创建一个连接,有时会在短时间内发起几十次请求,而请求发送的数据也不多,花销在创建连接上的时间却非常的客观。

  使用CshBBrainJS 请求后台数据的使用方式和使用jquery的getScript函数获取数据的方式相同。CshBBrainJS 提供getData(url,callBack)函数获取数据,使用时首先设置好要连接的地址bizServer;然后调用getData获取数据,调用时传递请求的接口以及参数url和获取数据后的处理函数callBack,url的格式遵循html的get请求数据格式:接口名?a=av&b=bv,会调函数在接收到请求响应数据时由CshBBrainJS 框架自动调用,CshBBrainJS 框架自己会调度管理好每个请求的发送,数据的接收和回调函数的调用。

  使用CshBBrainJS作为前台框架时,服务器返回时,必须将框架请求时传递的taskKey的值传递回来,并且约定好数据发送回来的js对象的变量名即可。下面贴上CshBBrainJS的所有代码,CshBBrainJS所有代码不压缩只有3k多点,压缩后只有1k多点。下面贴上CshBBrainJS的所有代码:

var taskCount = 1;

function socket(){

this.managerServer = "";//管理服务器地址

this.isClusters = 1;// 是否为集群websocket服务器,1:表示不是,2:表示是集群

this.bizServer = "ws://192.168.1.100:9090/";// 业务websocket服务器地址

this.bizSocket = null;// 业务websocket服务器连接

this.taskQueue = {};// 调用任务队列

this.waitQueue = new Array();// 等待调用的队列

this.taskKey = "taskKey";//任务调用key

this.tryCount = 0;// 尝试连接的次数

this.status = -1;// 业务websocket连接状态,-1:表示还没有创建连接,0:正在连接,1:表示没有连上服务器,2:正在关闭连接,3:表示已经关闭连接

var self = this;

this.init = function(){// 初始化连接

if(self.isClusters == 2){// 服务器采用集群的方式

var clusterSocket = new WebSocket(self.managerServer);

clusterSocket.onopen = function(event){

clusterSocket.send();// 获取biz地址请求

}

clusterSocket.onmessage = function(event){// 设置biz服务器地址,关闭连接

self.bizServer = event.data;

self.createBizSocket();

clusterSocket.close();

}

}else{// 服务器没有采用集群的方式

self.createBizSocket();

}

}

this.createBizSocket = function(){

self.bizSocket = new WebSocket(self.bizServer);

self.status = 0;// 正在连接中

self.bizSocket.onopen = function(event){// 连接成功

self.status = 1;// 成功连接上服务器

self.tryCount = 0;// 尝试连接的次数置空

var paramter = self.waitQueue.shift();

while(paramter){

self.taskQueue[paramter.timestamp] = paramter;

self.bizSocket.send(paramter.url);

paramter = self.waitQueue.shift();

}

}

this.bizSocket.onerror = function(event){

try{

self.bizSocket.close();

}catch(e){}

self.status = -1;// 正在关闭连接

++self.tryCount;// 尝试连接的次数

if(self.tryCount <= 10000){

// 将执行队列中的任务添加到数组中

for (prop in self.taskQueue){

self.waitQueue.unshift(self.taskQueue[prop]);

}

self.init();// 继续连接

}

//alert("连接出错");

}

/*

this.bizSocket.onclose = function(event){

this.status = -1;// 已经完成关闭操作

++this.tryCount;// 尝试连接的次数

}*/

this.bizSocket.onmessage = function(event){// 对业务服务器返回的业务代码进行处理,并调用回调函数

eval(event.data);// 执行服务器端返回的js代码

if(taskKey){// 有回调函数,则调用回调函数

try{

if(self.taskQueue[taskKey].callBack){// 存在回调函数就调用

self.taskQueue[taskKey].callBack(resultData);

}

}catch(e){

self.taskQueue[taskKey].callBack(null);

//alert("业务处理函数出现异常");

}

self.taskQueue[taskKey] = undefined;

}

//var stock = window.JSON.parse(event.data);

   };

}

this.currTime = function(formatStr){// 获取系统当前时间戳

return ++taskCount;

/*

if(formatStr){

return (new Date()).format(formatStr);

}else{

return (new Date()).valueOf();

}*/

}

this.processURL = function(url,timestamp){// 对于请求地址进行处理,并添加任务调度key

if(!url){

return;

}

var single = "&";

if(url.indexOf("?") == -1){

single = "?";

}

return encodeURIComponent(url) + single + self.taskKey + "=" + timestamp;

}

this.getData = function(url,callBack){// 调用websocket获取数据

var callKey = self.currTime();

var requestUrl = self.processURL(url,callKey);

console.log(url);

//if(callBack){// 有回调函数,将函数放入到调度队列中

var paramters = {timestamp:callKey,url:requestUrl,callBack:callBack};

self.taskQueue[callKey] = {timestamp:callKey,url:requestUrl,callBack:callBack};

//}

switch(self.status){

case -1:// 没有建立连接

self.init();

self.waitQueue.push(paramters);

break;

case 0:// 正在建立连接

self.waitQueue.push(paramters);

break;

case 1:// 连接已经建立

self.taskQueue[callKey] = paramters;

self.bizSocket.send(requestUrl);

break;

case 2:// 正在关闭连接

self.waitQueue.push(paramters);

break;

case 3:// 已经关闭连接

self.waitQueue.push(paramters);

break;

}

}

this.init();// 初始化websocket

}

var _ = new socket();// 创建websocket连接对象

服务器返回的数据格式为:

var taskKey = 5;var resultData= [{a:2,b:3},{a:8,b:9}];

希望以上的内容对你使用和学习CshBBrain,CshBBrainAIO,CshBBrainJS有所帮助。

猜你喜欢

转载自cshbbrain.iteye.com/blog/1739517