APP下载

基于websocket的信息推送技术在网站系统中的设计与实现

2016-12-15张文康王治豪唐建国

电脑知识与技术 2016年27期

张文康+王治豪+唐建国

摘要:该文论述websocket信息推送技术的概念,以及如何在高校教学网络系统中设计实现websocket信息推送。

关键词:推送技术;高校教学网络系统;实时通信

中图分类号:TP393 文献标识码:A 文章编号:1009-3044(2016)27-0064-02

1 背景

传统的HTTP request模式存在明显的缺点,浏览器通过不断向服务器“轮询”获取最新信息,从而占用大量带宽资源。WebSocket协议作为HTML5的一种新协议,很好地解决了这个问题:浏览器发出websocket连接请求,服务器发生回应,产生“握手”,从而使浏览器与服务器之间产生长连接。这个长连接的生命周期是从建立握手到浏览器或者服务器一方主动断开连接为止,相对于以往的http连接,生命周期长。

2 服务器信息推送

首先,用户页面的布局用的是html的iframe子框架组合,头部子框架留出一块位置用户显示系统推送消息,同时负责与Server端进行websocket连接。然后管理员账号有系统消息选项,进入消息面板进行消息推送。

关键功能代码介绍如下。

子frame触发同源兄弟frame发送函数:

function postToServer(){

parent.frames["topFrame"].postToServer();

}

兄弟frame将消息发送给服务器:

function postToServer(){

var msg = parent.frames["main"].document.getElementById("pushtext").value;

we.send(msg);

}

Server端存储在线用户session的数据结构为动态数组:

private static ArrayList mmiList = new ArrayList();

若用户成功用websocket与Server握手时,将该用户节点加入数组:

public void onOpen(WsOutbound outbound){

System.out.println("Open Client");

this.myoutbound = outbound;

mmiList.add(this);}

用户关闭网站,与服务器断开握手时,删除该用户节点:

public void onClose(int status){

System.out.println("Close Client.");

mmiList.remove(this);

}

系统消息推送时,Server端将从管理员用户接收来的消息发送给所有在线用户;发送时通过遍历在线用户session数组进行发送:

public void onTextMessage(CharBuffer cb) throws IOException{

System.out.println("Accept Message:"+cb);

for(MyMessageInbound mmib : mmiList){

CharBuffer buffer = CharBuffer.wrap(cb);

mmib.myoutbound.writeTextMessage(buffer);

mmib.myoutbound.flush();

}

}

3 客户端信息推送

websocket技术如果是管理员面向全体在线用户就是系统推送,而如果是用户与用户间互相进行推送就升级为了在线聊天。

关键功能代码介绍如下。

//发送消息给Server

function sendMsg() {

var fromName = parent.frames["main"].document.getElementById("userName").innerHTML;

var toName = parent.frames["main"].document.getElementById(name).value; //发给谁

var content = parent.frames["main"].document.getElementById(writeMsg).value; //发送内容

ws.send(fromName+","+toName+","+content);

}

//从Server接收消息

ws.onmessage = function(e){

if(flage == 0){ parent.frames["BoardMenu"].document.getElementById("message").inn erHTML += e.data + "

";

}else{ parent.frames["main"].document.getElementById("xiaoxi").textContent += e.data + "\n";

} }

//Server端存储用户数据的数据结构是哈希表

public static HashMap getMessage(CharBuffer msg) {

HashMap map = new HashMap();

String msgString = msg.toString();

String m[] = msgString.split(",");

map.put("fromName", m[0]);

map.put("toName", m[1]);

map.put("content", m[2]);

return map;

}

//服务器发送给消息接收者

protected void onTextMessage(CharBuffer msg) throws IOException {

//用户所发消息处理后的map

HashMap messageMap = MessageUtil.getMessage(msg); //处理消息类

//上线用户集合类map

HashMap userMsgMap = socketList; //InitServlet.getSocketList();

String fromName = messageMap.get("fromName"); //消息来自人 的userId

String toName = messageMap.get("toName"); //消息发往人的 userId

//获取该用户

MessageInbound messageInbound = userMsgMap.get(toName);

MessageInbound messageInbound2 = userMsgMap.get(fromName); //在仓库中取出发往人的MessageInbound

if(messageInbound!=null){ //如果发往人 存在进行操作

WsOutbound outbound = messageInbound.getWsOutbound();

String content = messageMap.get("content"); //获取消息内容

String msgContentString = fromName + " " + content; //构造发送的消息

//发出去内容

CharBuffer toMsg = CharBuffer.wrap(msgContentString.toCharArray());

outbound.writeTextMessage(toMsg); //

outbound.flush();

if(messageInbound2!=null){ //如果发往人 存在进行操作

WsOutbound outbound2 = messageInbound2.getWsOutbound();

String content2 = messageMap.get("content"); //获取消息内容

String msgContentString2 = fromName + " " + content2; //构造发送的消息

//发出去内容

CharBuffer toMsg2 = CharBuffer.wrap(msgContentString2.toCharArray());

outbound2.writeTextMessage(toMsg2);

outbound2.flush();

}

}else{

WsOutbound outbound3 = messageInbound2.getWsOutbound();

String error = "系统消息:对不起,对方不在线!";

CharBuffer toMsg3 = CharBuffer.wrap(error.toCharArray());

outbound3.writeTextMessage(toMsg3);

outbound3.flush();

}

4 结束语

本文基于websocket技术实现了网络教学系统的信息推送服务,实现了管理员面向全体在线用户就是系统推送,用户与用户间的在线聊天等功能。

参考文献:

[1] Williams N S. Java Web高级编程[M]. 王肖峰, 译. 北京: 清华大学出版社, 2015.

[2] Ja Lengstorf, Leggetter P.构建实时Web应用[M]. 肖志清, 译.北京: 机械工业出版社, 2013.

[3] 赵振,王顺. Web异步与实时交互iframe Ajax WebSocket开发实战[M]. 北京: 人民邮电出版社, 2016.