|
@@ -2,18 +2,11 @@ import io.undertow.Undertow;
|
2
|
2
|
import io.undertow.server.HttpHandler;
|
3
|
3
|
import io.undertow.server.HttpServerExchange;
|
4
|
4
|
import io.undertow.util.Headers;
|
5
|
|
-import io.undertow.websockets.WebSocketConnectionCallback;
|
6
|
|
-import io.undertow.websockets.core.AbstractReceiveListener;
|
7
|
|
-import io.undertow.websockets.core.BufferedTextMessage;
|
8
|
|
-import io.undertow.websockets.core.WebSocketChannel;
|
9
|
|
-import io.undertow.websockets.core.WebSockets;
|
10
|
|
-import io.undertow.websockets.spi.WebSocketHttpExchange;
|
11
|
|
-
|
12
|
|
-import java.net.URL;
|
13
|
|
-import java.nio.file.Files;
|
14
|
|
-import java.nio.file.Paths;
|
|
5
|
+import tcp.CuisineTCPServer;
|
|
6
|
+
|
|
7
|
+import websockets.CuisineWebSocketServer;
|
|
8
|
+
|
15
|
9
|
import java.util.Scanner;
|
16
|
|
-import java.util.stream.Stream;
|
17
|
10
|
|
18
|
11
|
import static io.undertow.Handlers.*;
|
19
|
12
|
|
|
@@ -23,55 +16,57 @@ public class CuisineHTTPServer extends Thread
|
23
|
16
|
|
24
|
17
|
static int webSocketPort = 42069;
|
25
|
18
|
static String webSocketHost = "192.168.0.24";
|
|
19
|
+ static String webSocketProcolPrefix = "wss";
|
|
20
|
+ static boolean writePortInClientSideURI = false;
|
26
|
21
|
|
27
|
22
|
static int tcpSocketPort = 6868;
|
28
|
23
|
static String HOST = "0.0.0.0" ;
|
29
|
24
|
|
30
|
|
-
|
31
|
25
|
private static CuisineTCPServer fCuisineTCPServer;
|
32
|
26
|
|
33
|
|
- public void startServer(String iHostName, int iWebPagePort, int iWebSocketPort, int iTcpSocketPort)
|
|
27
|
+ public void startServer(String iHostName, int iWebPagePort, int iWebSocketPort, boolean iWebSocketUriClPortDisplay, String iWebSocketProtocolPrefix, int iTcpSocketPort)
|
34
|
28
|
{
|
35
|
29
|
webSocketHost = iHostName;
|
36
|
30
|
webPagePort = iWebPagePort;
|
37
|
31
|
webSocketPort = iWebSocketPort;
|
38
|
32
|
tcpSocketPort = iTcpSocketPort;
|
|
33
|
+ webSocketProcolPrefix = iWebSocketProtocolPrefix;
|
|
34
|
+ writePortInClientSideURI = iWebSocketUriClPortDisplay;
|
39
|
35
|
initResources();
|
40
|
36
|
|
41
|
|
- //Create CuisineTCPServers
|
|
37
|
+ //Create CuisineTCPServer
|
42
|
38
|
fCuisineTCPServer = new CuisineTCPServer(tcpSocketPort);
|
43
|
|
- CuisineWebSocketMessageHandler vWebSocketMessageHandler = new CuisineWebSocketMessageHandler( fCuisineTCPServer );
|
44
|
|
- CuisineWebSocketConnectionHandler vWebSocketConnectionHandler = new CuisineWebSocketConnectionHandler( vWebSocketMessageHandler );
|
45
|
39
|
|
46
|
40
|
//Start HTTP Server : serve webpage with JS that calls the websocket
|
47
|
41
|
Undertow WebPageServer = Undertow.builder()
|
48
|
42
|
.addHttpListener(webPagePort, HOST)
|
49
|
43
|
.setHandler(
|
50
|
|
- new CuisineResponseHandler("ws://"+webSocketHost+":"+webSocketPort+"/data")
|
|
44
|
+ new CuisineResponseHandler(iWebSocketProtocolPrefix+"://"+webSocketHost+":"+( writePortInClientSideURI ? webSocketPort : "/data" ))
|
51
|
45
|
).build();
|
52
|
46
|
WebPageServer.start();
|
53
|
47
|
|
54
|
48
|
System.out.println("WebPageServer started");
|
|
49
|
+
|
|
50
|
+ CuisineWebSocketServer vWebSocketServer = new CuisineWebSocketServer(webSocketHost,webSocketPort);
|
|
51
|
+ vWebSocketServer.initTCPServer(fCuisineTCPServer);
|
55
|
52
|
//Start websocket
|
56
|
|
- Undertow WebSocketServer = Undertow.builder()
|
57
|
|
- .addHttpListener(webSocketPort, HOST)
|
58
|
|
- .setHandler(
|
59
|
|
- path()
|
60
|
|
- .addPrefixPath("/data",websocket(vWebSocketConnectionHandler))
|
61
|
|
- )
|
62
|
|
- .build();
|
63
|
|
- WebSocketServer.start();
|
|
53
|
+ vWebSocketServer.start();
|
64
|
54
|
System.out.println("WebSocketServer started");
|
65
|
55
|
|
|
56
|
+
|
|
57
|
+
|
66
|
58
|
}
|
67
|
59
|
|
68
|
60
|
static String sHtml_ASCII_title;
|
69
|
61
|
static String sHtml_CSS;
|
|
62
|
+ static String sJS_ClientSide;
|
70
|
63
|
static void initResources()
|
71
|
64
|
{
|
72
|
65
|
try {
|
73
|
66
|
sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
|
74
|
67
|
sHtml_CSS = new Scanner(CuisineHTTPServer.class.getResourceAsStream("style.css"), "UTF-8").useDelimiter("\\A").next();
|
|
68
|
+ sJS_ClientSide = new Scanner(CuisineHTTPServer.class.getResourceAsStream("clientsidecoms.js"), "UTF-8").useDelimiter("\\A").next();
|
|
69
|
+
|
75
|
70
|
}
|
76
|
71
|
catch(Exception vex)
|
77
|
72
|
{
|
|
@@ -100,38 +95,6 @@ public class CuisineHTTPServer extends Thread
|
100
|
95
|
}
|
101
|
96
|
}
|
102
|
97
|
|
103
|
|
- private class CuisineWebSocketConnectionHandler implements WebSocketConnectionCallback
|
104
|
|
- {
|
105
|
|
- private CuisineWebSocketMessageHandler fWebSocketMessageHandler;
|
106
|
|
-
|
107
|
|
- protected CuisineWebSocketConnectionHandler(CuisineWebSocketMessageHandler iWebSocketMessageHandler)
|
108
|
|
- {
|
109
|
|
- fWebSocketMessageHandler = iWebSocketMessageHandler;
|
110
|
|
- }
|
111
|
|
-
|
112
|
|
- @Override
|
113
|
|
- public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
|
114
|
|
- channel.getReceiveSetter().set(fWebSocketMessageHandler);
|
115
|
|
- channel.resumeReceives();
|
116
|
|
- }
|
117
|
|
- }
|
118
|
|
-
|
119
|
|
- private class CuisineWebSocketMessageHandler extends AbstractReceiveListener {
|
120
|
|
-
|
121
|
|
- private CuisineTCPServer fTCPServer;
|
122
|
|
-
|
123
|
|
- protected CuisineWebSocketMessageHandler( CuisineTCPServer iTCPServer)
|
124
|
|
- {
|
125
|
|
-
|
126
|
|
- fTCPServer = iTCPServer;
|
127
|
|
- }
|
128
|
|
-
|
129
|
|
- protected void onFullTextMessage(WebSocketChannel iChannel, BufferedTextMessage iTextMessage)
|
130
|
|
- {
|
131
|
|
- //Handle message on webSocket
|
132
|
|
- WebSockets.sendText(String.valueOf(fTCPServer.getCurrentTemperature()), iChannel, null);
|
133
|
|
- }
|
134
|
|
- }
|
135
|
98
|
|
136
|
99
|
|
137
|
100
|
static String getHTMLFrontPage(String iWsUri)
|
|
@@ -150,19 +113,7 @@ public class CuisineHTTPServer extends Thread
|
150
|
113
|
.append(" </div>")
|
151
|
114
|
.append(" <h1 id=\"tempValue\">None</h1>")
|
152
|
115
|
.append(" <script>")
|
153
|
|
- .append(" var socket=new WebSocket(\""+iWsUri+"\");\n")
|
154
|
|
- .append(" let connected=false;")
|
155
|
|
- .append(" let view=document.getElementById(\"tempValue\");")
|
156
|
|
- .append(" socket.onerror=function(error){connected=false;view.innerHTML=\"oups :(\";};")
|
157
|
|
- .append(" socket.onmessage=function(event){view.innerHTML=event.data+\"°C\"};")
|
158
|
|
- .append(" socket.onopen=function(e){")
|
159
|
|
- .append(" connected=true;")
|
160
|
|
- .append(" callServer();")
|
161
|
|
- .append(" };")
|
162
|
|
- .append(" var callServer=function(){")
|
163
|
|
- .append(" socket.send(\"coucou\");")
|
164
|
|
- .append(" };")
|
165
|
|
- .append(" let timer=setInterval(callServer,100);")
|
|
116
|
+ .append(sJS_ClientSide.replace("%WSURI%",iWsUri))
|
166
|
117
|
.append(" </script>")
|
167
|
118
|
.append("</body>")
|
168
|
119
|
.append("")
|