Browse Source

petit refactor et paramètres

master
DemiSel 3 years ago
parent
commit
ee50fef00c

+ 124
- 0
.idea/uiDesigner.xml View File

1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project version="4">
3
+  <component name="Palette2">
4
+    <group name="Swing">
5
+      <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
6
+        <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
7
+      </item>
8
+      <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
9
+        <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
10
+      </item>
11
+      <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
12
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
13
+      </item>
14
+      <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
15
+        <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
16
+      </item>
17
+      <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
18
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
19
+        <initial-values>
20
+          <property name="text" value="Button" />
21
+        </initial-values>
22
+      </item>
23
+      <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
24
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
25
+        <initial-values>
26
+          <property name="text" value="RadioButton" />
27
+        </initial-values>
28
+      </item>
29
+      <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
30
+        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
31
+        <initial-values>
32
+          <property name="text" value="CheckBox" />
33
+        </initial-values>
34
+      </item>
35
+      <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
36
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
37
+        <initial-values>
38
+          <property name="text" value="Label" />
39
+        </initial-values>
40
+      </item>
41
+      <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
42
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
43
+          <preferred-size width="150" height="-1" />
44
+        </default-constraints>
45
+      </item>
46
+      <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
47
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
48
+          <preferred-size width="150" height="-1" />
49
+        </default-constraints>
50
+      </item>
51
+      <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
52
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
53
+          <preferred-size width="150" height="-1" />
54
+        </default-constraints>
55
+      </item>
56
+      <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
57
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
58
+          <preferred-size width="150" height="50" />
59
+        </default-constraints>
60
+      </item>
61
+      <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
62
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
63
+          <preferred-size width="150" height="50" />
64
+        </default-constraints>
65
+      </item>
66
+      <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
67
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
68
+          <preferred-size width="150" height="50" />
69
+        </default-constraints>
70
+      </item>
71
+      <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
72
+        <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
73
+      </item>
74
+      <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
75
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
76
+          <preferred-size width="150" height="50" />
77
+        </default-constraints>
78
+      </item>
79
+      <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
80
+        <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
81
+          <preferred-size width="150" height="50" />
82
+        </default-constraints>
83
+      </item>
84
+      <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
85
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
86
+          <preferred-size width="150" height="50" />
87
+        </default-constraints>
88
+      </item>
89
+      <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
90
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
91
+          <preferred-size width="200" height="200" />
92
+        </default-constraints>
93
+      </item>
94
+      <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
95
+        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
96
+          <preferred-size width="200" height="200" />
97
+        </default-constraints>
98
+      </item>
99
+      <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
100
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
101
+      </item>
102
+      <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
103
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
104
+      </item>
105
+      <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
106
+        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
107
+      </item>
108
+      <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
109
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
110
+      </item>
111
+      <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
112
+        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
113
+          <preferred-size width="-1" height="20" />
114
+        </default-constraints>
115
+      </item>
116
+      <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
117
+        <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
118
+      </item>
119
+      <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
120
+        <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
121
+      </item>
122
+    </group>
123
+  </component>
124
+</project>

+ 16
- 1
README.md View File

1
 # CyberCuisineWebServer
1
 # CyberCuisineWebServer
2
+Server HTTP + WebSocket + Serveur TCP pour transmettre la température du thermometre dans ma cuisine
2
 
3
 
3
-Server HTTP + WebSocket + Serveur TCP pour transmettre la température du thermometre dans ma cuisine
4
+## Running
5
+**Java 14**
6
+~~~~
7
+java -jar [jar] -h hostname 
8
+~~~~
9
+
10
+Parametres :
11
+~~~
12
+-h hostname
13
+-wp port : pour la page web (8080)
14
+-sp port : pour le websocket (10269)
15
+-wsp    : afficher le port dans l'uri du websocket dans le code côté client
16
+-sr prefix : pour le websocket [prefix (ws/wss)]://hostname (wss)
17
+-tp port : pour le socket tcp (6868)
18
+~~~

+ 1
- 1
build.gradle View File

26
                     'Implementation-Version': version,
