首先给大家说一下:需要jdk7,tomcat需要支持websocket的版本,那么下面我们来看一下关于使用JavaWeb webSocket实现简易的点对点聊天功能实例代码吧。
1、InitServlet
该类主要是用来初始化构造将来存储用户身份信息的map仓库,利用其初始化方法Init初始化仓库,利用其静态方法getSocketList获得对应的用户身份信息。
webSocket,我认为MessageInbound用来识别登录人的信息,用它来找到对应的人,推送消息。每次登录都会产生一个MessageInbound。
这里的HashMap:string存储用户session的登录id,MessageInbound存储推送需要的身份信息。以上属于个人口头话理解。
packagesocket;
importjava.nio.CharBuffer;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjavax.servlet.ServletConfig;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importorg.apache.catalina.websocket.MessageInbound;
publicclassInitServletextendsHttpServlet{
privatestaticfinallongserialVersionUID=-L;
//privatestaticListsocketList;
privatestaticHashMapsocketList;
publicvoidinit(ServletConfigconfig)throwsServletException{
//InitServlet.socketList=newArrayList();
InitServlet.socketList=newHashMap();
super.init(config);
System.out.println("Serverstart============");
}
publicstaticHashMapgetSocketList(){
returnInitServlet.socketList;
}
/*publicstaticListgetSocketList(){
returnInitServlet.socketList;
}
*/}
2、MyWebSocketServlet
websocket用来建立连接的servlet,建立连接时,首先在session获取该登录人的userId,在调用MyMessageInbound构造函数传入userId
packagesocket;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.nio.CharBuffer;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.catalina.websocket.StreamInbound;
importorg.apache.catalina.websocket.WebSocketServlet;
/**
*
*@ClassName:MyWebSocketServlet
*@Description:建立连接时创立
*@authormangues
*@date--
*/
publicclassMyWebSocketServletextendsWebSocketServlet{
publicStringgetUser(HttpServletRequestrequest){
StringuserName=(String)request.getSession().getAttribute("user");
if(userName==null){
returnnull;
}
returnuserName;
//return(String)request.getAttribute("user");
}
@Override
protectedStreamInboundcreateWebSocketInbound(Stringarg,
HttpServletRequestrequest){
System.out.println("##########");
returnnewMyMessageInbound(this.getUser(request));
}
}
3、onOpen方法调用InitServlet的map身份仓库
放入用户userId和对应该登录用户的websocket身份信息MessageInbound(可以用userId来寻找到推送需要的身份MessageInbound)
onTextMessage:用来获取消息,并发送消息
packagesocket;
importjava.io.IOException;
importjava.nio.ByteBuffer;
importjava.nio.CharBuffer;
importjava.util.HashMap;
importorg.apache.catalina.websocket.MessageInbound;
importorg.apache.catalina.websocket.WsOutbound;
importutil.MessageUtil;
publicclassMyMessageInboundextendsMessageInbound{
privateStringname;
publicMyMessageInbound(){
super();
}
publicMyMessageInbound(Stringname){
super();
this.name=name;
}
@Override
protectedvoidonBinaryMessage(ByteBufferarg)throwsIOException{
//TODOAuto-generatedmethodstub
}
@Override
protectedvoidonTextMessage(CharBuffermsg)throwsIOException{
//用户所发消息处理后的map
HashMapmessageMap=MessageUtil.getMessage(msg);//处理消息类
//上线用户集合类map
HashMapuserMsgMap=InitServlet.getSocketList();
StringfromName=messageMap.get("fromName");//消息来自人的userId
StringtoName=messageMap.get("toName");//消息发往人的userId
//获取该用户
MessageInboundmessageInbound=userMsgMap.get(toName);//在仓库中取出发往人的MessageInbound
if(messageInbound!=null){//如果发往人存在进行操作
WsOutboundoutbound=messageInbound.getWsOutbound();
Stringcontent=messageMap.get("content");//获取消息内容
StringmsgContentString=fromName+""+content;//构造发送的消息
//发出去内容
CharBuffertoMsg=CharBuffer.wrap(msgContentString.toCharArray());
outbound.writeTextMessage(toMsg);//
outbound.flush();
}
/*for(MessageInboundmessageInbound:InitServlet.getSocketList()){
CharBufferbuffer=CharBuffer.wrap(msg);
WsOutboundoutbound=messageInbound.getWsOutbound();
outbound.writeTextMessage(buffer);
outbound.flush();
}*/
}
@Override
protectedvoidonClose(intstatus){
InitServlet.getSocketList().remove(this);
super.onClose(status);
}
@Override
protectedvoidonOpen(WsOutboundoutbound){
super.onOpen(outbound);
//登录的用户注册进去
if(name!=null){
InitServlet.getSocketList().put(name,this);
}
//InitServlet.getSocketList().add(this);
}
}
4、消息处理类,处理前端发来的消息
packageutil;
importjava.nio.CharBuffer;
importjava.util.HashMap;
/**
*
*@ClassName:MessageUtil
*@Description:消息处理类
*@authormangues
*@date--
*/
publicclassMessageUtil{
publicstaticHashMapgetMessage(CharBuffermsg){
HashMapmap=newHashMap();
StringmsgString=msg.toString();
Stringm[]=msgString.split(",");
map.put("fromName",m[]);
map.put("toName",m[]);
map.put("content",m[]);
returnmap;
}
}
5、web配置
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
mywebsocket
socket.MyWebSocketServlet
mywebsocket
*.do
initServlet
socket.InitServlet
1
index.jsp
6、前端,为方便起见,我直接用了两个jsp,在其中用<%session.setAttribute("user","小明")%>;来表示登录。
两个jsp没任何本质差别,只是用来表示两个不同的人登录,可以同两个浏览器打开不同的jsp,来聊天操作
A.小化
<%@pagelanguage="java"contentType="text/html;charset=UTF-"
pageEncoding="UTF-"%>
Index
<%session.setAttribute("user","小化");%>
varws=null;
functionstartWebSocket(){
if('WebSocket'inwindow)
ws=newWebSocket("ws://localhost:/webSocket/mywebsocket.do");
elseif('MozWebSocket'inwindow)
ws=newMozWebSocket("ws://localhost:/webSocket/mywebsocket.do");
else
alert("notsupport");
ws.onmessage=function(evt){
//alert(evt.data);
console.log(evt);
$("#xiaoxi").val(evt.data);
};
ws.onclose=function(evt){
//alert("close");
document.getElementById('denglu').innerHTML="离线";
};
ws.onopen=function(evt){
//alert("open");
document.getElementById('denglu').innerHTML="在线";
document.getElementById('userName').innerHTML='小化';
};
}
functionsendMsg(){
varfromName="小化";
vartoName=document.getElementById('name').value;//发给谁
varcontent=document.getElementById('writeMsg').value;//发送内容
ws.send(fromName+","+toName+","+content);
}
聊天功能实现
登录状态:
正在登录
登录人:
发送给谁:
发送内容:
聊天框:
B.小明
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
Index
<%session.setAttribute("user","小明");%>
varws=null;
functionstartWebSocket(){
if('WebSocket'inwindow)
ws=newWebSocket("ws://localhost:8080/webSocket/mywebsocket.do");
elseif('MozWebSocket'inwindow)
ws=newMozWebSocket("ws://localhost:8080/webSocket/mywebsocket.do");
else
alert("notsupport");
ws.onmessage=function(evt){
console.log(evt);
//alert(evt.data);
$("#xiaoxi").val(evt.data);
};
ws.onclose=function(evt){
//alert("close");
document.getElementById('denglu').innerHTML="离线";
};
ws.onopen=function(evt){
//alert("open");
document.getElementById('denglu').innerHTML="在线";
document.getElementById('userName').innerHTML="小明";
};
}
functionsendMsg(){
varfromName="小明";
vartoName=document.getElementById('name').value;//发给谁
varcontent=document.getElementById('writeMsg').value;//发送内容
ws.send(fromName+","+toName+","+content);
}
聊天功能实现
登录状态:
正在登录
登录人:
发送给谁:
发送内容:
聊天框:
以上所述是小编给大家介绍的使用JavaWebwebSocket实现简易的点对点聊天功能实例代码的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。
网友评论