使用JavaWeb webSocket实现简易的点对点聊天功能实例代码
作者:mangues 发布时间:2023-10-29 00:14:17
首先给大家声明一点:需要 jdk 7 , tomcat需要支持websocket的版本
1.InitServlet
该类主要是用来初始化构造将来存储用户身份信息的map仓库,利用其初始化方法Init 初始化仓库, 利用其静态方法getSocketList 获得对应的用户身份信息。
webSocket ,我认为MessageInbound 用来识别登录人的信息,用它来找到对应的人,推送消息。每次登录都会产生一个MessageInbound。
这里的 HashMap<String,MessageInbound> :string 存储用户session的登录id,MessageInbound存储 推送需要的身份信息。以上属于个人口头话理解。
package socket;
import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.catalina.websocket.MessageInbound;
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = -L;
//private static List<MessageInbound> socketList;
private static HashMap<String,MessageInbound> socketList;
public void init(ServletConfig config) throws ServletException {
// InitServlet.socketList = new ArrayList<MessageInbound>();
InitServlet.socketList = new HashMap<String,MessageInbound>();
super.init(config);
System.out.println("Server start============");
}
public static HashMap<String,MessageInbound> getSocketList() {
return InitServlet.socketList;
}
/* public static List<MessageInbound> getSocketList() {
return InitServlet.socketList;
}
*/}
2.MyWebSocketServlet
websocket用来建立连接的servlet,建立连接时,首先在session获取该登录人的userId,在调用MyMessageInbound构造函数传入userId
package socket;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.CharBuffer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
/**
*
* @ClassName: MyWebSocketServlet
* @Description: 建立连接时创立
* @author mangues
* @date --
*/
public class MyWebSocketServlet extends WebSocketServlet {
public String getUser(HttpServletRequest request){
String userName = (String) request.getSession().getAttribute("user");
if(userName==null){
return null;
}
return userName;
// return (String) request.getAttribute("user");
}
@Override
protected StreamInbound createWebSocketInbound(String arg,
HttpServletRequest request) {
System.out.println("##########");
return new MyMessageInbound(this.getUser(request));
}
}
3.onOpen方法调用InitServlet的map身份仓库,
放入用户userId 和 对应该登录用户的websocket身份信息MessageInbound (可以用userId来寻找到推送需要的 身份MessageInbound )
onTextMessage :用来获取消息,并发送消息
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 arg) throws IOException {
// TODO Auto-generated method stub
}
@Override
protected void onTextMessage(CharBuffer msg) throws IOException {
//用户所发消息处理后的map
HashMap<String,String> messageMap = MessageUtil.getMessage(msg); //处理消息类
//上线用户集合类map
HashMap<String, MessageInbound> userMsgMap = InitServlet.getSocketList();
String fromName = messageMap.get("fromName"); //消息来自人 的userId
String toName = messageMap.get("toName"); //消息发往人的 userId
//获取该用户
MessageInbound messageInbound = userMsgMap.get(toName); //在仓库中取出发往人的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();
}
/* for (MessageInbound messageInbound : InitServlet.getSocketList()) {
CharBuffer buffer = CharBuffer.wrap(msg);
WsOutbound outbound = messageInbound.getWsOutbound();
outbound.writeTextMessage(buffer);
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);
}
// InitServlet.getSocketList().add(this);
}
}
4.消息处理类,处理前端发来的消息
package util;
import java.nio.CharBuffer;
import java.util.HashMap;
/**
*
* @ClassName: MessageUtil
* @Description: 消息处理类
* @author mangues
* @date --
*/
public class MessageUtil {
public static HashMap<String,String> getMessage(CharBuffer msg) {
HashMap<String,String> map = new HashMap<String,String>();
String msgString = msg.toString();
String m[] = msgString.split(",");
map.put("fromName", m[]);
map.put("toName", m[]);
map.put("content", m[]);
return map;
}
}
5.web配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="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.小化
<%@ page language="java" contentType="text/html; charset=UTF-"
pageEncoding="UTF-"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-">
<title>Index</title>
<script type="text/javascript" src="js/jquery ...min.js"></script>
<%session.setAttribute("user", "小化");%>
<script type="text/javascript">
var ws = null;
function startWebSocket() {
if ('WebSocket' in window)
ws = new WebSocket("ws://localhost:/webSocket/mywebsocket.do");
else if ('MozWebSocket' in window)
ws = new MozWebSocket("ws://localhost:/webSocket/mywebsocket.do");
else
alert("not support");
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='小化';
};
}
function sendMsg() {
var fromName = "小化";
var toName = document.getElementById('name').value; //发给谁
var content = document.getElementById('writeMsg').value; //发送内容
ws.send(fromName+","+toName+","+content);
}
</script>
</head>
<body onload="startWebSocket();">
<p>聊天功能实现</p>
登录状态:
<span id="denglu" style="color:red;">正在登录</span>
<br>
登录人:
<span id="userName"></span>
<br>
<br>
<br>
发送给谁:<input type="text" id="name" value="小明"></input>
<br>
发送内容:<input type="text" id="writeMsg"></input>
<br>
聊天框:<textarea rows="" cols="" readonly id="xiaoxi"></textarea>
<br>
<input type="button" value="send" onclick="sendMsg()"></input>
</body>
</html>
B.小明
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index</title>
<script type="text/javascript" src="js/jquery 2.1.1.min.js"></script>
<%session.setAttribute("user", "小明");%>
<script type="text/javascript">
var ws = null;
function startWebSocket() {
if ('WebSocket' in window)
ws = new WebSocket("ws://localhost:8080/webSocket/mywebsocket.do");
else if ('MozWebSocket' in window)
ws = new MozWebSocket("ws://localhost:8080/webSocket/mywebsocket.do");
else
alert("not support");
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="小明";
};
}
function sendMsg() {
var fromName = "小明";
var toName = document.getElementById('name').value; //发给谁
var content = document.getElementById('writeMsg').value; //发送内容
ws.send(fromName+","+toName+","+content);
}
</script>
</head>
<body onload="startWebSocket();">
<p>聊天功能实现</p>
登录状态:
<span id="denglu" style="color:red;">正在登录</span>
<br>
登录人:
<span id="userName"></span>
<br>
<br>
<br>
发送给谁:<input type="text" id="name" value="小化"></input>
<br>
发送内容:<input type="text" id="writeMsg"></input>
<br>
聊天框:<textarea rows="13" cols="100" readonly id="xiaoxi"></textarea>
<br>
<input type="button" value="send" onclick="sendMsg()"></input>
</body>
</html>
以上所述是小编给大家介绍的使用JavaWeb webSocket实现简易的点对点聊天功能实例代码的相关知识网站的支持!


猜你喜欢
- 1. 启动入口本系列RocketMQ4.8注释github地址,希望对大家有所帮助,要是觉得可以的话麻烦给点一下Star哈前面我们已经分析完
- 为大家提供的MySQL忘记密码的解决方案,供大家参考,具体内容如下1.在操作系统windows操作系统,xp或win7.中进入如下目录:C:
- 本文实例讲述了Android开发实现的简单五子棋游戏。分享给大家供大家参考,具体如下:我刚刚在Android上写的一个五子棋的小程序,在这里
- 1.首先看下我的项目结构我们逐个讲解/** * 用户登录配置类 * @author Administrator * */public cla
- 之前的文章都是讲解springmvc+spring+mybatis 的整合,而很少有springmvc+spring+hibernate 因
- 非Web程序 1.AppDomain.CurrentDomain.BaseDirectory 2.Environment.CurrentDi
- ActiveMQ 结合 Spring 收发消息直接使用 ActiveMQ 的方式需要重复写很多代码,且不利于管理,Spring 提供了一种更
- 目录事件分发机制ViewGroup.dispatchTouchEvent 源码分析View.dispatchTouchEvent 和 Vie
- 目录知识点介绍正文1、质量压缩2、采样率压缩3、缩放法压缩4、RGB_565 通过改变图片格式来实现压缩总结知识点介绍Android 中图片
- 本文为大家分享了C#实现图书管理系统课程设计,供大家参考,具体内容如下一、设计目的通过模拟图书管理系统,实现以下功能学生账号的注册学生对馆藏
- Maven本地仓库有对应的jar包但是报找不到问题原因第一,你本地仓库对应的包文件夹下有_remote.repositories这个文件;第
- 本文实例为大家分享了Android实现可折叠式标题栏的具体代码,供大家参考,具体内容如下先看效果图:一、实现步骤:1、布局文件<?xm
- 经典排序算法 - 冒泡排序Bubble sort原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,这样一趟过去后,最大或
- 简述SpringBoot对Spring的的使用做了全面的封装,使用SpringBoot大大加快了开发进程,但是如果不了解Spring的特性,
- 目录Jacoco原理简介使用Jacoco生成代码执行覆盖率报告小结Jacoco是Java Code Coverage的缩写,顾名思义,它是获
- 1. 前言对于写Crud的老司机来说时间非常宝贵,一些样板代码写不但费时费力,而且枯燥无味。经常有小伙伴问我,胖哥你怎么天天那么有时间去搞新
- 今天给大家带来的是仅仅使用一个TextView实现一个 * 京东、淘宝、唯品会等各种电商APP的活动倒计时。最近公司一直加班也没来得及时间去整
- 首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。@Bean@LoadBalanc
- session简介做过Web开发的程序员应该对Session都比较熟悉,Session是一块保存在服务器端的内存空间,一般用于保存用户的会话
- 1.1 接口组成更新概述接口的组成常量:public static final抽象方法:public abstract默认方法(Java 8