转移
This commit is contained in:
		
							
								
								
									
										44
									
								
								templates/websocket.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								templates/websocket.html
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html>
 | 
			
		||||
    <head>
 | 
			
		||||
        <title>WebSocket Demo</title>
 | 
			
		||||
    </head>
 | 
			
		||||
    <body>
 | 
			
		||||
        <div>
 | 
			
		||||
            <p>{{ task_list }}</p>
 | 
			
		||||
            <button id="create_task">Create Task</button>
 | 
			
		||||
        </div>
 | 
			
		||||
        <h1>WebSocket Demo</h1>
 | 
			
		||||
        <div id="message"></div>
 | 
			
		||||
        <button id="create">Create WebSocket</button>
 | 
			
		||||
        <button id="close">Close WebSocket</button>
 | 
			
		||||
        <script>
 | 
			
		||||
            function createWebSocket(id) {
 | 
			
		||||
                var ws = new WebSocket("ws://localhost:5001/api/task/"+id);
 | 
			
		||||
                ws.onmessage = function(event) {
 | 
			
		||||
                    var message = event.data;
 | 
			
		||||
                    document.getElementById("message").innerHTML += message + "<br>";
 | 
			
		||||
                };
 | 
			
		||||
                ws.onclose = function(event) {
 | 
			
		||||
                    document.getElementById("message").innerHTML += "WebSocket closed";
 | 
			
		||||
                };
 | 
			
		||||
                return ws;
 | 
			
		||||
            }
 | 
			
		||||
            document.getElementById("create_task").onclick = function() {
 | 
			
		||||
                fetch("/api/task", {
 | 
			
		||||
                    method: "POST",
 | 
			
		||||
                    headers: {
 | 
			
		||||
                        "Content-Type": "application/json"
 | 
			
		||||
                    },
 | 
			
		||||
                    body: JSON.stringify({
 | 
			
		||||
                        "name": "Task 1",
 | 
			
		||||
                        "description": "Task 1 description"
 | 
			
		||||
                    })
 | 
			
		||||
                }).then(res => res.json()).then(data => {
 | 
			
		||||
                    console.log(data)
 | 
			
		||||
                    var ws = createWebSocket(data.id);
 | 
			
		||||
                });
 | 
			
		||||
            };
 | 
			
		||||
        </script>
 | 
			
		||||
    </body>
 | 
			
		||||
</html>
 | 
			
		||||
		Reference in New Issue
	
	Block a user