Spring Boot webSocket从入门到放弃(5)

import java.net.URI; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft; import org.java_websocket.handshake.ServerHandshake; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestWebSocketClient extends WebSocketClient { private final Logger LOGGER = LoggerFactory.getLogger(TestWebSocketClient.class); public TestWebSocketClient(URI serverUri) { super(serverUri); } public TestWebSocketClient(URI serverUri, Draft protocolDraft) { super(serverUri, protocolDraft); } @Override public void onOpen(ServerHandshake serverHandshake) { LOGGER.info("Open a WebSocket connection on client. "); } @Override public void onClose(int arg0, String arg1, boolean arg2) { LOGGER.info("Close a WebSocket connection on client. "); } @Override public void onMessage(String msg) { LOGGER.info("WebSocketClient receives a message: " + msg); } @Override public void onError(Exception exception) { LOGGER.error("WebSocketClient exception. ", exception); } }

 

(2)WebSocketClient 发送数据

String serverUrl = "ws://127.0.0.1:18080/testWebsocket" URI recognizeUri = new URI(serverUrl); client = new TestWebSocketClient(recognizeUri, new Draft_6455()); client.connect(); client.send("This is a message from client. ");

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/8454cdbe5e3b14b8a41971404c368172.html