RTDT 基于websocket的数据传输框架

来源:互联网 发布:宾夕法尼亚大学 知乎 编辑:程序博客网 时间:2024/06/08 16:20

项目地址 RTDT for java
项目需求:最近做一个基于web页面的聊天系统,在做好友列表获取的时候,如果采用ajax来做确实很快,但是想想,我需要先从服务器拉取好友的最后一条消息,然后还需要从服务器拉取每个用户的头像,名称等,那么数据量就不少了,而且消息列表需要手动去刷新才会更新。为了找到另一种方案来解决,决定自己写一个websocket的数据交互框架
项目进展
前端:目前实现常见ajax请求方式,另外增加广播地址绑定功能,代码已封装
后端:满足前台要求,action采用xml文件方式,自动赋值,只需要写对应的javabean并配 置了xml文件,其它操作都交给RTDT框架完成。
兼容性
js兼容
搜狗浏览器最新版本 通过
chrome最新版本 通过
QQ浏览器最新版本 通过
edge 通过
后台
jdk 开发在jdk 1.8
tomcat 8
项目用时
两天
前端调用案例

    <script type="text/javascript" src="js/jquery-2.1.0.min.js" ></script>        <script type="text/javascript" src="js/conf.js" ></script>        <script type="text/javascript" src="js/RTDT.js" ></script>        <script>            RTDT.Req({                action: "testAction",                parameters:"name=%E6%B5%8B%E8%AF%95%E8%80%85&age=22&sex=%E7%94%B7&other=",                success:function(data){                    $("body").append("服务器返回正常数据:"+data+"</br>");                },error:function(err){                    console.log(err)                    $("body").append("服务器返回错误数据:"+err.data+"</br>");                }            })            RTDT.Bind({                action:"NotifyBind",                bind:function(data){                    $("body").append("绑定成功:"+data+"<br/>");                },                notify:function(data){                    $("body").append("得到广播:"+data+"<br/>");                },error:function(err){                $("body").append("出现错误:"+err.status+"<br/>");                    console.log(err)                }            })        </script>

后台XML配置案例

<?xml version="1.0" encoding="UTF-8"?><rtdt-mapping>    <action name="testAction" method="test" class="com.test.rdt.RDTAction"></action>    <action name="TokenSave" method="TokenSave" class="com.test.rdt.RDTAction"></action>    <action name="TokenGet" method="TokenGet" class="com.test.rdt.RDTAction"></action>    <action name="Notify" method="notifytest" type="Notify" class="com.test.rdt.RDTAction"></action>    <action name="Radio" method="radio" class="com.test.rdt.RDTAction"></action>    <!-- 这是一个虚拟地址 -->    <action name="NotifyBind" type="Notify"></action></rtdt-mapping>

后台javaBean案例

package com.test.rdt;import java.util.Iterator;import java.util.List;import com.YaNan.frame.RTDT.Notification;import com.YaNan.frame.RTDT.actionSupport.TokenAction;import com.YaNan.frame.RTDT.context.ActionManager;import com.YaNan.frame.RTDT.entity.RequestAction;public class RDTAction extends TokenAction{    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAge() {        return age;    }    public void setAge(String age) {        this.age = age;    }    public String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    private String age;    private String sex;    public String test(){        System.out.println("excute test rtdt");        System.out.println(this.toString());        return "hello RTDT! my name is "+this.name+",and my age is "+this.age+",last and important,im a " +this.sex;    }    @Override    public String toString() {        return "RDTAction [name=" + name + ", age=" + age + ", sex=" + sex + "]";    }    public void TokenSave(){        this.TokenContext.set("myinfo", this);        System.out.println("token:"+this.TokenContext.get("myinfo").toString());    }    public String TokenGet(){        System.out.println("token :"+this.TokenContext.get("myinfo").toString());        return this.TokenContext.get("myinfo").toString();    }    public void  notifytest(){        Notification notify = this.RequestContext.getNotification();        for(int i=0;i<10;i++){            notify.Notify("hello! its notify : "+i);        }    }    public String radio(){        System.out.println("得到信息:"+this.toString());        List<RequestAction> list = ActionManager.getActionManager().getNotifyList("NotifyBind");        if(list!=null){            System.out.println("notify size:"+list.size());            Iterator<RequestAction> iterator = list.iterator();            while (iterator.hasNext()) {                RequestAction notify = iterator.next();                System.out.println("转发到:"+notify.getAUID());                notify.getNotification().Notify("我是广播信息:"+this.toString());            }            return "广播发送成功";        }else{            return "没有绑定的notify";        }    }}

js调用代码测试代码:(普通请求和notify绑定)
RTDT js调用代码
模拟发送一条广播
发送一条广播
广播运行效果
RTDT广播运行效果
数据提交测试
RTDT数据提交效果
最后
如你所见,前台只需要很少的代码,你可以轻松搭建基于RTDT的网站,效率更高,速度更快,后台使用类似struts2的方式编写和配置。