26
                     'Implementation-Version': version,
27
                     'Main-Class': 'Application'
27
                     'Main-Class': 'Application'
28
     }
28
     }
29
-    baseName = project.name + '-all'
29
+    baseName = project.name
30
     from {
30
     from {
31
         configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
31
         configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
32
 
32
 

+ 3
- 3
docker/docker-compose.yml View File

8
     ports:
8
     ports:
9
       - "10200:8080"
9
       - "10200:8080"
10
       - "10220:6868"
10
       - "10220:6868"
11
-      - "10269:42069"
11
+      - "10269:10269"
12
     volumes:
12
     volumes:
13
-      - "./CyberCuisine.jar:/app.jar"
14
-    command: ['java', '-jar', '/app.jar']
13
+      - "./CyberCuisine-1.0.jar:/app.jar"
14
+    command: ["java", "-jar", "/app.jar","-h","demisel.space","-sr","wss","-sp","10269","-wp","8080","-tp","6868"]

+ 25
- 1
src/main/java/Application.java View File

5
         String vHostName = "demisel.space";
5
         String vHostName = "demisel.space";
6
         int vWebPagePort = 8080;
6
         int vWebPagePort = 8080;
7
         int vWebSocketPort = 10269;
7
         int vWebSocketPort = 10269;
8
+        boolean vWebSocketUriClDispPort = false;
8
         int vTcpSocketPort = 6868;
9
         int vTcpSocketPort = 6868;
10
+        String vWebSocketProtocolPrefix = "wss";
9
         CuisineHTTPServer vServer = new CuisineHTTPServer();
11
         CuisineHTTPServer vServer = new CuisineHTTPServer();
10
-        vServer.startServer(vHostName,vWebPagePort,vWebSocketPort,vTcpSocketPort);
12
+
13
+        int i = 0;
14
+        for(i = 0 ; i<args.length ; i++)
15
+        {
16
+            switch(args[i])
17
+            {
18
+                case "-h" -> vHostName = args[++i];
19
+                case "-sr" -> vWebSocketProtocolPrefix = args[++i];
20
+                case "-wp" -> vWebPagePort = Integer.parseInt(args[++i]);
21
+                case "-sp" -> vWebSocketPort = Integer.parseInt(args[++i]);
22
+                case "-tp" -> vTcpSocketPort = Integer.parseInt(args[++i]);
23
+                case "-wsp" -> vWebSocketUriClDispPort = true;
24
+            }
25
+        }
26
+        vServer.startServer
27
+            (
28
+            vHostName,
29
+            vWebPagePort,
30
+            vWebSocketPort,
31
+            vWebSocketUriClDispPort,
32
+            vWebSocketProtocolPrefix,
33
+            vTcpSocketPort
34
+            );
11
     }
35
     }
12
 }
36
 }

+ 21
- 70
src/main/java/CuisineHTTPServer.java View File

2
 import io.undertow.server.HttpHandler;
2
 import io.undertow.server.HttpHandler;
3
 import io.undertow.server.HttpServerExchange;
3
 import io.undertow.server.HttpServerExchange;
4
 import io.undertow.util.Headers;
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
 import java.util.Scanner;
9
 import java.util.Scanner;
16
-import java.util.stream.Stream;
17
 
10
 
18
 import static io.undertow.Handlers.*;
11
 import static io.undertow.Handlers.*;
19
 
12
 
23
 
16
 
24
     static int webSocketPort = 42069;
17
     static int webSocketPort = 42069;
25
     static String webSocketHost = "192.168.0.24";
18
     static String webSocketHost = "192.168.0.24";
19
+    static String webSocketProcolPrefix = "wss";
20
+    static boolean writePortInClientSideURI = false;
26
 
21
 
27
     static int tcpSocketPort = 6868;
22
     static int tcpSocketPort = 6868;
28
     static String HOST = "0.0.0.0" ;
23
     static String HOST = "0.0.0.0" ;
29
 
24
 
30
-
31
     private static CuisineTCPServer fCuisineTCPServer;
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
         webSocketHost = iHostName;
29
         webSocketHost = iHostName;
36
         webPagePort = iWebPagePort;
30
         webPagePort = iWebPagePort;
37
         webSocketPort = iWebSocketPort;
31
         webSocketPort = iWebSocketPort;
38
         tcpSocketPort = iTcpSocketPort;
32
         tcpSocketPort = iTcpSocketPort;
33
+        webSocketProcolPrefix = iWebSocketProtocolPrefix;
34
+        writePortInClientSideURI = iWebSocketUriClPortDisplay;
39
         initResources();
35
         initResources();
40
 
36
 
41
-        //Create CuisineTCPServers
37
+        //Create CuisineTCPServer
42
         fCuisineTCPServer = new CuisineTCPServer(tcpSocketPort);
38
         fCuisineTCPServer = new CuisineTCPServer(tcpSocketPort);
43
-        CuisineWebSocketMessageHandler vWebSocketMessageHandler         = new CuisineWebSocketMessageHandler( fCuisineTCPServer );
44
-        CuisineWebSocketConnectionHandler vWebSocketConnectionHandler   = new CuisineWebSocketConnectionHandler( vWebSocketMessageHandler );
45
 
39
 
46
         //Start HTTP Server : serve webpage with JS that calls the websocket
40
         //Start HTTP Server : serve webpage with JS that calls the websocket
47
         Undertow WebPageServer = Undertow.builder()
41
         Undertow WebPageServer = Undertow.builder()
48
                 .addHttpListener(webPagePort, HOST)
42
                 .addHttpListener(webPagePort, HOST)
49
                 .setHandler(
43
                 .setHandler(
50
-                        new CuisineResponseHandler("ws://"+webSocketHost+":"+webSocketPort+"/data")
44
+                        new CuisineResponseHandler(iWebSocketProtocolPrefix+"://"+webSocketHost+":"+( writePortInClientSideURI ? webSocketPort : "/data" ))
51
                             ).build();
45
                             ).build();
52
         WebPageServer.start();
46
         WebPageServer.start();
53
 
47
 
54
         System.out.println("WebPageServer started");
48
         System.out.println("WebPageServer started");
49
+
50
+        CuisineWebSocketServer vWebSocketServer = new CuisineWebSocketServer(webSocketHost,webSocketPort);
51
+        vWebSocketServer.initTCPServer(fCuisineTCPServer);
55
         //Start websocket
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
         System.out.println("WebSocketServer started");
54
         System.out.println("WebSocketServer started");
65
 
55
 
56
+
57
+
66
     }
58
     }
67
 
59
 
68
     static String sHtml_ASCII_title;
60
     static String sHtml_ASCII_title;
69
     static String sHtml_CSS;
61
     static String sHtml_CSS;
62
+    static String sJS_ClientSide;
70
     static void initResources()
63
     static void initResources()
