commit 82ef0cdd1ff09d0eaa992f427f87cfe2616a5da7 Author: yaobo Date: Tue May 14 09:01:29 2024 +0000 上传文件至 websocket diff --git a/websocket/Java-WebSocket-1.5.2.jar b/websocket/Java-WebSocket-1.5.2.jar new file mode 100644 index 0000000..19b8c5d Binary files /dev/null and b/websocket/Java-WebSocket-1.5.2.jar differ diff --git a/websocket/SockJsTool.java b/websocket/SockJsTool.java new file mode 100644 index 0000000..dbb39da --- /dev/null +++ b/websocket/SockJsTool.java @@ -0,0 +1,103 @@ +package com.example.utils.SockJs; + + +import org.springframework.messaging.converter.StringMessageConverter; +import org.springframework.messaging.simp.stomp.StompFrameHandler; +import org.springframework.messaging.simp.stomp.StompHeaders; +import org.springframework.messaging.simp.stomp.StompSession; +import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; +import org.springframework.util.concurrent.ListenableFuture; +import org.springframework.web.socket.WebSocketHttpHeaders; +import org.springframework.web.socket.client.standard.StandardWebSocketClient; +import org.springframework.web.socket.messaging.WebSocketStompClient; +import org.springframework.web.socket.sockjs.client.SockJsClient; +import org.springframework.web.socket.sockjs.client.Transport; +import org.springframework.web.socket.sockjs.client.WebSocketTransport; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; + + +/* + org.java-websocket + Java-WebSocket + 1.5.2 + */ + +/** + * 请求工具 + */ +public class SockJsTool extends StompSessionHandlerAdapter { + /** + * 请求地址 + */ + private String url; + /** + * 请求 token + */ + private String token; + + /** + * + */ + private StompSession session; + + + public SockJsTool(String url, String token) { + this.url = url; + this.token = token; + this.httpConnect(); + } + + public SockJsTool(String url) { + this.url = url; + this.httpConnect(); + } + + + public SockJsTool() { + } + + public StompSession getSession() { + return session; + } + + + /** + * http 链接websocket + */ + public void httpConnect(){ + //判断是否已连接 + if(session == null||!session.isConnected()){ + System.out.println("当前处于断开状态,尝试链接"); + + List transports = new ArrayList<>(); + transports.add(new WebSocketTransport(new StandardWebSocketClient())); + SockJsClient sockJsClient = new SockJsClient(transports); + WebSocketStompClient webSocketStompClient=new WebSocketStompClient(sockJsClient); + webSocketStompClient.setMessageConverter(new StringMessageConverter()); + webSocketStompClient.setDefaultHeartbeat(new long[] { 20000, 0 }); + + ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); + taskScheduler.afterPropertiesSet(); + webSocketStompClient.setTaskScheduler(taskScheduler); + + WebSocketHttpHeaders webSocketHttpHeaders = null; + StompHeaders stompHeaders = new StompHeaders(); + stompHeaders.add("token", token); + stompHeaders.set("user","1"); + ListenableFuture future = webSocketStompClient.connect(url, webSocketHttpHeaders, stompHeaders, new SockJsTool()); + try { + this.session = future.get(); + this.session.setAutoReceipt(true); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } catch (ExecutionException e) { + throw new RuntimeException(e); + } + } + } + +} diff --git a/websocket/TEST009.java b/websocket/TEST009.java new file mode 100644 index 0000000..5292318 --- /dev/null +++ b/websocket/TEST009.java @@ -0,0 +1,26 @@ +package com.example.utils.SockJs; + + +import org.springframework.messaging.simp.stomp.StompHeaders; +import org.springframework.messaging.simp.stomp.StompSession; +import org.springframework.messaging.simp.stomp.StompSessionHandlerAdapter; + +import java.util.concurrent.ExecutionException; + +public class TEST009 { + public static void main(String[] args) throws ExecutionException, InterruptedException { + SockJsTool tool=new SockJsTool("http://localhost:8080/system/websocket"); + + StompSession session=tool.getSession(); + session.subscribe("/user/topic/subscribePrint5",new StompSessionHandlerAdapter(){ + @Override + public void handleFrame(StompHeaders headers, Object payload) { + System.out.println(payload); + } + }); + + session.send("/app/downloadLocal","2"); + + + } +}