Skip to content

Instantly share code, notes, and snippets.

@jordanorelli
Created August 4, 2012 17:12
Show Gist options
  • Save jordanorelli/3258815 to your computer and use it in GitHub Desktop.
Save jordanorelli/3258815 to your computer and use it in GitHub Desktop.
package main
import (
"code.google.com/p/go.net/websocket"
"io"
"log"
"net/http"
"time"
)
func hello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, page)
}
func socketHandler(conn *websocket.Conn) {
for {
_, err := io.WriteString(conn, "hello socket")
if err != nil {
log.Println("ERROR:", err)
return
}
time.Sleep(1 * time.Second)
}
}
func main() {
http.Handle("/ws", websocket.Handler(socketHandler))
http.HandleFunc("/", hello)
if err := http.ListenAndServe(":8000", nil); err != nil {
log.Fatal(err)
}
}
var page = `
<html>
<head>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function(){
var conn = new WebSocket("ws://localhost:8000/ws");
conn.onopen = function(event) {
console.log(event);
};
conn.onerror = function(event) {
console.log(event);
};
conn.onmessage = function(event) {
$("#messages").append("<li>" + event.data + "</li>");
console.log(event);
};
});
</script>
</head>
<body>
oh hai
<ul id="messages">
<!-- stuff should go here -->
</ul>
</body>
</html
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment