springboot整合websocket
服务端:
1.添加依赖
<!--websocket依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
2.
采用springboot内置容器启动项目的,则需要配置一个Bean,如果用外部容器,则不需要配置
package com.mybatis.demo.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import org.springframework.web.socket.server.standard.ServerEndpointExporter; /** * @author shafei * @version 1.0 * @date 8:24 2019/8/24 * @fun websocket启动配置 */ @Component public class WebSocketConfig { /** * ServerEndpointExporter 作用 * * 这个Bean会自动注册使用@ServerEndpoint注解声明的websocket endpoint * * @return */ @Bean public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } }
3.核心代码,监听客户端,收发消息
package com.mybatis.demo.demo; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.websocket.server.ServerEndpoint; import javax.websocket.OnClose; import javax.websocket.OnMessage; import javax.websocket.OnOpen; import javax.websocket.Session; import javax.websocket.server.PathParam; import java.util.concurrent.ConcurrentHashMap; /** * @author shafei * @version 1.0 * @date 8:26 2019/8/24 * @fun */ @Slf4j @Component @ServerEndpoint("/websocket/{name}") public class WebSocket { /** * 与某个客户端的连接对话,需要通过它来给客户端发送消息 */ private Session session; /** * 标识当前连接客户端的用户名 */ private String name; /** * 用于存所有的连接服务的客户端,这个对象存储是安全的 */ private static ConcurrentHashMap<String,WebSocket> webSocketSet = new ConcurrentHashMap<>(); @OnOpen public void OnOpen(Session session, @PathParam(value = "name") String name){ this.session = session; this.name = name; // name是用来表示唯一客户端,如果需要指定发送,需要指定发送通过name来区分 webSocketSet.put(name,this); log.info("[WebSocket] 连接成功,当前连接客户端数为:={}",webSocketSet.size()); } @OnClose public void OnClose(){ webSocketSet.remove(this.name); log.info("[WebSocket] 退出成功,当前连接客户端数为:={}",webSocketSet.size()); } @OnMessage public void OnMessage(String message){ log.info("[WebSocket] 收到消息:{}",message); //判断是否需要指定发送,具体规则自定义 if(message.indexOf("TOUSER") == 0){ String name = message.substring(message.indexOf("TOUSER")+6,message.indexOf(";")); AppointSending(name,message.substring(message.indexOf(";")+1,message.length())); }else{ GroupSending(message); } } /** * 群发 * @param message */ public void GroupSending(String message){ for (String name : webSocketSet.keySet()){ try { webSocketSet.get(name).session.getBasicRemote().sendText(message); }catch (Exception e){ e.printStackTrace(); } } } /** * 指定发送 * @param name * @param message */ public void AppointSending(String name,String message){ try { webSocketSet.get(name).session.getBasicRemote().sendText(message); }catch (Exception e){ e.printStackTrace(); } } }
备注:
@Slf4j这个注解坐标:
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
4.控制层测试发送消息,当然这一步需要将客户端连接到我们的这个服务端,客户端稍后下面会写
@Controller @RequestMapping("/test") public class TestController { @Autowired private WebSocket webSocket; @GetMapping("/sendMessage") @ResponseBody public void sendMessage(){ // AppointSending("") webSocket.AppointSending("test","shafei"); } }
客户端:
1.客户端依赖:
<!--websocket作为客户端-->
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.5</version>
</dependency>
2.连接服务端和接收消息的配置类
package com.mesboot.doc.config.websocket; import lombok.extern.slf4j.Slf4j; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft_6455; import org.java_websocket.handshake.ServerHandshake; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import java.net.URI; /** * @author shafei * @version 1.0 * @date 8:31 2019/8/24 * @fun */ @Slf4j @Component public class WebSocketConfig { @Bean public WebSocketClient webSocketClient() { try { WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://localhost:8089/mybatisDemo/websocket/test"),new Draft_6455()) { @Override public void onOpen(ServerHandshake handshakedata) { log.info("[websocket] 连接成功"); } @Override public void onMessage(String message) { log.info("[websocket] 收到消息={}",message); } @Override public void onClose(int code, String reason, boolean remote) { log.info("[websocket] 退出连接"); } @Override public void onError(Exception ex) { log.info("[websocket] 连接错误={}",ex.getMessage()); } }; webSocketClient.connect(); return webSocketClient; } catch (Exception e) { e.printStackTrace(); } return null; } }
以上的服务端和客户端是可以写在两个程序里面,互不干扰;
参考代码:https://blog.csdn.net/weixin_38111957/article/details/86352677