|
@@ -2,28 +2,27 @@ package websockets;
|
2
|
2
|
|
3
|
3
|
import io.undertow.Undertow;
|
4
|
4
|
import io.undertow.websockets.WebSocketConnectionCallback;
|
5
|
|
-import io.undertow.websockets.core.AbstractReceiveListener;
|
6
|
|
-import io.undertow.websockets.core.BufferedTextMessage;
|
7
|
5
|
import io.undertow.websockets.core.WebSocketChannel;
|
8
|
|
-import io.undertow.websockets.core.WebSockets;
|
9
|
6
|
import io.undertow.websockets.spi.WebSocketHttpExchange;
|
|
7
|
+import org.xnio.ChannelListener;
|
10
|
8
|
import tcp.CuisineTCPServer;
|
11
|
|
-
|
12
|
|
-import java.util.ArrayList;
|
|
9
|
+import websockets.handlers.CuisineWebSocketReceiveHandler;
|
|
10
|
+import websockets.handlers.CuisineWebSocketWebSocketChichatHandler;
|
|
11
|
+import websockets.handlers.CuisineWebSocketWebSocketTemperatureHandler;
|
13
|
12
|
|
14
|
13
|
import static io.undertow.Handlers.path;
|
15
|
14
|
import static io.undertow.Handlers.websocket;
|
16
|
|
-import static io.undertow.Handlers.*;
|
17
|
15
|
|
18
|
16
|
public class CuisineWebSocketServer {
|
19
|
17
|
|
20
|
18
|
CuisineWebSocketConnectionHandler fConnectionHandler;
|
21
|
19
|
CuisineWebSocketConnectionHandler fChatConnectionHandler;
|
22
|
|
- CuisineWebSocketMessageHandler fMessageHandler;
|
23
|
|
- CuisineWebSocketChichatHandler fChitchatMessageHandler;
|
|
20
|
+ CuisineWebSocketWebSocketTemperatureHandler fMessageHandler;
|
|
21
|
+ CuisineWebSocketWebSocketChichatHandler fChitchatMessageHandler;
|
24
|
22
|
Undertow fWebSocketServer;
|
25
|
23
|
String fWebSocketHost;
|
26
|
24
|
int fWebSocketPort;
|
|
25
|
+ int fConnectedClients;
|
27
|
26
|
|
28
|
27
|
public CuisineWebSocketServer(String iHost, int iPort
|
29
|
28
|
) {
|
|
@@ -32,11 +31,11 @@ public class CuisineWebSocketServer {
|
32
|
31
|
}
|
33
|
32
|
|
34
|
33
|
public void initTCPServer(CuisineTCPServer iTcpServer) {
|
35
|
|
- fMessageHandler = new CuisineWebSocketMessageHandler(iTcpServer);
|
|
34
|
+ fMessageHandler = new CuisineWebSocketWebSocketTemperatureHandler(iTcpServer);
|
36
|
35
|
}
|
37
|
36
|
|
38
|
37
|
public void start() {
|
39
|
|
- fChitchatMessageHandler = new CuisineWebSocketChichatHandler();
|
|
38
|
+ fChitchatMessageHandler = new CuisineWebSocketWebSocketChichatHandler();
|
40
|
39
|
|
41
|
40
|
fConnectionHandler = new CuisineWebSocketConnectionHandler(fMessageHandler);
|
42
|
41
|
fChatConnectionHandler = new CuisineWebSocketConnectionHandler(fChitchatMessageHandler);
|
|
@@ -54,32 +53,29 @@ public class CuisineWebSocketServer {
|
54
|
53
|
|
55
|
54
|
|
56
|
55
|
private class CuisineWebSocketConnectionHandler implements WebSocketConnectionCallback {
|
57
|
|
- private AbstractReceiveListener fWebSocketMessageHandler;
|
|
56
|
+ private CuisineWebSocketReceiveHandler fWebSocketMessageHandler;
|
58
|
57
|
|
59
|
|
- protected CuisineWebSocketConnectionHandler(AbstractReceiveListener iWebSocketMessageHandler) {
|
|
58
|
+ protected CuisineWebSocketConnectionHandler(CuisineWebSocketReceiveHandler iWebSocketMessageHandler) {
|
60
|
59
|
fWebSocketMessageHandler = iWebSocketMessageHandler;
|
61
|
60
|
}
|
62
|
61
|
|
63
|
62
|
@Override
|
64
|
63
|
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
|
65
|
64
|
channel.getReceiveSetter().set(fWebSocketMessageHandler);
|
|
65
|
+ fWebSocketMessageHandler.register(channel);
|
|
66
|
+ channel.addCloseTask(new ChannelListener<WebSocketChannel>(){
|
|
67
|
+
|
|
68
|
+ @Override
|
|
69
|
+ public void handleEvent(WebSocketChannel channel) {
|
|
70
|
+ fWebSocketMessageHandler.unregister(channel);
|
|
71
|
+ System.out.println(channel.getPeerAddress());
|
|
72
|
+ System.out.println("Connection closed");
|
|
73
|
+ }
|
|
74
|
+ });
|
66
|
75
|
channel.resumeReceives();
|
67
|
76
|
}
|
68
|
|
- }
|
69
|
|
-
|
70
|
|
- private class CuisineWebSocketMessageHandler extends AbstractReceiveListener {
|
71
|
|
-
|
72
|
|
- private CuisineTCPServer fTCPServer;
|
73
|
|
-
|
74
|
|
- protected CuisineWebSocketMessageHandler(CuisineTCPServer iTCPServer) {
|
75
|
|
- super();
|
76
|
|
- fTCPServer = iTCPServer;
|
77
|
|
- }
|
78
|
77
|
|
79
|
|
- protected void onFullTextMessage(WebSocketChannel iChannel, BufferedTextMessage iTextMessage) {
|
80
|
|
- //Handle message on webSocket
|
81
|
|
- WebSockets.sendText(String.valueOf(fTCPServer.getCurrentTemperature()), iChannel, null);
|
82
|
|
- }
|
83
|
78
|
}
|
84
|
79
|
|
|
80
|
+
|
85
|
81
|
}
|