-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
46 lines (40 loc) · 1.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"code.google.com/p/go.net/websocket"
"flag"
"fmt"
"github.com/oskarth/wsevents"
"log"
"net/http"
"text/template"
)
var addr = flag.String("addr", ":8080", "http service address")
var homeTempl = template.Must(template.ParseFiles("index.html"))
func homeHandler(c http.ResponseWriter, req *http.Request) {
homeTempl.Execute(c, req.Host)
}
func resourceHandler(c http.ResponseWriter, req *http.Request) {
fmt.Print("Serving "+req.URL.Path[1:], "\n")
http.ServeFile(c, req, req.URL.Path[1:])
}
func main() {
flag.Parse()
wsevents.Connect(func(c *wsevents.Connection) {
// wrap event with color before sending it
c.On("chat msg", func(msg interface{}) {
d := map[string]string{
"msg": msg.(string),
"color": "#999",
}
c.Broadcast("chat msg", d)
})
})
go wsevents.Run()
http.HandleFunc("/", homeHandler)
http.HandleFunc("/resources/webrtc.io.js", resourceHandler)
http.HandleFunc("/resources/style.css", resourceHandler)
http.Handle("/ws", websocket.Handler(wsevents.Handler))
if err := http.ListenAndServe(*addr, nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}