|
9 | 9 | import java.lang.reflect.Method; |
10 | 10 | import java.net.URL; |
11 | 11 | import java.net.URLClassLoader; |
12 | | -import java.nio.ByteBuffer; |
13 | 12 |
|
14 | 13 | /** |
15 | 14 | * @author ReaJason |
16 | 15 | * @since 2025/5/9 |
17 | 16 | */ |
18 | | -public class GodzillaWebSocket extends Endpoint implements MessageHandler.Whole<ByteBuffer> { |
| 17 | +public class GodzillaWebSocket extends Endpoint implements MessageHandler.Whole<String> { |
19 | 18 | public static String key; |
20 | 19 |
|
21 | 20 | private Session session; |
@@ -45,28 +44,64 @@ public void onOpen(final Session session, EndpointConfig config) { |
45 | 44 | } |
46 | 45 |
|
47 | 46 | @Override |
48 | | - public void onMessage(ByteBuffer byteBuffer) { |
| 47 | + public void onMessage(String message) { |
49 | 48 | try { |
50 | | - byte[] data = byteBuffer.array(); |
| 49 | + byte[] data = base64Decode(message); |
51 | 50 | data = x(data, false); |
52 | | - byte[] response = new byte[0]; |
53 | 51 | if (payload == null) { |
54 | 52 | payload = Q(data); |
| 53 | + session.getBasicRemote().sendText(base64Encode(x("ok".getBytes(), true))); |
55 | 54 | } else { |
56 | 55 | java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); |
57 | 56 | Object obj = payload.newInstance(); |
58 | 57 | obj.equals(data); |
59 | 58 | obj.equals(bos); |
60 | 59 | obj.toString(); |
61 | | - response = bos.toByteArray(); |
| 60 | + session.getBasicRemote().sendText(base64Encode(x(bos.toByteArray(), true))); |
62 | 61 | } |
63 | | - session.getBasicRemote().sendBinary(ByteBuffer.wrap(x(response, true))); |
64 | 62 | } catch (Throwable e) { |
65 | | - e.printStackTrace(); |
66 | 63 | try { |
67 | 64 | session.close(); |
68 | 65 | } catch (java.io.IOException ignored) { |
69 | 66 | } |
70 | 67 | } |
71 | 68 | } |
| 69 | + |
| 70 | + @SuppressWarnings("all") |
| 71 | + public static String base64Encode(byte[] bs) throws Exception { |
| 72 | + String value = null; |
| 73 | + Class<?> base64; |
| 74 | + try { |
| 75 | + base64 = Class.forName("java.util.Base64", true, Thread.currentThread().getContextClassLoader()); |
| 76 | + Object encoder = base64.getMethod("getEncoder", (Class<?>[]) null).invoke(base64, (Object[]) null); |
| 77 | + value = (String) encoder.getClass().getMethod("encodeToString", byte[].class).invoke(encoder, bs); |
| 78 | + } catch (Exception var6) { |
| 79 | + try { |
| 80 | + base64 = Class.forName("sun.misc.BASE64Encoder", true, Thread.currentThread().getContextClassLoader()); |
| 81 | + Object encoder = base64.newInstance(); |
| 82 | + value = (String) encoder.getClass().getMethod("encode", byte[].class).invoke(encoder, bs); |
| 83 | + } catch (Exception ignored) { |
| 84 | + } |
| 85 | + } |
| 86 | + return value; |
| 87 | + } |
| 88 | + |
| 89 | + @SuppressWarnings("all") |
| 90 | + public static byte[] base64Decode(String bs) { |
| 91 | + byte[] value = null; |
| 92 | + Class<?> base64; |
| 93 | + try { |
| 94 | + base64 = Class.forName("java.util.Base64", false, Thread.currentThread().getContextClassLoader()); |
| 95 | + Object decoder = base64.getMethod("getDecoder", (Class<?>[]) null).invoke(base64, (Object[]) null); |
| 96 | + value = (byte[]) decoder.getClass().getMethod("decode", String.class).invoke(decoder, bs); |
| 97 | + } catch (Exception var6) { |
| 98 | + try { |
| 99 | + base64 = Class.forName("sun.misc.BASE64Decoder", false, Thread.currentThread().getContextClassLoader()); |
| 100 | + Object decoder = base64.newInstance(); |
| 101 | + value = (byte[]) decoder.getClass().getMethod("decodeBuffer", String.class).invoke(decoder, bs); |
| 102 | + } catch (Exception ignored) { |
| 103 | + } |
| 104 | + } |
| 105 | + return value; |
| 106 | + } |
72 | 107 | } |
0 commit comments