71
     {
64
     {
72
         try {
65
         try {
73
             sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
66
             sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
74
             sHtml_CSS = new Scanner(CuisineHTTPServer.class.getResourceAsStream("style.css"), "UTF-8").useDelimiter("\\A").next();
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
         catch(Exception vex)
71
         catch(Exception vex)
77
         {
72
         {
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
     static String getHTMLFrontPage(String iWsUri)
100
     static String getHTMLFrontPage(String iWsUri)
150
             .append("   </div>")
113
             .append("   </div>")
151
             .append("   <h1 id=\"tempValue\">None</h1>")
114
             .append("   <h1 id=\"tempValue\">None</h1>")
152
             .append("   <script>")
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+\"&deg;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
             .append("   </script>")
117
             .append("   </script>")
167
             .append("</body>")
118
             .append("</body>")
168
             .append("")
119
             .append("")

src/main/java/CuisineTCPServer.java → src/main/java/tcp/CuisineTCPServer.java View File

1
+package tcp;
2
+
1
 import java.io.*;
3
 import java.io.*;
2
 import java.net.ServerSocket;
4
 import java.net.ServerSocket;
3
 import java.net.Socket;
5
 import java.net.Socket;

+ 77
- 0
src/main/java/websockets/CuisineWebSocketServer.java View File

1
+package websockets;
2
+
3
+import io.undertow.Undertow;
4
+import io.undertow.websockets.WebSocketConnectionCallback;
5
+import io.undertow.websockets.core.AbstractReceiveListener;
6
+import io.undertow.websockets.core.BufferedTextMessage;
7
+import io.undertow.websockets.core.WebSocketChannel;
8
+import io.undertow.websockets.core.WebSockets;
9
+import io.undertow.websockets.spi.WebSocketHttpExchange;
10
+import tcp.CuisineTCPServer;
11
+
12
+import static io.undertow.Handlers.path;
13
+import static io.undertow.Handlers.websocket;
14
+import static io.undertow.Handlers.*;
15
+
16
+public class CuisineWebSocketServer {
17
+
18
+    CuisineWebSocketConnectionHandler fConnectionHandler;
19
+    CuisineWebSocketMessageHandler fMessageHandler;
20
+    Undertow fWebSocketServer;
21
+    String fWebSocketHost;
22
+    int fWebSocketPort;
23
+
24
+    public CuisineWebSocketServer(String iHost, int iPort
25
+    ) {
26
+        fWebSocketPort = iPort;
27
+        fWebSocketHost = iHost;
28
+    }
29
+
30
+    public void initTCPServer(CuisineTCPServer iTcpServer) {
31
+        fMessageHandler = new CuisineWebSocketMessageHandler(iTcpServer);
32
+    }
33
+
34
+    public void start() {
35
+        fConnectionHandler = new CuisineWebSocketConnectionHandler(fMessageHandler);
36
+        System.out.println("Opening web socket server at "+fWebSocketHost+":"+fWebSocketPort);
37
+        fWebSocketServer = Undertow.builder()
38
+                .addHttpListener(fWebSocketPort, "0.0.0.0")
39
+                .setHandler(
40
+                        path()
41
+                                .addPrefixPath("/data", websocket(fConnectionHandler))
42
+                )
43
+                .build();
44
+        fWebSocketServer.start();
45
+    }
46
+
47
+
48
+    private class CuisineWebSocketConnectionHandler implements WebSocketConnectionCallback {
49
+        private CuisineWebSocketMessageHandler fWebSocketMessageHandler;
50
+
51
+        protected CuisineWebSocketConnectionHandler(CuisineWebSocketMessageHandler iWebSocketMessageHandler) {
52
+            fWebSocketMessageHandler = iWebSocketMessageHandler;
53
+        }
54
+
55
+        @Override
56
+        public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
57
+            channel.getReceiveSetter().set(fWebSocketMessageHandler);
58
+            channel.resumeReceives();
59
+        }
60
+    }
61
+
62
+    private class CuisineWebSocketMessageHandler extends AbstractReceiveListener {
63
+
64
+        private CuisineTCPServer fTCPServer;
65
+
66
+        protected CuisineWebSocketMessageHandler(CuisineTCPServer iTCPServer) {
67
+            super();
68
+            fTCPServer = iTCPServer;
69
+        }
70
+
71
+        protected void onFullTextMessage(WebSocketChannel iChannel, BufferedTextMessage iTextMessage) {
72
+            //Handle message on webSocket
73
+            WebSockets.sendText(String.valueOf(fTCPServer.getCurrentTemperature()), iChannel, null);
74
+        }
75
+    }
76
+
77
+}

+ 13
- 0
src/main/resources/clientsidecoms.js View File

1
+var socket=new WebSocket("%WSURI%/data");
2
+let connected=false;
3
+let view=document.getElementById("tempValue");
4
+
5
+socket.onerror=function(error){connected=false;view.innerHTML="oups :(";};
6
+socket.onmessage=function(event){view.innerHTML=event.data+"&deg;C"};
7
+socket.onopen=function(e){connected=true;callServer();};
8
+var callServer=function(){
9
+    socket.send("coucou");
10
+};
11
+
12
+
13
+let timer=setInterval(callServer,100);

Loading…
Cancel
Save