github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2012/chat/http-noembed/html.go (about) 1 // +build OMIT 2 3 package main 4 5 import "html/template" 6 import "net/http" 7 8 func rootHandler(w http.ResponseWriter, r *http.Request) { 9 rootTemplate.Execute(w, listenAddr) 10 } 11 12 var rootTemplate = template.Must(template.New("root").Parse(` 13 <!DOCTYPE html> 14 <html> 15 <head> 16 <meta charset="utf-8" /> 17 <script> 18 19 var input, output, websocket; 20 21 function showMessage(m) { 22 var p = document.createElement("p"); 23 p.innerHTML = m; 24 output.appendChild(p); 25 } 26 27 function onMessage(e) { 28 showMessage(e.data); 29 } 30 31 function onClose() { 32 showMessage("Connection closed."); 33 } 34 35 function sendMessage() { 36 var m = input.value; 37 input.value = ""; 38 websocket.send(m); 39 showMessage(m); 40 } 41 42 function onKey(e) { 43 if (e.keyCode == 13) { 44 sendMessage(); 45 } 46 } 47 48 function init() { 49 input = document.getElementById("input"); 50 input.addEventListener("keyup", onKey, false); 51 52 output = document.getElementById("output"); 53 54 websocket = new WebSocket("ws://{{.}}/socket"); 55 websocket.onmessage = onMessage; 56 websocket.onclose = onClose; 57 } 58 59 window.addEventListener("load", init, false); 60 61 </script> 62 </head> 63 <body> 64 <input id="input" type="text"> 65 <div id="output"></div> 66 </body> 67 </html> 68 `))