6 changed files with 91 additions and 74 deletions
-
5pom.xml
-
6src/main/java/com/iflytop/nuclear/NuclearApplication.java
-
47src/main/java/com/iflytop/nuclear/config/WebSocketConfig.java
-
24src/main/java/com/iflytop/nuclear/controller/TestController.java
-
15src/main/java/com/iflytop/nuclear/websocket/SocketClient.java
-
68src/main/java/com/iflytop/nuclear/websocket/WebSocketClient.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; |
|||
} |
|||
} |
@ -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"); |
|||
} |
|||
} |
@ -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"); |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue