blob: 8adce9a2a46770f744160211c777b3160d59c1bd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package de.torui.coflsky.websocket;
import java.io.IOException;
import java.net.URI;
import java.security.NoSuchAlgorithmException;
import javax.net.ssl.SSLContext;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.neovisionaries.ws.client.WebSocket;
import com.neovisionaries.ws.client.WebSocketAdapter;
import com.neovisionaries.ws.client.WebSocketException;
import com.neovisionaries.ws.client.WebSocketFactory;
import com.neovisionaries.ws.client.WebSocketState;
import net.minecraft.client.Minecraft;
import de.torui.coflsky.CoflSky;
import de.torui.coflsky.WSCommandHandler;
import de.torui.coflsky.core.Command;
import de.torui.coflsky.core.StringCommand;
public class WSClient extends WebSocketAdapter {
public static Gson gson;
static {
gson = new GsonBuilder()/*.setFieldNamingStrategy(new FieldNamingStrategy() {
@Override
public String translateName(Field f) {
String name = f.getName();
char firstChar = name.charAt(0);
return Character.toLowerCase(firstChar) + name.substring(1);
}
})*/.create();
}
public URI uri;
public WebSocket socket;
public boolean shouldRun = false;
public WebSocketState currentState = WebSocketState.CLOSED;
public WSClient(URI uri) {
this.uri = uri;
}
public void start() throws IOException, WebSocketException, NoSuchAlgorithmException {
WebSocketFactory factory = new WebSocketFactory();
/*// Create a custom SSL context.
SSLContext context = NaiveSSLContext.getInstance("TLS");
// Set the custom SSL context.
factory.setSSLContext(context);
// Disable manual hostname verification for NaiveSSLContext.
//
// Manual hostname verification has been enabled since the
// version 2.1. Because the verification is executed manually
// after Socket.connect(SocketAddress, int) succeeds, the
// hostname verification is always executed even if you has
// passed an SSLContext which naively accepts any server
// certificate. However, this behavior is not desirable in
// some cases and you may want to disable the hostname
// verification. You can disable the hostname verification
// by calling WebSocketFactory.setVerifyHostname(false).
factory.setVerifyHostname(false);
factory.*/
factory.setConnectionTimeout(10*1000);
this.socket = factory.createSocket(uri);
this.socket.addListener(this);
this.socket.connect();
}
public void stop() {
System.out.println("Closing Socket");
// socket.sendClose();
socket.clearListeners();
socket.disconnect();
/*try {
socket.getConnectedSocket().close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WebSocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
System.out.println("Socket closed");
}
@Override
public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception {
System.out.println("WebSocket Changed state to: " + newState);
currentState = newState;
if(newState == WebSocketState.CLOSED && shouldRun) {
CoflSky.Wrapper.restartWebsocketConnection();
}
super.onStateChanged(websocket, newState);
}
@Override
public void onTextMessage(WebSocket websocket, String text) throws Exception{
//super.onTextMessage(websocket, text);
System.out.println("Received: "+ text);
Command cmd = gson.fromJson(text, Command.class);
//System.out.println(cmd);
WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer);
}
public void SendCommand(Command cmd) {
String json = gson.toJson(cmd);
this.socket.sendText(json);
}
public void SendCommand(StringCommand sc) {
String json = gson.toJson(sc);
this.socket.sendText(json);
}
}
|