Java Springboot webSocket简单实现,调接口推送消息到客户端socket

1.添加pom依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>

2.添加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();
	}
}

3.WebSocket业务逻辑类

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.PathParam;
import javax.websocket.server.ServerEndpoint;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@ServerEndpoint(value = "/webSocket/{userId}")
@Component
public class MyWebSocket {
	static Logger log = Logger.getLogger(MyWebSocket.class);
	// 静态变量,用来记录当前在线连接数。应该把它设计成线程安全的。
	private static int onlineCount = 0;
	// concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
	private static CopyOnWriteArraySet<MyWebSocket> webSocketSet = new CopyOnWriteArraySet<MyWebSocket>();
	// 与某个客户端的连接会话,需要通过它来给客户端发送数据
	private Session session;
	/** 接收userId */
	private String userId = "";
	private static Map<String, MyWebSocket> webSocketMap = new HashMap<>();

	/**
	 * 连接建立成功调用的方法
	 */
	@OnOpen
	public void onOpen(Session session, @PathParam("userId") String userId) {
		this.session = session;
		this.userId = userId;
		webSocketSet.add(this);
		if (webSocketMap.containsKey(userId)) {
			webSocketMap.remove(userId);
			webSocketMap.put(userId, this);
		} else {
			webSocketMap.put(userId, this);// 加入set中
			addOnlineCount();// 在线数加1
		}
		log.info(userId + "加入");
		log.info("当前在线人数为:" + getOnlineCount());
		try {
			sendMessage("欢迎" + userId + "加入");
		} catch (IOException e) {
			System.out.println("IO异常");
		}
	}

	/**
	 * 连接关闭调用的方法
	 */
	@OnClose
	public void onClose() {
		webSocketSet.remove(this); // 从set中删除
		subOnlineCount(); // 在线数减1
		System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
	}

