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");
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 = "<b>Connection closed.</b>";

View File

@ -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() {

View File

@ -2,47 +2,53 @@
<html lang="en">
<head>
<title>Command Example</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
window.onload = function () {
var conn;
var msg = $("#msg");
var log = $("#log");
var msg = document.getElementById("msg");
var log = document.getElementById("log");
function appendLog(msg) {
var d = log[0]
var doScroll = d.scrollTop == d.scrollHeight - d.clientHeight;
msg.appendTo(log)
function appendLog(item) {
var doScroll = log.scrollTop > log.scrollHeight - log.clientHeight - 1;
log.appendChild(item);
if (doScroll) {
d.scrollTop = d.scrollHeight - d.clientHeight;
log.scrollTop = log.scrollHeight - log.clientHeight;
}
}
$("#form").submit(function() {
document.getElementById("form").onsubmit = function () {
if (!conn) {
return false;
}
if (!msg.val()) {
if (!msg.value) {
return false;
}
conn.send(msg.val());
msg.val("");
return false
});
conn.send(msg.value);
msg.value = "";
return false;
};
if (window["WebSocket"]) {
conn = new WebSocket("ws://{{$}}/ws");
conn = new WebSocket("ws://" + document.location.host + "/ws");
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) {
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 {
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>
<style type="text/css">
html {

View File

@ -7,7 +7,6 @@ package main
import (
"bufio"
"flag"
"html/template"
"io"
"log"
"net/http"
@ -21,7 +20,6 @@ import (
var (
addr = flag.String("addr", "127.0.0.1:8080", "http service address")
cmdPath string
homeTempl = template.Must(template.ParseFiles("home.html"))
)
const (
@ -176,8 +174,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")
homeTempl.Execute(w, r.Host)
http.ServeFile(w, r, "home.html")
}
func main() {