Skip to content
Myr

chat.html

This file

Path

examples/chat/chat.html

52 lines · 1.9 KiB · HTML

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than dcbcf5c point at it.

axum-extra: Make typed-routing feature enable routing feature (#3514)tottotobefore adoptiondcbcf5c

lines 1–52why, one step back
1<!DOCTYPE html>
2<html lang="en">3 <head>4 <meta charset="UTF-8">5 <title>WebSocket Chat</title>6 </head>7 <body>8 <h1>WebSocket Chat Example</h1>910 <input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">11 <button id="join-chat" type="button">Join Chat</button>12 <textarea id="chat" style="display:block; width:600px; height:400px; box-sizing: border-box" cols="30" rows="10"></textarea>13 <input id="input" style="display:block; width:600px; box-sizing: border-box" type="text" placeholder="chat">1415 <script>16 const username = document.querySelector("#username");17 const join_btn = document.querySelector("#join-chat");18 const textarea = document.querySelector("#chat");19 const input = document.querySelector("#input");2021 join_btn.addEventListener("click", function(e) {22 this.disabled = true;2324 const websocket = new WebSocket("ws://localhost:3000/websocket");2526 websocket.onopen = function() {27 console.log("connection opened");28 websocket.send(username.value);29 }3031 const btn = this;3233 websocket.onclose = function() {34 console.log("connection closed");35 btn.disabled = false;36 }3738 websocket.onmessage = function(e) {39 console.log("received message: "+e.data);40 textarea.value += e.data+"\r\n";41 }4243 input.onkeydown = function(e) {44 if (e.key == "Enter") {45 websocket.send(input.value);46 input.value = "";47 }48 }49 });50 </script>51 </body>52</html>