	/**
	 * 收到客户端消息后调用的方法
	 *
	 * @param message 客户端发送过来的消息
	 */
	@OnMessage
	public void onMessage(String message, Session session) {
		message = "来自"+userId+"的消息:" + message;
		log.info("onMessage:"+message);
		// 群发消息
		for (MyWebSocket item : webSocketSet) {
			try {
				item.sendMessage(message);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 发生错误时调用
	 *
	 */
	@OnError
	public void onError(Session session, Throwable error) {
		System.out.println("发生错误");
		error.printStackTrace();
	}

	public void sendMessage(String message) throws IOException {
		this.session.getBasicRemote().sendText(message);
		// this.session.getAsyncRemote().sendText(message);
	}

	/**
	 * 群发自定义消息
	 */
	public static void sendInfo(String message) throws IOException {
		for (MyWebSocket item : webSocketSet) {
			try {
				item.sendMessage(message);
			} catch (IOException e) {
				continue;
			}
		}
	}

	/**
	 * 发送自定义消息
	 * 按userid发送消息
	 */
	public static void sendInfo(String message, @PathParam("userId") String userId) throws IOException {
		log.info("发送消息到:" + userId + ",报文:" + message);
		if (webSocketMap.containsKey(userId)) {
			webSocketMap.get(userId).sendMessage(message);
		} else {
			log.error("用户" + userId + ",不在线!");
		}
	}

	/**
	 * 获取在线客户端数量
	 * 
	 * @return
	 */
	public static synchronized int getOnlineCount() {
		return onlineCount;
	}

	/**
	 * 添加线上链接的客户端数量
	 */
	public static synchronized void addOnlineCount() {
		MyWebSocket.onlineCount++;
	}

	/**
	 * 客户端下载是调用将在线数量减1
	 */
	public static synchronized void subOnlineCount() {
		MyWebSocket.onlineCount--;
	}
}

4.web测试代码

<!DOCTYPE HTML>
<html>
<head>
    <title>My WebSocket</title>
</head>

<body>
Welcome<br/>
<input id="text" type="text" /><button onclick="send()">Send</button>    <button onclick="closeWebSocket()">Close</button>
<div id="message">
</div>
</body>

<script type="text/javascript">
    var websocket = null;

    //判断当前浏览器是否支持WebSocket
    if('WebSocket' in window){
        websocket = new WebSocket("ws://localhost:8888/websocket");
    }
    else{
        alert('Not support websocket')
    }

    //连接发生错误的回调方法
    websocket.onerror = function(){
        setMessageInnerHTML("error");
    };

    //连接成功建立的回调方法
    websocket.onopen = function(event){
        setMessageInnerHTML("open");
    }

    //接收到消息的回调方法
    websocket.onmessage = function(event){
        setMessageInnerHTML(event.data);
    }

    //连接关闭的回调方法
    websocket.onclose = function(){
        setMessageInnerHTML("close");
    }

    //监听窗口关闭事件,当窗口关闭时,主动去关闭websocket连接,防止连接还没断开就关闭窗口,server端会抛异常。
    window.onbeforeunload = function(){
        websocket.close();
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML){
        document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }

    //关闭连接
    function closeWebSocket(){
        websocket.close();
    }

    //发送消息
    function send(){
        var message = document.getElementById('text').value;
        websocket.send(message);
    }
</script>
</html>

5.在接口调用MyWebsocket中的sendInfo方法发送消息到客户端

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.UserInfo;
import com.example.demo.service.MyService;
import com.example.demo.websocket.MyWebSocket;
import com.google.gson.Gson;

@Controller
@RequestMapping(value = "/test")
public class MyController {

	@Autowired
	private MyService Service;

	@ResponseBody
	@RequestMapping(value = "/hello", method = RequestMethod.GET)
	public String test() {
		Gson gson = new Gson();
		try {
			MyWebSocket.sendInfo("大家好");//发送消息到clientsocket
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map = new HashMap<>();
		map.put("result","Hello");
		return gson.toJson(map);
	}
}

6.实现效果
在这里插入图片描述

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: 好的,首先你需要在你的 Spring Boot 应用中引入对 WebSocket 的支持。你可以在 pom.xml 中添加如下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 然后,你需要定义一个 WebSocket 服务端点。这可以通过实现 Spring 的 WebSocketHandler 接口来完成。例如: ``` @Component public class MyWebSocketHandler implements WebSocketHandler { @Override public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception { // 处理从客户端接收到的消息 } @Override public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception { // 处理连接出错的情况 } @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { // 处理新的 WebSocket 连接 } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception { // 处理连接断开的情况 } } ``` 接着,你需要在 Spring Boot 应用的配置类中(通常是一个带有 @Configuration 注解的类)中配置 WebSocket 服务端点,并为它指定一个 URL 映射。例如: ``` @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Autowired private MyWebSocketHandler webSocketHandler; @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(webSocketHandler, "/ws").setAllowedOrigins("*"); } } ``` 最后,在你的 Vue 应用中使用 JavaScript 的 WebSocket API 与你的 Spring Boot 应用建立 WebSocket 连接即可。例如: ``` const socket = new WebSocket('ws://localhost:8080/ws'); // 处理连接建立的情况 socket.onopen ### 回答2: 要实现在Spring Boot和Vue中使用WebSocket进行后台消息推送,需要完成以下步骤: 1. 在Spring Boot中添加WebSocket支持。在Spring Boot的依赖管理中添加`spring-boot-starter-websocket`依赖,并创建一个WebSocket配置类。 ```java @Configuration @EnableWebSocket public class WebSocketConfig implements WebSocketConfigurer { @Override public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) { registry.addHandler(new WebSocketHandler(), "/websocket").setAllowedOrigins("*"); } } ``` 2. 创建一个WebSocket处理器,处理WebSocket的连接、消息发送和断开连接等操作。 ```java public class WebSocketHandler extends TextWebSocketHandler { private static final Set<WebSocketSession> sessions = ConcurrentHashMap.newKeySet(); @Override public void afterConnectionEstablished(WebSocketSession session) throws Exception { sessions.add(session); } @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { // 处理接收到的消息 } @Override public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception { sessions.remove(session); } public static void sendMessageToAll(String message) throws IOException { for (WebSocketSession session : sessions) { session.sendMessage(new TextMessage(message)); } } } ``` 3. 在Vue中使用WebSocket连接后台,并处理接收到的消息。 ```javascript const websocket = new WebSocket('ws://localhost:8080/websocket'); websocket.onopen = function() { console.log('WebSocket连接已建立'); }; websocket.onmessage = function(event) { const message = event.data; // 处理接收到的消息 }; websocket.onclose = function() { console.log('WebSocket连接已关闭'); }; websocket.onerror = function() { console.log('WebSocket连接出错'); }; function sendWebSocketMessage(message) { websocket.send(message); } ``` 4. 实现后台消息推送的功能。在Spring Boot的任意位置用`WebSocketHandler.sendMessageToAll(message)`方法,该方法将消息发送给已连接的所有WebSocket客户端。 ```java @RestController public class MessageController { @GetMapping("/push-message") public void pushMessage() throws IOException { WebSocketHandler.sendMessageToAll("Hello, World!"); } } ``` 以上就是通过WebSocket在Spring Boot和Vue中实现后台消息推送的简要步骤。根据实际需求,还可以对连接进行身份验证、消息转发等扩展。 ### 回答3: 要在Spring Boot和Vue中实现后台消息推送,可以使用WebSocket技术。 首先,在Spring Boot中配置WebSocket,可以使用Spring提供的注解@EnableWebSocket和@WebSocket实现。在配置类上加上@EnableWebSocket注解,然后创建一个WebSocket处理类,使用@WebSocket注解标注,并实现WebSocketHandler接口。在WebSocket处理类中,重写WebSocketHandler接口中的方法,例如handleMessage、afterConnectionEstablished等,用于处理消息和建立连接等操作。在handleMessage方法中,可以进行消息推送的逻辑处理。 接下来,在Vue中使用WebSocket连接到Spring Boot后台。在Vue中,可以使用Vue插件vue-websocket来连接WebSocket。首先,安装vue-websocket插件,然后在Vue项目的入口文件(通常是main.js)中,通过Vue.use()方法来引入插件并进行配置,设置WebSocket连接的地址和相关参数。在Vue组件中,可以使用this.$socket.send()方法发送消息,使用this.$socket.on()方法监听消息。 通过以上步骤,就可以实现后台消息推送功能了。当有新消息需要推送时,后台可以通过WebSocket发送消息给前端,前端接收到消息后可以进行相应的处理和展示。 注意,为了确保通讯的稳定性和安全性,可能需要进行一些额外的配置和处理,例如设置WebSocket消息的格式、进行身份认证等。 总结起来,实现后台消息推送的步骤如下: 1. 在Spring Boot中配置WebSocket,创建WebSocket处理类处理消息推送逻辑。 2. 在Vue中安装vue-websocket插件,配置WebSocket连接信息。 3. 在Vue组件中发送和接收WebSocket消息,进行相应的处理和展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值