HTML5WebSocket点对点谈天实现办法-
发布时间:08/01 来源:未知 浏览:
关键词:
本文主要和大家介绍HTML5 WebSocket实现点对点谈天的示例代码的相干材料,小编觉得挺不错的,此刻分享给大家,也给大家做个参照 。一起追随小编过来看看吧,但愿能帮忙到大家。
public class InitServlet extends HttpServlet { private static final long serialVersionUID = -3163557381361759907L; private static HashMapsocketList; public void init(ServletConfig config) throws ServletException { InitServlet.socketList = new HashMap (); super.init(config); System.out.println("初始化谈天容器"); } public static HashMap getSocketList() { return InitServlet.socketList; } }
这里你可以跟本人的系统联合,对应的web配置代码如下:
websocket socket.MyWebSocketServlet websocket *.do initServlet socket.InitServlet 1 index.jsp
这就是最普通的前台像后台发送要求的历程,也是很容易嵌入到本人的系统里
MyWebSocketServlet:
public class MyWebSocketServlet extends WebSocketServlet { public String getUser(HttpServletRequest request){ String userName = (String) request.getSession().getAttribute("user"); if(userName==null){ return null; } return userName; } protected StreamInbound createWebSocketInbound(String arg0, HttpServletRequest request) { System.out.println("会员" + request.getSession().getAttribute("user") + "登录"); return new MyMessageInbound(this.getUser(request)); } }
MyMessageInbound继承MessageInbound
package socket; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.util.HashMap; import org.apache.catalina.websocket.MessageInbound; import org.apache.catalina.websocket.WsOutbound; import util.MessageUtil; public class MyMessageInbound extends MessageInbound { private String name; public MyMessageInbound() { super(); } public MyMessageInbound(String name) { super(); this.name = name; } @Override protected void onBinaryMessage(ByteBuffer arg0) throws IOException { } @Override protected void onTextMessage(CharBuffer msg) throws IOException { //会员所发新闻处置后的map HashMapmessageMap = MessageUtil.getMessage(msg); //处置新闻类 //上线会员汇合类map HashMap userMsgMap = InitServlet.getSocketList(); String fromName = messageMap.get("fromName"); //新闻来自人 的userId String toName = messageMap.get("toName"); //新闻发往人的 userId //猎取该会员 MessageInbound messageInbound = userMsgMap.get(toName); //在仓库中掏出发往人的MessageInbound MessageInbound messageFromInbound = userMsgMap.get(fromName); if(messageInbound!=null && messageFromInbound!=null){ //要是发往人 存在进行操纵 WsOutbound outbound = messageInbound.getWsOutbound(); WsOutbound outFromBound = messageFromInbound.getWsOutbound(); String content = messageMap.get("content"); //猎取新闻内容 String msgContentString = fromName + "说: " + content; //结构发送的新闻 //发出去内容 CharBuffer toMsg = CharBuffer.wrap(msgContentString.toCharArray()); CharBuffer fromMsg = CharBuffer.wrap(msgContentString.toCharArray()); outFromBound.writeTextMessage(fromMsg); outbound.writeTextMessage(toMsg); // outFromBound.flush(); outbound.flush(); } } @Override protected void onClose(int status) { InitServlet.getSocketList().remove(this); super.onClose(status); } @Override protected void onOpen(WsOutbound outbound) { super.onOpen(outbound); //登录的会员注册进去 if(name!=null){ InitServlet.getSocketList().put(name, this);//寄存客服ID与会员 } } @Override public int getReadTimeout() { return 0; } }
在onTextMessage中处置前台发出的信息,并封装信息传给指标
还有一个messageutil
package util; import java.nio.CharBuffer; import java.util.HashMap; public class MessageUtil { public static HashMapgetMessage(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; } }
固然了,前台也要按照规定的格局传信息
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Index 打赏
- 上一篇:HTML5表单验证失败的提醒语题目-
- 下一篇:AugularJS根基入门与实践-