Sfoglia il codice sorgente

c pete sans ca

master
DemiSel 3 anni fa
parent
commit
ef5baf3024

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

@@ -66,11 +66,10 @@ public class CuisineHTTPServer extends Thread
66 66
     {
67 67
         try {
68 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 74
         catch(Exception vex)
76 75
         {

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

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

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

@@ -3,15 +3,30 @@ package websockets.handlers;
3 3
 import io.undertow.websockets.core.AbstractReceiveListener;
4 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 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 Vedi File

@@ -1,18 +1,18 @@
1 1
 package websockets.handlers;
2 2
 
3
-import io.undertow.websockets.core.AbstractReceiveListener;
4 3
 import io.undertow.websockets.core.BufferedTextMessage;
5 4
 import io.undertow.websockets.core.WebSocketChannel;
6 5
 import io.undertow.websockets.core.WebSockets;
7 6
 
8 7
 import java.util.ArrayList;
9 8
 
10
-public class CuisineWebSocketChichatHandler extends CuisineReceiveListener {
9
+public class CuisineWebSocketWebSocketChichatHandler extends CuisineWebSocketReceiveHandler {
11 10
 
12 11
     final int MAX_MESSAGES = 10;
13 12
     ArrayList<String> vMessageHistory;
14 13
 
15
-    public CuisineWebSocketChichatHandler() {
14
+    public CuisineWebSocketWebSocketChichatHandler() {
15
+        super();
16 16
         vMessageHistory = new ArrayList<>();
17 17
     }
18 18
 
@@ -30,22 +30,28 @@ public class CuisineWebSocketChichatHandler extends CuisineReceiveListener {
30 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 39
     public void register(WebSocketChannel iChannel)
37 40
     {
38
-
41
+        super.register(iChannel);
39 42
     }
40 43
 
41 44
     public void unregister(WebSocketChannel iChannel)
42 45
     {
43
-
46
+        super.unregister(iChannel);
44 47
     }
45 48
 
46 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 Vedi File

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

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

@@ -1,9 +1,9 @@
1 1
 <div id="chatbox" >
2
-    <div id="chatboxcontent">
2
+    <div id="chatboxcontent" class="tile">
3 3
 
4 4
     </div>
5
-    <div id="chatboxinput">
5
+    <form id="chatboxinput" action="javascript:void(0);">
6 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 9
 </div>

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

@@ -7,21 +7,22 @@ var chatSocket=new WebSocket("%WSURI%/chat");
7 7
 
8 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 13
         input = chatInput.value;
14
+        chatInput.value="";
14 15
         console.log(input);
15 16
         chatSocket.send(input);
16 17
     };
17 18
 
18 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 21
     chatSocket.onopen=function(e){connected=true;callServer();};
21 22
     var callServer=function(){
22 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 Vedi File

@@ -3,6 +3,15 @@
3 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 15
 .centered{
7 16
     margin : auto;
8 17
     display: bloc;

Loading…
Annulla
Salva