diff --git a/pom.xml b/pom.xml index df88023..3dc989e 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,11 @@ 4.1.2 + org.java-websocket + Java-WebSocket + 1.3.5 + + io.jsonwebtoken jjwt 0.9.0 diff --git a/src/main/java/com/iflytop/nuclear/NuclearApplication.java b/src/main/java/com/iflytop/nuclear/NuclearApplication.java index 350e953..69776b8 100644 --- a/src/main/java/com/iflytop/nuclear/NuclearApplication.java +++ b/src/main/java/com/iflytop/nuclear/NuclearApplication.java @@ -1,20 +1,14 @@ package com.iflytop.nuclear; -import com.iflytop.nuclear.websocket.WebSocketClient; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.Banner; import org.springframework.boot.ResourceBanner; -import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.core.io.ClassPathResource; -import javax.websocket.ContainerProvider; import javax.websocket.DeploymentException; -import javax.websocket.Session; -import javax.websocket.WebSocketContainer; import java.io.IOException; -import java.net.URI; @SpringBootApplication @MapperScan("com.iflytop.nuclear.mapper") diff --git a/src/main/java/com/iflytop/nuclear/config/WebSocketConfig.java b/src/main/java/com/iflytop/nuclear/config/WebSocketConfig.java new file mode 100644 index 0000000..47bf140 --- /dev/null +++ b/src/main/java/com/iflytop/nuclear/config/WebSocketConfig.java @@ -0,0 +1,47 @@ +package com.iflytop.nuclear.config; + +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; + +@Component +@Slf4j +public class WebSocketConfig { + @Bean + public WebSocketClient webSocketClient() { + try { + WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://localhost:12306"), 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; + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/nuclear/controller/TestController.java b/src/main/java/com/iflytop/nuclear/controller/TestController.java new file mode 100644 index 0000000..645328c --- /dev/null +++ b/src/main/java/com/iflytop/nuclear/controller/TestController.java @@ -0,0 +1,24 @@ +package com.iflytop.nuclear.controller; + +/** + * @author cool + * @date 2023/7/6 15:31 + */ + +import com.iflytop.nuclear.websocket.SocketClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +@RestController +@RequestMapping("/websocket") +public class TestController { + + @Autowired + private SocketClient webScoketClient; + + @GetMapping("/sendMessage") + public void sendMessage(){ + webScoketClient.groupSending("123"); + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/nuclear/websocket/SocketClient.java b/src/main/java/com/iflytop/nuclear/websocket/SocketClient.java new file mode 100644 index 0000000..7998db1 --- /dev/null +++ b/src/main/java/com/iflytop/nuclear/websocket/SocketClient.java @@ -0,0 +1,15 @@ +package com.iflytop.nuclear.websocket; + +import org.java_websocket.client.WebSocketClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class SocketClient { + @Autowired + private WebSocketClient webSocketClient; + + public void groupSending(String message) { + webSocketClient.send(message+"---6666"); + } +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/nuclear/websocket/WebSocketClient.java b/src/main/java/com/iflytop/nuclear/websocket/WebSocketClient.java deleted file mode 100644 index cc59b76..0000000 --- a/src/main/java/com/iflytop/nuclear/websocket/WebSocketClient.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.iflytop.nuclear.websocket; - -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; -import com.iflytop.nuclear.handler.MessageHandler; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import org.springframework.util.StringUtils; - -import javax.websocket.*; -import java.io.IOException; -import java.net.URI; - -/** - * @author cool - * @date 2023/7/4 19:12 - */ -@ClientEndpoint -@Component -@Slf4j -public class WebSocketClient { - - @Autowired - MessageHandler messageHandler; - - private Session session; - - @OnOpen - public void onOpen(Session session) { - this.session = session; - } - - // WebSocketContainer container = ContainerProvider.getWebSocketContainer(); - // String uri ="ws://127.0.0.1:8888/websocket/123"; - // container.connectToServer(WebSocketClient.class, URI.create(uri)); - - @OnMessage - public void onMessage(String message) throws IOException { - if (!StringUtils.isEmpty(message)) { - try { - JSONObject jsonObject = JSON.parseObject(message); - String command = jsonObject.getString("command"); - // 根据command类型,分发给不同的handler和detection - if (!command.isEmpty()) { - messageHandler.dispatcher(command, jsonObject, this.session); - } - } catch (Exception e) { - JSONObject msg = new JSONObject(); - msg.put("message", "传输数据必须为包含command字段的JSON格式"); - this.session.getBasicRemote().sendText(msg.toString()); - e.printStackTrace(); - } - } - } - - @OnError - public void onError(Throwable t) { - t.printStackTrace(); - } - - public void sendMessage(String message) throws IOException { - if(this.session != null) { - this.session.getBasicRemote().sendText(message); - } - } - -}