瀏覽代碼

réparé des trucs jcrois

master
DemiSel 3 年之前
父節點
當前提交
646de7a2d7

+ 7
- 1
src/main/java/CuisineHTTPServer.java 查看文件

@@ -41,7 +41,7 @@ public class CuisineHTTPServer extends Thread
41 41
         Undertow WebPageServer = Undertow.builder()
42 42
                 .addHttpListener(webPagePort, HOST)
43 43
                 .setHandler(
44
-                        new CuisineResponseHandler(iWebSocketProtocolPrefix+"://"+webSocketHost+":"+( writePortInClientSideURI ? webSocketPort : "/data" ))
44
+                        new CuisineResponseHandler(iWebSocketProtocolPrefix+"://"+webSocketHost+":"+( writePortInClientSideURI ? webSocketPort : "" ))
45 45
                             ).build();
46 46
         WebPageServer.start();
47 47
 
@@ -60,12 +60,16 @@ public class CuisineHTTPServer extends Thread
60 60
     static String sHtml_ASCII_title;
61 61
     static String sHtml_CSS;
62 62
     static String sJS_ClientSide;
63
+    static String sHtml_Chatbox;
64
+    static String sJS_Chatbox;
63 65
     static void initResources()
64 66
     {
65 67
         try {
66 68
             sHtml_ASCII_title = new Scanner(CuisineHTTPServer.class.getResourceAsStream("html_ascii_title"), "UTF-8").useDelimiter("\\A").next();
67 69
             sHtml_CSS = new Scanner(CuisineHTTPServer.class.getResourceAsStream("style.css"), "UTF-8").useDelimiter("\\A").next();
68 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();
69 73
 
70 74
         }
71 75
         catch(Exception vex)
@@ -112,8 +116,10 @@ public class CuisineHTTPServer extends Thread
112 116
             .append(sHtml_ASCII_title)
113 117
             .append("   </div>")
114 118
             .append("   <h1 id=\"tempValue\">None</h1>")
119
+            .append(sHtml_Chatbox.replace("%WSURI%",iWsUri))
115 120
             .append("   <script>")
116 121
             .append(sJS_ClientSide.replace("%WSURI%",iWsUri))
122
+            .append(sJS_Chatbox.replace("%WSURI%",iWsUri))
117 123
             .append("   </script>")
118 124
             .append("</body>")
119 125
             .append("")

+ 14
- 4
src/main/java/websockets/CuisineWebSocketChichatHandler.java 查看文件

@@ -9,18 +9,28 @@ import java.util.ArrayList;
9 9
 
10 10
 public class CuisineWebSocketChichatHandler extends AbstractReceiveListener {
11 11
 
12
-    final int MAX_MESSAGES = 5;
12
+    final int MAX_MESSAGES = 10;
13 13
     ArrayList<String> vMessageHistory;
14 14
 
15 15
     protected CuisineWebSocketChichatHandler() {
16
-
16
+        vMessageHistory = new ArrayList<>();
17 17
     }
18 18
 
19 19
     protected void onFullTextMessage(WebSocketChannel iChannel, BufferedTextMessage iTextMessage) {
20 20
         String vMessageContent = iTextMessage.getData();
21
-        System.out.println("Received " + iTextMessage.toString());
21
+        if(!vMessageContent.equals("R0VU") && vMessageContent.length()>0 && vMessageContent.length()<250)
22
+            vMessageHistory.add(vMessageContent);
23
+        if(vMessageHistory.size() > MAX_MESSAGES)
24
+        {
25
+            vMessageHistory.remove(0);
26
+        }
27
+        StringBuilder vBuilder = new StringBuilder();
28
+        for(String vMessage : vMessageHistory)
29
+        {
30
+            vBuilder.append(vMessage+"\n");
31
+        }
22 32
         //Handle message on webSocket
23
-        WebSockets.sendText(vMessageContent, iChannel, null);
33
+        WebSockets.sendText(vBuilder.toString(), iChannel, null);
24 34
     }
25 35
 
26 36
 }

+ 10
- 2
src/main/java/websockets/CuisineWebSocketServer.java 查看文件

@@ -9,6 +9,8 @@ import io.undertow.websockets.core.WebSockets;
9 9
 import io.undertow.websockets.spi.WebSocketHttpExchange;
10 10
 import tcp.CuisineTCPServer;
11 11
 
12
+import java.util.ArrayList;
13
+
12 14
 import static io.undertow.Handlers.path;
13 15
 import static io.undertow.Handlers.websocket;
14 16
 import static io.undertow.Handlers.*;
@@ -16,7 +18,9 @@ import static io.undertow.Handlers.*;
16 18
 public class CuisineWebSocketServer {
17 19
 
18 20
     CuisineWebSocketConnectionHandler fConnectionHandler;
21
+    CuisineWebSocketConnectionHandler fChatConnectionHandler;
19 22
     CuisineWebSocketMessageHandler fMessageHandler;
23
+    CuisineWebSocketChichatHandler fChitchatMessageHandler;
20 24
     Undertow fWebSocketServer;
21 25
     String fWebSocketHost;
22 26
     int fWebSocketPort;
@@ -32,13 +36,17 @@ public class CuisineWebSocketServer {
32 36
     }
33 37
 
34 38
     public void start() {
39
+        fChitchatMessageHandler = new CuisineWebSocketChichatHandler();
40
+
35 41
         fConnectionHandler = new CuisineWebSocketConnectionHandler(fMessageHandler);
42
+        fChatConnectionHandler = new CuisineWebSocketConnectionHandler(fChitchatMessageHandler);
36 43
         System.out.println("Opening web socket server at "+fWebSocketHost+":"+fWebSocketPort);
37 44
         fWebSocketServer = Undertow.builder()
38 45
                 .addHttpListener(fWebSocketPort, "0.0.0.0")
39 46
                 .setHandler(
40 47
                         path()
41 48
                                 .addPrefixPath("/data", websocket(fConnectionHandler))
49
+                                .addPrefixPath("/chat", websocket(fChatConnectionHandler))
42 50
                 )
43 51
                 .build();
44 52
         fWebSocketServer.start();
@@ -46,9 +54,9 @@ public class CuisineWebSocketServer {
46 54
 
47 55
 
48 56
     private class CuisineWebSocketConnectionHandler implements WebSocketConnectionCallback {
49
-        private CuisineWebSocketMessageHandler fWebSocketMessageHandler;
57
+        private AbstractReceiveListener fWebSocketMessageHandler;
50 58
 
51
-        protected CuisineWebSocketConnectionHandler(CuisineWebSocketMessageHandler iWebSocketMessageHandler) {
59
+        protected CuisineWebSocketConnectionHandler(AbstractReceiveListener iWebSocketMessageHandler) {
52 60
             fWebSocketMessageHandler = iWebSocketMessageHandler;
53 61
         }
54 62
 

+ 14
- 10
src/main/resources/clientsidecoms.js 查看文件

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

+ 9
- 10
src/main/resources/components/chat.html 查看文件

@@ -1,10 +1,9 @@
1
-<!DOCTYPE html>
2
-<html lang="en">
3
-<head>
4
-  <meta charset="UTF-8">
5
-  <title>$Title$</title>
6
-</head>
7
-<body>
8
-$END$
9
-</body>
10
-</html>
1
+<div id="chatbox" >
2
+    <div id="chatboxcontent">
3
+
4
+    </div>
5
+    <div id="chatboxinput">
6
+        <input type="text" id="chatboxinputtext">
7
+        <input type="button" id="chatboxinputbutton" value="ok"/>
8
+    </div>
9
+</div>

+ 27
- 0
src/main/resources/components/chatscript.js 查看文件

@@ -0,0 +1,27 @@
1
+var chatSocket=new WebSocket("%WSURI%/chat");
2
+    document.addEventListener("DOMContentLoaded", (event) => {
3
+
4
+
5
+    let connected=false;
6
+    let view=document.getElementById("chatboxcontent");
7
+
8
+    let chatInput = document.getElementById("chatboxinputtext");
9
+
10
+    let chatInputButton = document.getElementById("chatboxinputbutton");
11
+    chatInputButton.onclick = function()
12
+    {
13
+        input = chatInput.value;
14
+        console.log(input);
15
+        chatSocket.send(input);
16
+    };
17
+
18
+    chatSocket.onerror=function(error){connected=false;view.innerHTML="oups :(";};
19
+    chatSocket.onmessage=function(event){view.innerHTML="<pre>"+event.data+"</pre>";};
20
+    chatSocket.onopen=function(e){connected=true;callServer();};
21
+    var callServer=function(){
22
+        chatSocket.send("R0VU");
23
+    };
24
+
25
+    let timer=setInterval(callServer,1000);
26
+
27
+});

Loading…
取消
儲存