Improve examples

- Remove jQuery dependency in command example.
- Fix auto scroll on new content in chat and command example.
- Update chat and command example to use a static HTML file.
This commit is contained in:
Gary Burd 2017-02-15 17:01:07 -08:00
parent 804cb600d0
commit 5ade364dff
4 changed files with 37 additions and 37 deletions

View File

@ -9,7 +9,7 @@ window.onload = function () {
var log = document.getElementById("log"); var log = document.getElementById("log");
function appendLog(item) { function appendLog(item) {
var doScroll = log.scrollTop === log.scrollHeight - log.clientHeight; var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1;
log.appendChild(item); log.appendChild(item);
if (doScroll) { if (doScroll) {
log.scrollTop = log.scrollHeight - log.clientHeight; log.scrollTop = log.scrollHeight - log.clientHeight;
@ -29,7 +29,7 @@ window.onload = function () {
}; };
if (window["WebSocket"]) { if (window["WebSocket"]) {
conn = new WebSocket("ws://{{$}}/ws"); conn = new WebSocket("ws://" + document.location.host + "/ws");
conn.onclose = function (evt) { conn.onclose = function (evt) {
var item = document.createElement("div"); var item = document.createElement("div");
item.innerHTML = "<b>Connection closed.</b>"; item.innerHTML = "<b>Connection closed.</b>";

View File

@ -6,13 +6,11 @@ package main
import ( import (
"flag" "flag"
"html/template"
"log" "log"
"net/http" "net/http"
) )
var addr = flag.String("addr", ":8080", "http service address") var addr = flag.String("addr", ":8080", "http service address")
var homeTemplate = template.Must(template.ParseFiles("home.html"))
func serveHome(w http.ResponseWriter, r *http.Request) { func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL) log.Println(r.URL)
@ -24,8 +22,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method not allowed", 405) http.Error(w, "Method not allowed", 405)
return return
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8") http.ServeFile(w, r, "home.html")
homeTemplate.Execute(w, r.Host)
} }
func main() { func main() {

View File

@ -2,47 +2,53 @@
<html lang="en"> <html lang="en">
<head> <head>
<title>Command Example</title> <title>Command Example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function() { window.onload = function () {
var conn; var conn;
var msg = $("#msg"); var msg = document.getElementById("msg");
var log = $("#log"); var log = document.getElementById("log");
function appendLog(msg) { function appendLog(item) {
var d = log[0] var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1;
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight; log.appendChild(item);
msg.appendTo(log)
if (doScroll) { if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight; log.scrollTop = log.scrollHeight - log.clientHeight;
} }
} }
$("#form").submit(function() { document.getElementById("form").onsubmit = function () {
if (!conn) { if (!conn) {
return false; return false;
} }
if (!msg.val()) { if (!msg.value) {
return false; return false;
} }
conn.send(msg.val()); conn.send(msg.value);
msg.val(""); msg.value = "";
return false return false;
}); };
if (window["WebSocket"]) { if (window["WebSocket"]) {
conn = new WebSocket("ws://{{$}}/ws"); conn = new WebSocket("ws://" + document.location.host + "/ws");
conn.onclose = function (evt) { conn.onclose = function (evt) {
appendLog($("<div><b>Connection closed.</b></div>")) var item = document.createElement("div");
} item.innerHTML = "<b>Connection closed.</b>";
appendLog(item);
};
conn.onmessage = function (evt) { conn.onmessage = function (evt) {
appendLog($("<pre/>").text(evt.data)) var messages = evt.data.split('\n');
for (var i = 0; i < messages.length; i++) {
var item = document.createElement("div");
item.innerText = messages[i];
appendLog(item);
} }
};
} else { } else {
appendLog($("<div><b>Your browser does not support WebSockets.</b></div>")) var item = document.createElement("div");
item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
appendLog(item);
} }
}); };
</script> </script>
<style type="text/css"> <style type="text/css">
html { html {

View File

@ -7,7 +7,6 @@ package main
import ( import (
"bufio" "bufio"
"flag" "flag"
"html/template"
"io" "io"
"log" "log"
"net/http" "net/http"
@ -21,7 +20,6 @@ import (
var ( var (
addr = flag.String("addr", "127.0.0.1:8080", "http service address") addr = flag.String("addr", "127.0.0.1:8080", "http service address")
cmdPath string cmdPath string
homeTempl = template.Must(template.ParseFiles("home.html"))
) )
const ( const (
@ -176,8 +174,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Method not allowed", 405) http.Error(w, "Method not allowed", 405)
return return
} }
w.Header().Set("Content-Type", "text/html; charset=utf-8") http.ServeFile(w, r, "home.html")
homeTempl.Execute(w, r.Host)
} }
func main() { func main() {