博客
关于我
Springboot2模块系列:websocket(即时消息推送)
阅读量:253 次
发布时间:2019-03-01

本文共 5641 字,大约阅读时间需要 18 分钟。

Spring Boot WebSocket 实时消息系统配置与实现

1. 配置基础

1.1 加载静态文件

pom.xml 中添加 WebSocket 和 Thymeleaf 组件:

org.springframework.boot
spring-boot-starter-websocket
org.springframework.boot
spring-boot-starter-thymeleaf

1.2 WebSocket 配置

WebSocketConfig 类中启用 WebSocket 支持:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

2. WebSocket 路由配置

2.1 WebSocket 服务器配置

WebSocketServerConfig 类中实现 WebSocket 服务器功能:

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.PathParam;
import javax.websocket.server.ServerEndpoint;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@ServerEndpoint("/imserver/{userId}")
public class WebSocketServerConfig {
private static Logger log = LoggerFactory.getLogger(WebSocketServerConfig.class);
private static ConcurrentHashMap
webSocketMap = new ConcurrentHashMap<>();
private Session session;
private String userId;
@OnOpen
public void onOpen(Session session, @PathParam("userId") String userId) {
this.session = session;
this.userId = userId;
if (webSocketMap.containsKey(userId)) {
webSocketMap.remove(userId);
}
webSocketMap.put(userId, this);
addOnlineCount();
sendMessage("我在线上");
}
@OnClose
public void onClose() {
if (webSocketMap.containsKey(userId)) {
webSocketMap.remove(userId);
subOnlineCount();
}
}
@OnMessage
public void onMessage(String message, Session session) {
JSONObject jsonObject = JSON.parseObject(message);
String toUserId = jsonObject.getString("toUserId");
if (!StringUtils.isBlank(toUserId) && webSocketMap.containsKey(toUserId)) {
WebSocketServerConfig.sendInfo(message, toUserId);
} else {
log.error("用户 " + toUserId + " 不在线!");
}
}
@OnError
public void onError(Session session, Throwable error) {
log.error("用户错误:" + this.userId + ", 原因:" + error.getMessage());
error.printStackTrace();
}
public static void sendMessage(String message, @PathParam("userId") String userId) throws IOException {
WebSocketServerConfig.sendInfo(message, userId);
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
onlineCount++;
}
public static synchronized void subOnlineCount() {
onlineCount--;
}
}

3. WebSocket 消息处理

3.1 前端 WebSocket 连接

在 HTML 页面中实现 WebSocket 连接:

var socket;
function openSocket() {
if (typeof WebSocket === "undefined") {
alert("您的浏览器不支持 WebSocket");
} else {
var socketUrl = "ws://localhost:10106/imserver/" + $("#userId").val();
socket = new WebSocket(socketUrl);
socket.onopen = function() {
console.log("WebSocket 已打开");
};
socket.onmessage = function(msg) {
var contentText = msg.data;
talking(contentText);
};
socket.onclose = function() {
console.log("WebSocket 已关闭");
};
socket.onerror = function() {
console.log("WebSocket 发生了错误");
};
}
}

3.2 发送消息

实现消息发送功能:

function sendMessage() {
if (typeof WebSocket === "undefined") {
alert("您的浏览器不支持 WebSocket");
} else {
var toUserId = $("#toUserId").val();
var message = JSON.stringify({
toUserId: toUserId,
contentText: $("#contentText").val()
});
socket.send(message);
var username = $("#userId").val();
var sendhistory = $("#contentText").val();
document.getElementById("content").append(username + ":" + sendhistory + "\r\n");
document.getElementById("contentText").val("");
}
}
function talking(content) {
var toUsername = document.getElementById("toUserId").value;
document.getElementById("content").append(toUsername + ":" + content + "\r\n");
}

4. 接口实现

4.1 推送消息接口

实现接口发送消息:

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MsgController {
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/websocket")
public class MsgController {
@GetMapping("/instance/message")
public String page() {
return "websocket/chatpage";
}
@RequestMapping(value = "/push/{toUserId}", method = RequestMethod.POST)
public Map
pushToWeb(@RequestBody Map
params, @PathVariable String toUserId) throws IOException {
String message = params.get("message");
WebSocketServerConfig.sendInfo(message, toUserId);
Map
returnMap = new HashMap<>();
returnMap.put("code", "200");
returnMap.put("msg", "成功发送消息");
return returnMap;
}
}
}

5. 消息记录展示

5.1 前端显示

在 HTML 页面中展示消息记录:

消息记录

通过以上配置和实现,可以实现一个基于 Spring Boot 的 WebSocket 实时消息系统,支持用户之间实时通信。

转载地址:http://npht.baihongyu.com/

你可能感兴趣的文章
Objective-C实现乘方运算---m的n次方(附完整源码)
查看>>
Objective-C实现二分查找最接近的数值m(附完整源码)
查看>>
Objective-C实现二叉树层序遍历(附完整源码)
查看>>
Objective-C实现二叉树遍历算法(附完整源码)
查看>>
Objective-C实现二次方程复数算法(附完整源码)
查看>>
Objective-C实现二进制和算法(附完整源码)
查看>>
Objective-C实现二进制移位算法(附完整源码)
查看>>
Objective-C实现二进制补码算法(附完整源码)
查看>>
Objective-C实现二进制计数尾随零算法(附完整源码)
查看>>
Objective-C实现二进制转八进制算法(附完整源码)
查看>>
Objective-C实现互斥锁同步执行两个线程函数(附完整源码)
查看>>
Objective-C实现交易密码算法(附完整源码)
查看>>
Objective-C实现亨元模式(附完整源码)
查看>>
Objective-C实现人工势场法(附完整源码)
查看>>
Objective-C实现代理服务器(附完整源码)
查看>>
Objective-C实现令牌桶算法(附完整源码)
查看>>
Objective-C实现以递归的形式MatrixExponentiation矩阵求幂算法 (附完整源码)
查看>>
Objective-C实现仿射密码算法(附完整源码)
查看>>
Objective-C实现优先级调度算法(附完整源码)
查看>>
Objective-C实现优先队列算法(附完整源码)
查看>>