Skip to content

Commit

Permalink
Improve web socket implementation and move shaded libraries to anothe…
Browse files Browse the repository at this point in the history
…r package
  • Loading branch information
stirante committed Feb 4, 2020
1 parent e5dccbd commit c8ca76c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.stirante</groupId>
<artifactId>lol-client-java-api</artifactId>
<version>1.1.7</version>
<version>1.1.8</version>

<properties>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -96,6 +96,22 @@
</goals>
</execution>
</executions>
<configuration>
<relocations>
<relocation>
<pattern>org.apache</pattern>
<shadedPattern>com.stirante.lolclient.libs.org.apache</shadedPattern>
</relocation>
<relocation>
<pattern>com.google</pattern>
<shadedPattern>com.stirante.lolclient.libs.com.google</shadedPattern>
</relocation>
<relocation>
<pattern>org.java_websocket</pattern>
<shadedPattern>com.stirante.lolclient.libs.org.java_websocket</shadedPattern>
</relocation>
</relocations>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down
43 changes: 33 additions & 10 deletions src/main/java/com/stirante/lolclient/ClientWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public void checkServerTrusted(X509Certificate[] certs, String authType) {
}

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

public void subscribe(String event) {
sendMessage(MessageType.SUBSCRIBE, event);
sendMessage(new Message(MessageType.SUBSCRIBE, event, null));
}

public void unsubscribe(String event) {
sendMessage(MessageType.UNSUBSCRIBE, event);
sendMessage(new Message(MessageType.UNSUBSCRIBE, event, null));
}


public void onOpen(ServerHandshake handshakedata) {
}

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

public void onMessage(String message) {
if (message.isEmpty()) return;
if (message.isEmpty()) {
return;
}
Message mess = GSON.fromJson(message, Message.class);
if (mess == null) {
return;
}
if (mess.type == MessageType.EVENT && socketListener != null) {
socketListener.onEvent(mess.event);
}
Expand All @@ -99,8 +105,9 @@ public void setSocketListener(SocketListener socketListener) {
this.socketListener = socketListener;
}

public void sendMessage(MessageType type, String message) {
send(GSON.toJson(new Object[]{type.getId(), message}));
public void sendMessage(Message message) {
String text = GSON.toJson(message);
send(text);
}

@Override
Expand Down Expand Up @@ -155,7 +162,7 @@ public static class Message {
private final String source;
private final Event event;

private Message(MessageType type, String source, Event event) {
public Message(MessageType type, String source, Event event) {
this.type = type;
this.source = source;
this.event = event;
Expand Down Expand Up @@ -232,7 +239,9 @@ public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
}
}
}
if (c == null) data = context.deserialize((JsonElement) data, Object.class);
if (c == null) {
data = context.deserialize((JsonElement) data, Object.class);
}
else {
try {
data = context.deserialize((JsonElement) data, c);
Expand All @@ -246,10 +255,13 @@ public Event deserialize(JsonElement json, Type typeOfT, JsonDeserializationCont
}
}

public static class MessageDeserializer implements JsonDeserializer<Message> {
public static class MessageDeserializer implements JsonDeserializer<Message>, JsonSerializer<Message> {

@Override
public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
if (!(json instanceof JsonArray)) {
return null;
}
JsonArray jArr = (JsonArray) json;
MessageType type = MessageType.getById(jArr.get(0).getAsInt());
String source = jArr.get(1).getAsString();
Expand All @@ -259,6 +271,17 @@ public Message deserialize(JsonElement json, Type typeOfT, JsonDeserializationCo
}
return new Message(type, source, event);
}

@Override
public JsonElement serialize(Message message, Type type, JsonSerializationContext context) {
JsonArray result = new JsonArray();
result.add(message.type.id);
result.add(message.source);
if (message.event != null) {
result.add(context.serialize(message.event));
}
return result;
}
}

}

0 comments on commit c8ca76c

Please sign in to comment.