Browse Source

c pete sans ca

master
DemiSel 3 years ago
parent
commit
ef5baf3024

+ 4
- 5
src/main/java/CuisineHTTPServer.java View File

66
     {
66
     {
67
         try {
67
         try {
68
             sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
68
             sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
69
-            sHtml_CSS = new Scanner(CuisineHTTPServer.class.getResourceAsStream("style.css"), "UTF-8").useDelimiter("\\A").next();
70
-            sJS_ClientSide = new Scanner(CuisineHTTPServer.class.getResourceAsStream("clientsidecoms.js"), "UTF-8").useDelimiter("\\A").next();
71
-            sHtml_Chatbox = new Scanner(CuisineHTTPServer.class.getResourceAsStream("components/chat.html"), "UTF-8").useDelimiter("\\A").next();
72
-            sJS_Chatbox = new Scanner(CuisineHTTPServer.class.getResourceAsStream("components/chatscript.js"), "UTF-8").useDelimiter("\\A").next();
73
-
69
+            sHtml_CSS       = new Scanner(CuisineHTTPServer.class.getResourceAsStream("style.css"), "UTF-8").useDelimiter("\\A").next();
70
+            sJS_ClientSide  = new Scanner(CuisineHTTPServer.class.getResourceAsStream("clientsidecoms.js"), "UTF-8").useDelimiter("\\A").next();
71
+            sHtml_Chatbox   = new Scanner(CuisineHTTPServer.class.getResourceAsStream("components/chat.html"), "UTF-8").useDelimiter("\\A").next();
72
+            sJS_Chatbox     = new Scanner(CuisineHTTPServer.class.getResourceAsStream("components/chatscript.js"), "UTF-8").useDelimiter("\\A").next();
74
         }
73
         }
75
         catch(Exception vex)
74
         catch(Exception vex)
76
         {
75
         {

+ 22
- 26
src/main/java/websockets/CuisineWebSocketServer.java View File

2
 
2
 
3
 import io.undertow.Undertow;
3
 import io.undertow.Undertow;
4
 import io.undertow.websockets.WebSocketConnectionCallback;
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;
5
 import io.undertow.websockets.core.WebSocketChannel;
8
-import io.undertow.websockets.core.WebSockets;
9
 import io.undertow.websockets.spi.WebSocketHttpExchange;
6
 import io.undertow.websockets.spi.WebSocketHttpExchange;
7
+import org.xnio.ChannelListener;
10
 import tcp.CuisineTCPServer;
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
 import static io.undertow.Handlers.path;
13
 import static io.undertow.Handlers.path;
15
 import static io.undertow.Handlers.websocket;
14
 import static io.undertow.Handlers.websocket;
16
-import static io.undertow.Handlers.*;
17
 
15
 
18
 public class CuisineWebSocketServer {
16
 public class CuisineWebSocketServer {
19
 
17
 
20
     CuisineWebSocketConnectionHandler fConnectionHandler;
18
     CuisineWebSocketConnectionHandler fConnectionHandler;
21
     CuisineWebSocketConnectionHandler fChatConnectionHandler;
19
     CuisineWebSocketConnectionHandler fChatConnectionHandler;
22
-    CuisineWebSocketMessageHandler fMessageHandler;
23
-    CuisineWebSocketChichatHandler fChitchatMessageHandler;
20
+    CuisineWebSocketWebSocketTemperatureHandler fMessageHandler;
21
+    CuisineWebSocketWebSocketChichatHandler fChitchatMessageHandler;
24
     Undertow fWebSocketServer;
22
     Undertow fWebSocketServer;
25
     String fWebSocketHost;
23
     String fWebSocketHost;
26
     int fWebSocketPort;
24
     int fWebSocketPort;
25
+    int fConnectedClients;
27
 
26
 
28
     public CuisineWebSocketServer(String iHost, int iPort
27
     public CuisineWebSocketServer(String iHost, int iPort
29
     ) {
28
     ) {
32
     }
31
     }
33
 
32
 
34
     public void initTCPServer(CuisineTCPServer iTcpServer) {
33
     public void initTCPServer(CuisineTCPServer iTcpServer) {
35
-        fMessageHandler = new CuisineWebSocketMessageHandler(iTcpServer);
34
+        fMessageHandler = new CuisineWebSocketWebSocketTemperatureHandler(iTcpServer);
36
     }
35
     }
37
 
36
 
38
     public void start() {
37
     public void start() {
39
-        fChitchatMessageHandler = new CuisineWebSocketChichatHandler();
38
+        fChitchatMessageHandler = new CuisineWebSocketWebSocketChichatHandler();
40
 
39
 
41
         fConnectionHandler = new CuisineWebSocketConnectionHandler(fMessageHandler);
40
         fConnectionHandler = new CuisineWebSocketConnectionHandler(fMessageHandler);
42
         fChatConnectionHandler = new CuisineWebSocketConnectionHandler(fChitchatMessageHandler);
41
         fChatConnectionHandler = new CuisineWebSocketConnectionHandler(fChitchatMessageHandler);
54
 
53
 
55
 
54
 
56
     private class CuisineWebSocketConnectionHandler implements WebSocketConnectionCallback {
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
             fWebSocketMessageHandler = iWebSocketMessageHandler;
59
             fWebSocketMessageHandler = iWebSocketMessageHandler;
61
         }
60
         }
62
 
61
 
63
         @Override
62
         @Override
64
         public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
63
         public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
65
             channel.getReceiveSetter().set(fWebSocketMessageHandler);
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
             channel.resumeReceives();
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
 }

+ 18
- 3
src/main/java/websockets/handlers/CuisineWebSocketReceiveHandler.java View File

3
 import io.undertow.websockets.core.AbstractReceiveListener;
3
 import io.undertow.websockets.core.AbstractReceiveListener;
4
 import io.undertow.websockets.core.WebSocketChannel;
4
 import io.undertow.websockets.core.WebSocketChannel;
5
 
5
 
6
-public class CuisineReceiveListener extends AbstractReceiveListener {
6
+import java.util.ArrayList;
7
 
7
 
8
-    public void register(WebSocketChannel iChannel)
8
+public class CuisineWebSocketReceiveHandler extends AbstractReceiveListener {
9
+
10
+    private ArrayList <WebSocketChannel> fRegisteredChannels;
11
+
12
+    protected CuisineWebSocketReceiveHandler()
13
+    {
14
+        super();
15
+        fRegisteredChannels = new ArrayList<>();
16
+    }
17
+
18
+    public ArrayList <WebSocketChannel> getRegisteredChannels()
9
     {
19
     {
20
+        return fRegisteredChannels;
21
+    }
10
 
22
 
23
+    public void register(WebSocketChannel iChannel)
24
+    {
25
+        fRegisteredChannels.add(iChannel);
11
     }
26
     }
12
 
27
 
13
     public void unregister(WebSocketChannel iChannel)
28
     public void unregister(WebSocketChannel iChannel)
14
     {
29
     {
15
-
30
+        fRegisteredChannels.remove(iChannel);
16
     }
31
     }
17
 }
32
 }

+ 13
- 7
src/main/java/websockets/handlers/CuisineWebSocketWebSocketChichatHandler.java View File

1
 package websockets.handlers;
1
 package websockets.handlers;
2
 
2
 
3
-import io.undertow.websockets.core.AbstractReceiveListener;
4
 import io.undertow.websockets.core.BufferedTextMessage;
3
 import io.undertow.websockets.core.BufferedTextMessage;
5
 import io.undertow.websockets.core.WebSocketChannel;
4
 import io.undertow.websockets.core.WebSocketChannel;
6
 import io.undertow.websockets.core.WebSockets;
5
 import io.undertow.websockets.core.WebSockets;
7
 
6
 
8
 import java.util.ArrayList;
7
 import java.util.ArrayList;
9
 
8
 
10
-public class CuisineWebSocketChichatHandler extends CuisineReceiveListener {
9
+public class CuisineWebSocketWebSocketChichatHandler extends CuisineWebSocketReceiveHandler {
11
 
10
 
12
     final int MAX_MESSAGES = 10;
11
     final int MAX_MESSAGES = 10;
13
     ArrayList<String> vMessageHistory;
12
     ArrayList<String> vMessageHistory;
14
 
13
 
15
-    public CuisineWebSocketChichatHandler() {
14
+    public CuisineWebSocketWebSocketChichatHandler() {
15
+        super();
16
         vMessageHistory = new ArrayList<>();
16
         vMessageHistory = new ArrayList<>();
17
     }
17
     }
18
 
18
 
30
             vBuilder.append(vMessage+"\n");
30
             vBuilder.append(vMessage+"\n");
31
         }
31
         }
32
 
32
 
33
-        WebSockets.sendText(vBuilder.toString(), iChannel, null);
33
+        String vMessage = vBuilder.toString();
34
+        notifyOthers(vMessage);
35
+        WebSockets.sendText(vMessage, iChannel, null);
36
+
34
     }
37
     }
35
 
38
 
36
     public void register(WebSocketChannel iChannel)
39
     public void register(WebSocketChannel iChannel)
37
     {
40
     {
38
-
41
+        super.register(iChannel);
39
     }
42
     }
40
 
43
 
41
     public void unregister(WebSocketChannel iChannel)
44
     public void unregister(WebSocketChannel iChannel)
42
     {
45
     {
43
-
46
+        super.unregister(iChannel);
44
     }
47
     }
45
 
48
 
46
     protected void notifyOthers(String iMessages)
49
     protected void notifyOthers(String iMessages)
47
     {
50
     {
48
-        Websockets.sendText(iMessages)
51
+        for( WebSocketChannel vChannel : this.getRegisteredChannels() )
52
+        {
53
+            WebSockets.sendText(iMessages, vChannel,null);
54
+        }
49
     }
55
     }
50
 
56
 
51
 }
57
 }

+ 2
- 2
src/main/java/websockets/handlers/CuisineWebSocketWebSocketTemperatureHandler.java View File

6
 import io.undertow.websockets.core.WebSockets;
6
 import io.undertow.websockets.core.WebSockets;
7
 import tcp.CuisineTCPServer;
7
 import tcp.CuisineTCPServer;
8
 
8
 
9
-public class CuisineWebSocketWebSocketMessageHandler extends CuisineWebSocketReceiveHandler {
9
+public class CuisineWebSocketWebSocketTemperatureHandler extends CuisineWebSocketReceiveHandler {
10
 
10
 
11
     private CuisineTCPServer fTCPServer;
11
     private CuisineTCPServer fTCPServer;
12
 
12
 
13
-    public CuisineWebSocketWebSocketMessageHandler(CuisineTCPServer iTCPServer) {
13
+    public CuisineWebSocketWebSocketTemperatureHandler(CuisineTCPServer iTCPServer) {
14
         super();
14
         super();
15
         fTCPServer = iTCPServer;
15
         fTCPServer = iTCPServer;
16
     }
16
     }

+ 4
- 4
src/main/resources/components/chat.html View File

1
 <div id="chatbox" >
1
 <div id="chatbox" >
2
-    <div id="chatboxcontent">
2
+    <div id="chatboxcontent" class="tile">
3
 
3
 
4
     </div>
4
     </div>
5
-    <div id="chatboxinput">
5
+    <form id="chatboxinput" action="javascript:void(0);">
6
         <input type="text" id="chatboxinputtext">
6
         <input type="text" id="chatboxinputtext">
7
-        <input type="button" id="chatboxinputbutton" value="ok"/>
8
-    </div>
7
+        <input type="submit"  action="submit" id="chatboxinputbutton" value="ok"/>
8
+    </form>
9
 </div>
9
 </div>

+ 5
- 4
src/main/resources/components/chatscript.js View File

7
 
7
 
8
     let chatInput = document.getElementById("chatboxinputtext");
8
     let chatInput = document.getElementById("chatboxinputtext");
9
 
9
 
10
-    let chatInputButton = document.getElementById("chatboxinputbutton");
11
-    chatInputButton.onclick = function()
10
+    let chatInputButton = document.getElementById("chatboxinput");
11
+    chatInputButton.onsubmit = function()
12
     {
12
     {
13
         input = chatInput.value;
13
         input = chatInput.value;
14
+        chatInput.value="";
14
         console.log(input);
15
         console.log(input);
15
         chatSocket.send(input);
16
         chatSocket.send(input);
16
     };
17
     };
17
 
18
 
18
     chatSocket.onerror=function(error){connected=false;view.innerHTML="oups :(";};
19
     chatSocket.onerror=function(error){connected=false;view.innerHTML="oups :(";};
19
-    chatSocket.onmessage=function(event){view.innerHTML="<pre>"+event.data+"</pre>";};
20
+    chatSocket.onmessage=function(event){view.innerHTML=event.data;};
20
     chatSocket.onopen=function(e){connected=true;callServer();};
21
     chatSocket.onopen=function(e){connected=true;callServer();};
21
     var callServer=function(){
22
     var callServer=function(){
22
         chatSocket.send("R0VU");
23
         chatSocket.send("R0VU");
23
     };
24
     };
24
 
25
 
25
-    let timer=setInterval(callServer,1000);
26
+   // let timer=setInterval(callServer,1000);
26
 
27
 
27
 });
28
 });

+ 9
- 0
src/main/resources/style.css View File

3
     white-space: pre;
3
     white-space: pre;
4
 }
4
 }
5
 
5
 
6
+.tile{
7
+    display: bloc;
8
+    width: 25%;
9
+    overflow-x: hidden;
10
+    overflow-y: auto;
11
+    white-space: pre-line;
12
+    word-wrap: break-word;
13
+}
14
+
6
 .centered{
15
 .centered{
7
     margin : auto;
16
     margin : auto;
8
     display: bloc;
17
     display: bloc;

Loading…
Cancel
Save