websocket+tomcat实现聊天室功能

本文不介绍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为与某个客户端的连接会话,需要通过它来给客户端发送数据
	 */
	@OnOpen
	public void onOpen(Session session) throws IOException {
		// 初始化session
		this.session = session;
		// 将当前session添加到set集合中
		set.add(this);				
		// 在线人数加1
		onlineCount++;
		// 修改当前线程的名字
		Thread.currentThread().setName("用户" + onlineCount);
		// 获取当前线程的名字
		username = Thread.currentThread().getName();
		// 编写返回的信息
		message = username + "登录成功,当前在线人数为:" + onlineCount + "人";
		// 群发消息
		for (websocketTomcat websocketTomcat : set) {
			websocketTomcat.session.getBasicRemote().sendText(message);
		}
	}

	/**
	 * 连接关闭调用的方法
	 */
	@OnClose
	public void onClose() throws IOException {
		// 将当前session从set集合中移除
		set.remove(this);
		// 在线人数减1
		onlineCount--;
		// 编写返回的信息
		message = username + "下线成功,当前在线人数为:" + onlineCount + "人";
		// 判断set集合中是否还有websocket对象
		if (set.size() > 0) {
			// 群发消息
			for (websocketTomcat websocketTomcat : set) {
				websocketTomcat.session.getBasicRemote().sendText(message);
			}
		}
	}

	/**
	 * 连接关闭调用的方法
	 */
	@OnError
	public void onError(Throwable error) {
		error.printStackTrace();
	}

	/**
	 * 收到客户端消息后调用的方法
	 * @param message 客户端发送过来的消息
	 * @param session 可选的参数
	 */
	@OnMessage
	public 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.结果展示

分别打开该地址两次

第一个人登录:


第二个登录的时候:


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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值