Created
December 9, 2012 09:25
-
-
Save koyachi/4244024 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// Usage: | |
// % tail -f a.txt | go run wstail.go | |
import ( | |
"code.google.com/p/go.net/websocket" | |
"fmt" | |
"io" | |
"net/http" | |
"os" | |
) | |
func indexHandler(w http.ResponseWriter, req *http.Request) { | |
io.WriteString(w, `<html> | |
<head> | |
<script type="text/javascript"> | |
var path; | |
var ws; | |
function init() { | |
console.log("init"); | |
if (ws != null) { | |
ws.close(); | |
ws = null; | |
} | |
//path = document.msgform.path.value; | |
path = "/tail"; | |
console.log("path:" + path); | |
var div = document.getElementById("msg"); | |
div.innerText = "path:" + path + "\n" + div.innerText; | |
ws = new WebSocket("ws://localhost:23456" + path); | |
ws.onopen = function () { | |
div.innerText = "opened\n" + div.innerText; | |
}; | |
ws.onmessage = function (e) { | |
div.innerText = "msg:" + e.data + "\n" + div.innerText; | |
if (e.data instanceof ArrayBuffer) { | |
s = "ArrayBuffer: " + e.data.byteLength + "["; | |
var view = new Uint8Array(e.data); | |
for (var i = 0; i < view.length; ++i) { | |
s += " " + view[i]; | |
} | |
s += "]"; | |
div.innerText = s + "\n" + div.innerText; | |
} | |
}; | |
ws.onclose = function (e) { | |
div.innerText = "closed\n" + div.innerText; | |
}; | |
console.log("init"); | |
div.innerText = "init\n" + div.innerText; | |
}; | |
function send() { | |
console.log("send"); | |
var m = document.msgform.message.value; | |
if (path == "/sendRecvArrayBuffer" || path == "/sendRecvBlob") { | |
var t = m; | |
if (t != "") { | |
var array = new Uint8Array(t.length); | |
for (var i = 0; i < t.length; i++) { | |
array[i] = t.charCodeAt(i); | |
} | |
m = array.buffer; | |
} else { | |
m = document.msgform.file.files[0]; | |
} | |
} else if (path == "/json") { | |
m = JSON.stringify({Msg: m, Path: path}) | |
} | |
console.log("send:" + m); | |
if (m instanceof ArrayBuffer) { | |
var s = "arrayBuffer:" + m.byteLength + "["; | |
var view = new Uint8Array(m); | |
for (var i = 0; i < m.byteLength; ++i) { | |
s += " " + view[i]; | |
} | |
s += "]"; | |
console.log(s); | |
} | |
ws.send(m); | |
return false; | |
}; | |
</script> | |
<body onLoad="init();"> | |
<form name="msgform" action="#" onsubmit="return send();"> | |
<input type="text" name="message" size="80" value=""> | |
<input type="file" name="file" > | |
<input type="submit" value="send"> | |
</form> | |
<div id="msg"></div> | |
</html> | |
`) | |
} | |
func main() { | |
http.Handle("/tail", websocket.Handler(func() func(*websocket.Conn) { | |
return func(ws *websocket.Conn) { | |
fmt.Printf("tailHandler %v\n", ws) | |
for { | |
written, err := io.Copy(ws, os.Stdin) | |
fmt.Println("---") | |
fmt.Println(written) | |
fmt.Println("---") | |
fmt.Println(err) | |
fmt.Println("---") | |
} | |
fmt.Println("tailHandler finished") | |
} | |
}())) | |
http.HandleFunc("/", indexHandler) | |
fmt.Println("start wstail...") | |
err := http.ListenAndServe(":23456", nil) | |
if err != nil { | |
panic("ListenAndServe: " + err.Error()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment