Skip to content

Instantly share code, notes, and snippets.

@mattn
Created July 5, 2011 02:40
Show Gist options
  • Save mattn/1064173 to your computer and use it in GitHub Desktop.
Save mattn/1064173 to your computer and use it in GitHub Desktop.
Go round the face
package main
import (
"http"
"websocket"
"syscall"
)
var aa = []string{
"(´・ω・`)",
"( ´・ω・)",
"( ´・ω)",
"(   ´・)",
"(   ´)",
"(    )",
"(`   )",
"(・`  )",
"(ω・` )",
"(・ω・` )",
}
func main() {
conns := make(map[*websocket.Conn]chan string)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<!doctype html>
<head>
<title>hmm.........</title>
<style>
body {
text-align : center;
}
#face {
margin: 0 auto;
width: 500px;
font: normal 100px arial, sans-serif;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
var conn = new WebSocket("ws://"+location.host+"/ws");
conn.onclose = function(e) {}
conn.onmessage = function(e) { $("#face").text(e.data) }
});
</script>
</head>
<body>
<div id="face"></div>
</body>
`))
})
go func() {
i := 0
for {
for _, c := range conns {
c <- aa[i]
}
syscall.Sleep(1e8)
i = (i + 1) % len(aa)
}
}()
http.Handle("/ws", websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
face := make(chan string)
conns[ws] = face
for {
_, err := ws.Write([]byte(<-face))
if err != nil {
break
}
}
conns[ws] = nil, false
}))
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment