websocket+tomcat实现聊天室功能

来源:互联网 发布:java如何实现分布式 编辑:程序博客网 时间:2024/05/22 14:49

本文不介绍websocket的理论知识,直接进行代码的实现,并且只对服务端进行实现,没有前端页面代码,通过在线工具进行测试


开发环境:eclipse Mars Release(4.5.0) + jdk1.7.0_79 + apache-maven-3.3.9 + tomcat7.0.82


1.项目模块



2.pom.xml中maven坐标的导入

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.lsy</groupId><artifactId>websocket-tomcat</artifactId><version>0.0.1-SNAPSHOT</version><packaging>war</packaging><dependencies><dependency><groupId>javax</groupId><artifactId>javaee-api</artifactId><version>7.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>3.1.0</version><scope>provided</scope></dependency></dependencies><build><finalName>tomcat</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.7</source><target>1.7</target></configuration></plugin></plugins></build></project>


3.java代码的实现

package com.lsy.websocket.demo;import java.io.IOException;import java.util.concurrent.CopyOnWriteArraySet;import javax.websocket.OnClose;import javax.websocket.OnError;import javax.websocket.OnMessage;import javax.websocket.OnOpen;import javax.websocket.Session;import javax.websocket.server.ServerEndpoint;/** * @ServerEndpoint 定义当前类为websocket服务器端口,参数是访问的路径 */@ServerEndpoint("/websocket")public class websocketTomcat {// 记录当前在线人数private static int onlineCount = 0;// CopyOnWriteArraySet是安全的set集合,用来存放客户端的每个websocket对象private static CopyOnWriteArraySet<websocketTomcat> set = new CopyOnWriteArraySet<websocketTomcat>();// 与客户端的连接会话private Session session;// 返回的信息private String message;// 当前用户的线程名字private String username;/** * 连接建立成功调用的方法 * @param session  可选的参数。session为与某个客户端的连接会话,需要通过它来给客户端发送数据 */@OnOpenpublic void onOpen(Session session) throws IOException {// 初始化sessionthis.session = session;// 将当前session添加到set集合中set.add(this);// 在线人数加1onlineCount++;// 修改当前线程的名字Thread.currentThread().setName("用户" + onlineCount);// 获取当前线程的名字username = Thread.currentThread().getName();// 编写返回的信息message = username + "登录成功,当前在线人数为:" + onlineCount + "人";// 群发消息for (websocketTomcat websocketTomcat : set) {websocketTomcat.session.getBasicRemote().sendText(message);}}/** * 连接关闭调用的方法 */@OnClosepublic void onClose() throws IOException {// 将当前session从set集合中移除set.remove(this);// 在线人数减1onlineCount--;// 编写返回的信息message = username + "下线成功,当前在线人数为:" + onlineCount + "人";// 判断set集合中是否还有websocket对象if (set.size() > 0) {// 群发消息for (websocketTomcat websocketTomcat : set) {websocketTomcat.session.getBasicRemote().sendText(message);}}}/** * 连接关闭调用的方法 */@OnErrorpublic void onError(Throwable error) {error.printStackTrace();}/** * 收到客户端消息后调用的方法 * @param message 客户端发送过来的消息 * @param session 可选的参数 */@OnMessagepublic void onMessage(String message) throws IOException {// 群发消息for (websocketTomcat websocketTomcat : set) {websocketTomcat.session.getBasicRemote().sendText(username + "说:" + message);}}}

4.测试

点击进入在线测试工具:websocket在线测试工具

输入websocket测试地址:ws://localhost:8080/websocket+tomcat/websocket

5.结果展示

分别打开该地址两次

第一个人登录:


第二个登录的时候:


然后就可以在对话框进行聊天了