这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 使用JavaWebwebSocket实现简易的点对点聊天功能实例代码

共1条 1/1 1 跳转至

使用JavaWebwebSocket实现简易的点对点聊天功能实例代码

菜鸟
2020-12-09 14:10:25     打赏

首先给大家说一下:需要jdk7,tomcat需要支持websocket的版本,那么下面我们来看一下关于使用JavaWeb webSocket实现简易的点对点聊天功能实例代码吧。

1、InitServlet

该类主要是用来初始化构造将来存储用户身份信息的map仓库,利用其初始化方法Init初始化仓库,利用其静态方法getSocketList获得对应的用户身份信息。

webSocket,我认为MessageInbound用来识别登录人的信息,用它来找到对应的人,推送消息。每次登录都会产生一个MessageInbound。

这里的HashMap<String,MessageInbound>: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;
  //privatestaticList<MessageInbound>socketList;
  privatestaticHashMap<String,MessageInbound>socketList;
  publicvoidinit(ServletConfigconfig)throwsServletException{
  //InitServlet.socketList=newArrayList<MessageInbound>();
  InitServlet.socketList=newHashMap<String,MessageInbound>();
  super.init(config);
  System.out.println("Serverstart============");
  }
  publicstaticHashMap<String,MessageInbound>getSocketList(){
  returnInitServlet.socketList;
  }
  /*publicstaticList<MessageInbound>getSocketList(){
  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
  HashMap<String,String>messageMap=MessageUtil.getMessage(msg);//处理消息类
  //上线用户集合类map
  HashMap<String,MessageInbound>userMsgMap=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{
  publicstaticHashMap<String,String>getMessage(CharBuffermsg){
  HashMap<String,String>map=newHashMap<String,String>();
  StringmsgString=msg.toString();
  Stringm[]=msgString.split(",");
  map.put("fromName",m[]);
  map.put("toName",m[]);
  map.put("content",m[]);
  returnmap;
  }
  }


5、web配置



  <?xmlversion="1.0"encoding="UTF-8"?>
  <web-appversion="3.0"
  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">
  <servlet>
  <servlet-name>mywebsocket</servlet-name>
  <servlet-class>socket.MyWebSocketServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>mywebsocket</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet>
  <servlet-name>initServlet</servlet-name>
  <servlet-class>socket.InitServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  </web-app>



6、前端,为方便起见,我直接用了两个jsp,在其中用<%session.setAttribute("user","小明")%>;来表示登录。

两个jsp没任何本质差别,只是用来表示两个不同的人登录,可以同两个浏览器打开不同的jsp,来聊天操作

  A.小化
  <%@pagelanguage="java"contentType="text/html;charset=UTF-"
  pageEncoding="UTF-"%>
  <!DOCTYPEhtml>
  <html>
  <head>
  <metahttp-equiv="Content-Type"content="text/html;charset=UTF-">
  <title>Index</title>
  <scripttype="text/javascript"src="js/jquery...min.js"></script>
  <%session.setAttribute("user","小化");%>
  <scripttype="text/javascript">
  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);
  }
  </script>
  </head>
  <bodyonload="startWebSocket();">
  <p>聊天功能实现</p>
  登录状态:
  <spanid="denglu"style="color:red;">正在登录</span>
  <br>
  登录人:
  <spanid="userName"></span>
  <br>
  <br>
  <br>
  发送给谁:<inputtype="text"id="name"value="小明"></input>
  <br>
  发送内容:<inputtype="text"id="writeMsg"></input>
  <br>
  聊天框:<textarearows=""cols=""readonlyid="xiaoxi"></textarea>
  <br>
  <inputtype="button"value="send"onclick="sendMsg()"></input>
  </body>
  </html>
  B.小明
  <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
  pageEncoding="UTF-8"%>
  <!DOCTYPEhtml>
  <html>
  <head>
  <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
  <title>Index</title>
  <scripttype="text/javascript"src="js/jquery2.1.1.min.js"></script>
  <%session.setAttribute("user","小明");%>
  <scripttype="text/javascript">
  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);
  }
  </script>
  </head>
  <bodyonload="startWebSocket();">
  <p>聊天功能实现</p>
  登录状态:
  <spanid="denglu"style="color:red;">正在登录</span>
  <br>
  登录人:
  <spanid="userName"></span>
  <br>
  <br>
  <br>
  发送给谁:<inputtype="text"id="name"value="小化"></input>
  <br>
  发送内容:<inputtype="text"id="writeMsg"></input>
  <br>
  聊天框:<textarearows="13"cols="100"readonlyid="xiaoxi"></textarea>
  <br>
  <inputtype="button"value="send"onclick="sendMsg()"></input>
  </body>
  </html>

以上所述是小编给大家介绍的使用JavaWebwebSocket实现简易的点对点聊天功能实例代码的相关知识,希望对大家有所帮助,最后想要了解更多Java信息的同学可以前往扣丁学堂官网咨询,扣丁学堂Java培训深受学员的喜爱。扣丁学堂不仅有专业的老师和与时俱进的课程体系,还有大量的Java视频教程供学员观看学习哦。扣丁学堂java技术交流群:487098661。



共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]