Spring Boot实战之netty-socketio实现简单聊天室(给指定用户推送消息)
发布时间 - 2026-01-11 00:12:36 点击率:次网上好多例子都是群发的,本文实现一对一的发送,给指定客户端进行消息推送

1、本文使用到netty-socketio开源库,以及MySQL,所以首先在pom.xml中添加相应的依赖库
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2、修改application.properties, 添加端口及主机数据库连接等相关配置,
wss.server.port=8081 wss.server.host=localhost spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springlearn spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver # Specify the DBMS spring.jpa.database = MYSQL # Show or not log for each sql query spring.jpa.show-sql = true # Hibernate ddl auto (create, create-drop, update) spring.jpa.hibernate.ddl-auto = update # Naming strategy spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy # stripped before adding them to the entity manager) spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
3、修改Application文件,添加nettysocket的相关配置信息
package com.xiaofangtech.sunt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.corundumstudio.socketio.AuthorizationListener;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.HandshakeData;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
@SpringBootApplication
public class NettySocketSpringApplication {
@Value("${wss.server.host}")
private String host;
@Value("${wss.server.port}")
private Integer port;
@Bean
public SocketIOServer socketIOServer()
{
Configuration config = new Configuration();
config.setHostname(host);
config.setPort(port);
//该处可以用来进行身份验证
config.setAuthorizationListener(new AuthorizationListener() {
@Override
public boolean isAuthorized(HandshakeData data) {
//http://localhost:8081?username=test&password=test
//例如果使用上面的链接进行connect,可以使用如下代码获取用户密码信息,本文不做身份验证
// String username = data.getSingleUrlParam("username");
// String password = data.getSingleUrlParam("password");
return true;
}
});
final SocketIOServer server = new SocketIOServer(config);
return server;
}
@Bean
public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketServer) {
return new SpringAnnotationScanner(socketServer);
}
public static void main(String[] args) {
SpringApplication.run(NettySocketSpringApplication.class, args);
}
}
4、添加消息结构类MessageInfo.java
package com.xiaofangtech.sunt.message;
public class MessageInfo {
//源客户端id
private String sourceClientId;
//目标客户端id
private String targetClientId;
//消息类型
private String msgType;
//消息内容
private String msgContent;
public String getSourceClientId() {
return sourceClientId;
}
public void setSourceClientId(String sourceClientId) {
this.sourceClientId = sourceClientId;
}
public String getTargetClientId() {
return targetClientId;
}
public void setTargetClientId(String targetClientId) {
this.targetClientId = targetClientId;
}
public String getMsgType() {
return msgType;
}
public void setMsgType(String msgType) {
this.msgType = msgType;
}
public String getMsgContent() {
return msgContent;
}
public void setMsgContent(String msgContent) {
this.msgContent = msgContent;
}
}
5、添加客户端信息,用来存放客户端的sessionid
package com.xiaofangtech.sunt.bean;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name="t_clientinfo")
public class ClientInfo {
@Id
@NotNull
private String clientid;
private Short connected;
private Long mostsignbits;
private Long leastsignbits;
private Date lastconnecteddate;
public String getClientid() {
return clientid;
}
public void setClientid(String clientid) {
this.clientid = clientid;
}
public Short getConnected() {
return connected;
}
public void setConnected(Short connected) {
this.connected = connected;
}
public Long getMostsignbits() {
return mostsignbits;
}
public void setMostsignbits(Long mostsignbits) {
this.mostsignbits = mostsignbits;
}
public Long getLeastsignbits() {
return leastsignbits;
}
public void setLeastsignbits(Long leastsignbits) {
this.leastsignbits = leastsignbits;
}
public Date getLastconnecteddate() {
return lastconnecteddate;
}
public void setLastconnecteddate(Date lastconnecteddate) {
this.lastconnecteddate = lastconnecteddate;
}
}
6、添加查询数据库接口ClientInfoRepository.java
package com.xiaofangtech.sunt.repository;
import org.springframework.data.repository.CrudRepository;
import com.xiaofangtech.sunt.bean.ClientInfo;
public interface ClientInfoRepository extends CrudRepository<ClientInfo, String>{
ClientInfo findClientByclientid(String clientId);
}
7、添加消息处理类MessageEventHandler.Java
package com.xiaofangtech.sunt.message;
import java.util.Date;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.xiaofangtech.sunt.bean.ClientInfo;
import com.xiaofangtech.sunt.repository.ClientInfoRepository;
@Component
public class MessageEventHandler
{
private final SocketIOServer server;
@Autowired
private ClientInfoRepository clientInfoRepository;
@Autowired
public MessageEventHandler(SocketIOServer server)
{
this.server = server;
}
//添加connect事件,当客户端发起连接时调用,本文中将clientid与sessionid存入数据库
//方便后面发送消息时查找到对应的目标client,
@OnConnect
public void onConnect(SocketIOClient client)
{
String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
ClientInfo clientInfo = clientInfoRepository.findClientByclientid(clientId);
if (clientInfo != null)
{
Date nowTime = new Date(System.currentTimeMillis());
clientInfo.setConnected((short)1);
clientInfo.setMostsignbits(client.getSessionId().getMostSignificantBits());
clientInfo.setLeastsignbits(client.getSessionId().getLeastSignificantBits());
clientInfo.setLastconnecteddate(nowTime);
clientInfoRepository.save(clientInfo);
}
}
//添加@OnDisconnect事件,客户端断开连接时调用,刷新客户端信息
@OnDisconnect
public void onDisconnect(SocketIOClient client)
{
String clientId = client.getHandshakeData().getSingleUrlParam("clientid");
ClientInfo clientInfo = clientInfoRepository.findClientByclientid(clientId);
if (clientInfo != null)
{
clientInfo.setConnected((short)0);
clientInfo.setMostsignbits(null);
clientInfo.setLeastsignbits(null);
clientInfoRepository.save(clientInfo);
}
}
//消息接收入口,当接收到消息后,查找发送目标客户端,并且向该客户端发送消息,且给自己发送消息
@OnEvent(value = "messageevent")
public void onEvent(SocketIOClient client, AckRequest request, MessageInfo data)
{
String targetClientId = data.getTargetClientId();
ClientInfo clientInfo = clientInfoRepository.findClientByclientid(targetClientId);
if (clientInfo != null && clientInfo.getConnected() != 0)
{
UUID uuid = new UUID(clientInfo.getMostsignbits(), clientInfo.getLeastsignbits());
System.out.println(uuid.toString());
MessageInfo sendData = new MessageInfo();
sendData.setSourceClientId(data.getSourceClientId());
sendData.setTargetClientId(data.getTargetClientId());
sendData.setMsgType("chat");
sendData.setMsgContent(data.getMsgContent());
client.sendEvent("messageevent", sendData);
server.getClient(uuid).sendEvent("messageevent", sendData);
}
}
}
8、添加ServerRunner.java
package com.xiaofangtech.sunt.message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.corundumstudio.socketio.SocketIOServer;
@Component
public class ServerRunner implements CommandLineRunner {
private final SocketIOServer server;
@Autowired
public ServerRunner(SocketIOServer server) {
this.server = server;
}
@Override
public void run(String... args) throws Exception {
server.start();
}
}
9、工程结构
10、运行测试
1) 添加基础数据,数据库中预置3个客户端testclient1,testclient2,testclient3
2) 创建客户端文件index.html,index2.html,index3.html分别代表testclient1 testclient2 testclient3三个用户
本文直接修改的https://github.com/mrniko/netty-socketio-demo/tree/master/client 中的index.html文件
其中clientid为发送者id, targetclientid为目标方id,本文简单的将发送方和接收方写死在html文件中
使用 以下代码进行连接
io.connect('http://localhost:8081?clientid='+clientid);
index.html 文件内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Demo Chat</title>
<link href="bootstrap.css" rel="external nofollow" rel="stylesheet">
<style>
body {
padding:20px;
}
#console {
height: 400px;
overflow: auto;
}
.username-msg {color:orange;}
.connect-msg {color:green;}
.disconnect-msg {color:red;}
.send-msg {color:#888}
</style>
<script src="js/socket.io/socket.io.js"></script>
<script src="js/moment.min.js"></script>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script>
var clientid = 'testclient1';
var targetClientId= 'testclient2';
var socket = io.connect('http://localhost:8081?clientid='+clientid);
socket.on('connect', function() {
output('<span class="connect-msg">Client has connected to the server!</span>');
});
socket.on('messageevent', function(data) {
output('<span class="username-msg">' + data.sourceClientId + ':</span> ' + data.msgContent);
});
socket.on('disconnect', function() {
output('<span class="disconnect-msg">The client has disconnected!</span>');
});
function sendDisconnect() {
socket.disconnect();
}
function sendMessage() {
var message = $('#msg').val();
$('#msg').val('');
var jsonObject = {sourceClientId: clientid,
targetClientId: targetClientId,
msgType: 'chat',
msgContent: message};
socket.emit('messageevent', jsonObject);
}
function output(message) {
var currentTime = "<span class='time'>" + moment().format('HH:mm:ss.SSS') + "</span>";
var element = $("<div>" + currentTime + " " + message + "</div>");
$('#console').prepend(element);
}
$(document).keydown(function(e){
if(e.keyCode == 13) {
$('#send').click();
}
});
</script>
</head>
<body>
<h1>Netty-socketio Demo Chat</h1>
<br/>
<div id="console" class="well">
</div>
<form class="well form-inline" onsubmit="return false;">
<input id="msg" class="input-xlarge" type="text" placeholder="Type something..."/>
<button type="button" onClick="sendMessage()" class="btn" id="send">Send</button>
<button type="button" onClick="sendDisconnect()" class="btn">Disconnect</button>
</form>
</body>
</html>
3、本例测试时
testclient1 发送消息给 testclient2
testclient2 发送消息给 testclient1
testclient3发送消息给testclient1
运行结果如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# springboot
# socketio
# netty
# 整合netty
# JAVA Netty实现聊天室+私聊功能的示例代码
# java基于netty NIO的简单聊天室的实现
# Java实战之用springboot+netty实现简单的一对一聊天
# 基于Java的Socket多客户端Client-Server聊天程序的实现
# 从入门到超神进阶的Netty群聊系统
# 客户端
# 发送消息
# 身份验证
# 都是
# 给自己
# 不做
# 可以使用
# 死在
# 数据库中
# 开源
# 大家多多
# 可以用来
# 本例
# 向该
# 网上
# setHostname
# config
# setPort
# button
# onClick
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
jQuery validate插件功能与用法详解
JavaScript中如何操作剪贴板_ClipboardAPI怎么用
如何快速生成高效建站系统源代码?
大学网站设计制作软件有哪些,如何将网站制作成自己app?
Laravel怎么实现模型属性转换Casting_Laravel自动将JSON字段转为数组【技巧】
canvas 画布在主流浏览器中的尺寸限制详细介绍
深圳网站制作平台,深圳市做网站好的公司有哪些?
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
如何用手机制作网站和网页,手机移动端的网站能制作成中英双语的吗?
如何用狗爹虚拟主机快速搭建网站?
Laravel如何使用Eloquent ORM进行数据库操作?(CRUD示例)
Laravel如何实现登录错误次数限制_Laravel自带LoginThrottles限流配置【方法】
C#如何调用原生C++ COM对象详解
如何在万网主机上快速搭建网站?
Laravel如何实现用户角色和权限系统_Laravel角色权限管理机制
Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程
Laravel如何配置Horizon来管理队列?(安装和使用)
Edge浏览器怎么启用睡眠标签页_节省电脑内存占用优化技巧
Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧
Laravel如何创建自定义Artisan命令?(代码示例)
网站制作软件免费下载安装,有哪些免费下载的软件网站?
网站制作大概多少钱一个,做一个平台网站大概多少钱?
历史网站制作软件,华为如何找回被删除的网站?
Laravel Seeder填充数据教程_Laravel模型工厂Factory使用
Zeus浏览器网页版官网入口 宙斯浏览器官网在线通道
如何快速搭建高效服务器建站系统?
详解Oracle修改字段类型方法总结
Laravel如何编写单元测试和功能测试?(PHPUnit示例)
如何快速搭建高效WAP手机网站吸引移动用户?
Laravel如何记录日志_Laravel Logging系统配置与自定义日志通道
百度输入法ai面板怎么关 百度输入法ai面板隐藏技巧
油猴 教程,油猴搜脚本为什么会网页无法显示?
Win11怎么更改系统语言为中文_Windows11安装语言包并设为显示语言
极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?
HTML5空格和nbsp有啥关系_nbsp的作用及使用场景【说明】
如何在Ubuntu系统下快速搭建WordPress个人网站?
如何用花生壳三步快速搭建专属网站?
企业在线网站设计制作流程,想建设一个属于自己的企业网站,该如何去做?
学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?
如何快速查询域名建站关键信息?
详解jQuery中的事件
php静态变量怎么调试_php静态变量作用域调试技巧【解答】
Python函数文档自动校验_规范解析【教程】
用yum安装MySQLdb模块的步骤方法
如何用IIS7快速搭建并优化网站站点?
如何在七牛云存储上搭建网站并设置自定义域名?
Laravel怎么实现模型属性的自动加密
Midjourney怎么调整光影效果_Midjourney光影调整方法【指南】
javascript基于原型链的继承及call和apply函数用法分析
如何在阿里云通过域名搭建网站?

