|
@@ -1,14 +1,15 @@
|
|
|
package com.fdkk.sxz;
|
|
|
|
|
|
-import cn.hutool.core.lang.Console;
|
|
|
-import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.socket.aio.AioClient;
|
|
|
-import cn.hutool.socket.aio.AioSession;
|
|
|
-import cn.hutool.socket.aio.SimpleIoAction;
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
-import java.net.InetSocketAddress;
|
|
|
-import java.nio.ByteBuffer;
|
|
|
+import okhttp3.*;
|
|
|
+import okio.ByteString;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+import org.jetbrains.annotations.Nullable;
|
|
|
+
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URISyntaxException;
|
|
|
+import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
public class main {
|
|
|
//public static void main(String[] args) {
|
|
@@ -190,21 +191,58 @@ public class main {
|
|
|
//}
|
|
|
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- AioClient client = new AioClient(new InetSocketAddress("192.168.0.75", 8233), new SimpleIoAction() {
|
|
|
+ public static void main(String[] args) throws URISyntaxException, ExecutionException, InterruptedException {
|
|
|
+ URI uri = new URI("ws://192.168.0.75:8223/wss/maxToDataSmith/");
|
|
|
+ synchronized (uri.getPath()) {
|
|
|
+ main.getsocket();
|
|
|
+ }
|
|
|
+ System.out.println("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh");
|
|
|
+ }
|
|
|
+
|
|
|
+ private synchronized static void getsocket() {
|
|
|
+ Request request = new Request.Builder().url("ws://127.0.0.1:8223/wss/maxToDataSmith/").build();
|
|
|
+ OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
|
|
|
+ .readTimeout(3, TimeUnit.SECONDS)//设置读取超时时间
|
|
|
+ .writeTimeout(3, TimeUnit.SECONDS)//设置写的超时时间
|
|
|
+ .connectTimeout(3, TimeUnit.SECONDS)//设置连接超时时间
|
|
|
+ .build();
|
|
|
+
|
|
|
+ WebSocketListener socketListener = new WebSocketListener() {
|
|
|
+ @Override
|
|
|
+ public void onClosed(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
|
|
|
+ super.onClosed(webSocket, code, reason);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClosing(@NotNull WebSocket webSocket, int code, @NotNull String reason) {
|
|
|
+ super.onClosing(webSocket, code, reason);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
|
|
|
+ super.onFailure(webSocket, t, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
|
|
|
+ super.onMessage(webSocket, text);
|
|
|
+ System.out.println(text);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {
|
|
|
+ super.onMessage(webSocket, bytes);
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
- public void doAction(AioSession session, ByteBuffer data) {
|
|
|
- if (data.hasRemaining()) {
|
|
|
- Console.log(StrUtil.utf8Str(data));
|
|
|
- session.read();
|
|
|
- }
|
|
|
- Console.log("OK");
|
|
|
+ public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
|
|
|
+ super.onOpen(webSocket, response);
|
|
|
+ webSocket.send("{\"uuid\":\"111222333\",\"max\":\"111222333.max\",\"flag\":\"false\"}");
|
|
|
}
|
|
|
- });
|
|
|
- String json = JSON.toJSONString("{\"uuid\":\"111222333\",\"max\":\"111222333.max\",\"flag\":\"false\"}");
|
|
|
- client.write(ByteBuffer.wrap(json.getBytes()));
|
|
|
- client.read();
|
|
|
- client.close();
|
|
|
+ };
|
|
|
+ mOkHttpClient.newWebSocket(request, socketListener);
|
|
|
+ mOkHttpClient.dispatcher().executorService().shutdown();
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|