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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package de.torui.coflsky.websocket;
import java.io.IOException;
import java.net.URI;
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.WSCommandHandler;
import de.torui.coflsky.core.Command;
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 WSClient(URI uri) {
this.uri = uri;
}
public void start() throws IOException, WebSocketException {
WebSocketFactory factory = new WebSocketFactory();
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);
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 class WSClient extends WebSocketClient{
public static Gson gson;
public static WSClient Instancce;
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 WSClient(URI serverUri) {
super(serverUri);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
}
@Override
public void onMessage(String message) {
//System.out.println(message);
Command cmd = gson.fromJson(message, Command.class);
//System.out.println(cmd);
WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer);
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.printf("code: %n reason:%s remote:%b", code, reason,remote);
}
@Override
public void onError(Exception ex) {
ex.printStackTrace();
}
public void SendCommand(Command command) {
String json = gson.toJson(command);
this.send(json);
}
}
*/
|