Springboot基于websocket实现简单在线聊天功能
更新时间:2020年06月24日 10:54:29 作者:yytxdy
这篇文章主要介绍了Springboot基于websocket实现简单在线聊天功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
添加maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springboot-websocket</artifactId>
<name>springboot-websocket</name>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>
</project>
添加websocket配置
@Configuration
@EnableWebSocket
public class MyWebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(myHandler(), "/myHandler").setAllowedOrigins("*");
}
@Bean
public WebSocketHandler myHandler() {
return new MyTextWebSocketHandler();
}
}
实现具体的handler
public class MyTextWebSocketHandler extends TextWebSocketHandler {
private Set<WebSocketSession> sessions = new HashSet<>();
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
if (session.isOpen()) {
sessions.add(session);
}
sendToAll(message);
}
private void sendToAll(TextMessage message) throws IOException {
for (WebSocketSession session : sessions) {
session.sendMessage(message);
}
}
@Override
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
sessions.remove(session);
}
}
即可通过ws://localhost:8080/myHandler访问websocket
添加测试页面:
<html>
<script type="text/javascript">
if ("WebSocket" in window) {
var ws = new WebSocket("ws://localhost:8080/myHandler");
ws.onopen = function () {
};
ws.onmessage = function (evt) {
document.getElementById('messageDiv').innerHTML += evt.data + "</br>";
};
ws.onclose = function () {
console.log("close connect");
};
} else {
alert("您的浏览器不支持 WebSocket!");
}
function send() {
ws.send(document.getElementById("input").value + ": " + document.getElementById("message").value);
}
</script>
</head>
<body>
当前用户: <input id="input"/><br/>
<a href="#" rel="external nofollow" onclick="send();">发送消息</a>: <input id="message"/>
<div id="messageDiv"></div>
</body>
</html>
即可实现简单的通信功能
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
深入理解springboot中配置文件application.properties
本文主要介绍了springboot中配置文件application.properties,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2021-10-10
Installij IDEA install或clean项目的使用
这篇文章主要介绍了Installij IDEA install或clean项目的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2024-08-08
Java inputstream和outputstream使用详解
这篇文章主要介绍了Java inputstream和outputstream使用详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下2021-08-08


最新评论