From 5ade364dff1f03abefdae800ed19074bf707b08f Mon Sep 17 00:00:00 2001 From: Gary Burd Date: Wed, 15 Feb 2017 17:01:07 -0800 Subject: [PATCH] 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. --- examples/chat/home.html | 4 +-- examples/chat/main.go | 5 +--- examples/command/home.html | 56 +++++++++++++++++++++----------------- examples/command/main.go | 9 ++---- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/examples/chat/home.html b/examples/chat/home.html index 7262918..a39a0c2 100644 --- a/examples/chat/home.html +++ b/examples/chat/home.html @@ -9,7 +9,7 @@ window.onload = function () { var log = document.getElementById("log"); function appendLog(item) { - var doScroll = log.scrollTop === log.scrollHeight - log.clientHeight; + var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1; log.appendChild(item); if (doScroll) { log.scrollTop = log.scrollHeight - log.clientHeight; @@ -29,7 +29,7 @@ window.onload = function () { }; if (window["WebSocket"]) { - conn = new WebSocket("ws://{{$}}/ws"); + conn = new WebSocket("ws://" + document.location.host + "/ws"); conn.onclose = function (evt) { var item = document.createElement("div"); item.innerHTML = "Connection closed."; diff --git a/examples/chat/main.go b/examples/chat/main.go index d08f895..74615d5 100644 --- a/examples/chat/main.go +++ b/examples/chat/main.go @@ -6,13 +6,11 @@ package main import ( "flag" - "html/template" "log" "net/http" ) 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) { log.Println(r.URL) @@ -24,8 +22,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) { http.Error(w, "Method not allowed", 405) return } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - homeTemplate.Execute(w, r.Host) + http.ServeFile(w, r, "home.html") } func main() { diff --git a/examples/command/home.html b/examples/command/home.html index 72fd02b..19c4612 100644 --- a/examples/command/home.html +++ b/examples/command/home.html @@ -2,47 +2,53 @@ Command Example -