aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/torui/coflsky/websocket/WSClient.java
blob: c26edf0b7d2e88322c8d31e70f211fccb4743d86 (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
package de.torui.coflsky.websocket;
import java.net.URI;
import java.util.LinkedList;
import java.util.Queue;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import de.torui.coflsky.core.Command;


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);
	}

	@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) {
		
	}
	
}