Created
July 5, 2011 02:40
-
-
Save mattn/1064173 to your computer and use it in GitHub Desktop.
Go round the face
This file contains 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 | |
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