Skip to content

Commit c8ca76c

Browse files
committed
Improve web socket implementation and move shaded libraries to another package
1 parent e5dccbd commit c8ca76c

2 files changed

Lines changed: 50 additions & 11 deletions

File tree

pom.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.stirante</groupId>
88
<artifactId>lol-client-java-api</artifactId>
9-
<version>1.1.7</version>
9+
<version>1.1.8</version>
1010

1111
<properties>
1212
<java.version>1.8</java.version>
@@ -96,6 +96,22 @@
9696
</goals>
9797
</execution>
9898
</executions>
99+
<configuration>
100+
<relocations>
101+
<relocation>
102+
<pattern>org.apache</pattern>
103+
<shadedPattern>com.stirante.lolclient.libs.org.apache</shadedPattern>
104+
</relocation>
105+
<relocation>
106+
<pattern>com.google</pattern>
107+
<shadedPattern>com.stirante.lolclient.libs.com.google</shadedPattern>
108+
</relocation>
109+
<relocation>
110+
<pattern>org.java_websocket</pattern>
111+
<shadedPattern>com.stirante.lolclient.libs.org.java_websocket</shadedPattern>
112+
</relocation>
113+
</relocations>
114+
</configuration>
99115
</plugin>
100116
<plugin>
101117
<groupId>org.codehaus.mojo</groupId>

src/main/java/com/stirante/lolclient/ClientWebSocket.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
5959
}
6060

6161
private static Map<String, String> createHeaders(String... headers) {
62-
if (headers.length % 2 != 0) throw new IllegalArgumentException("Invalid amount of parameters!");
62+
if (headers.length % 2 != 0) {
63+
throw new IllegalArgumentException("Invalid amount of parameters!");
64+
}
6365
HashMap<String, String> map = new HashMap<>();
6466
for (int i = 0; i < headers.length; i += 2) {
6567
String key = headers[i];
@@ -70,14 +72,13 @@ private static Map<String, String> createHeaders(String... headers) {
7072
}
7173

7274
public void subscribe(String event) {
73-
sendMessage(MessageType.SUBSCRIBE, event);
75+
sendMessage(new Message(MessageType.SUBSCRIBE, event, null));
7476
}
7577

7678
public void unsubscribe(String event) {
77-
sendMessage(MessageType.UNSUBSCRIBE, event);
79+
sendMessage(new Message(MessageType.UNSUBSCRIBE, event, null));
7880
}
7981

80-
8182
public void onOpen(ServerHandshake handshakedata) {
8283
}
8384

@@ -88,8 +89,13 @@ public void onClose(int code, String reason, boolean remote) {
8889
}
8990

9091
public void onMessage(String message) {
91-
if (message.isEmpty()) return;
92+
if (message.isEmpty()) {
93+
return;
94+
}
9295
Message mess = GSON.fromJson(message, Message.class);
96+
if (mess == null) {
97+
return;
98+
}
9399
if (mess.type == MessageType.EVENT && socketListener != null) {
94100
socketListener.onEvent(mess.event);
95101
}
@@ -99,8 +105,9 @@ public void setSocketListener(SocketListener socketListener) {
99105
this.socketListener = socketListener;
100106
}
101107

102-
public void sendMessage(MessageType type, String message) {
103-
send(GSON.toJson(new Object[]{type.getId(), message}));
108+
public void sendMessage(Message message) {
109+
String text = GSON.toJson(message);
110+
send(text);
104111
}
105112

106113
@Override
@@ -155,7 +162,7 @@ public static class Message {
155162
private final String source;
156163
private final Event event;
157164

158-
private Message(MessageType type, String source, Event event) {
165+
public Message(MessageType type, String source, Event event) {
159166
this.type = type;
160167
this.source = source;
161168
this.event = event;
@@ -232,7 +239,9 @@ public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
232239
}
233240
}
234241
}
235-
if (c == null) data = context.deserialize((JsonElement) data, Object.class);
242+
if (c == null) {
243+
data = context.deserialize((JsonElement) data, Object.class);
244+
}
236245
else {
237246
try {
238247
data = context.deserialize((JsonElement) data, c);
@@ -246,10 +255,13 @@ public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
246255
}
247256
}
248257

249-
public static class MessageDeserializer implements JsonDeserializer<Message> {
258+
public static class MessageDeserializer implements JsonDeserializer<Message>, JsonSerializer<Message> {
250259

251260
@Override
252261
public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
262+
if (!(json instanceof JsonArray)) {
263+
return null;
264+
}
253265
JsonArray jArr = (JsonArray) json;
254266
MessageType type = MessageType.getById(jArr.get(0).getAsInt());
255267
String source = jArr.get(1).getAsString();
@@ -259,6 +271,17 @@ public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
259271
}
260272
return new Message(type, source, event);
261273
}
274+
275+
@Override
276+
public JsonElement serialize(Message message, Type type, JsonSerializationContext context) {
277+
JsonArray result = new JsonArray();
278+
result.add(message.type.id);
279+
result.add(message.source);
280+
if (message.event != null) {
281+
result.add(context.serialize(message.event));
282+
}
283+
return result;
284+
}
262285
}
263286

264287
}

0 commit comments

Comments
 (0)