summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go25
-rw-r--r--websock.go40
2 files changed, 65 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..c18959a
--- /dev/null
+++ b/main.go
@@ -0,0 +1,25 @@
+package main
+
+import (
+ "flag"
+ "github.com/gorilla/mux"
+ "net/http"
+)
+
+var (
+ laddr = flag.String("laddr", ":8080", "Listen on this address")
+ tplpath = flag.String("tplpath", "tpls", "Path to templates")
+ staticpath = flag.String("staticpath", "static", "Path to static page elements")
+)
+
+func main() {
+ flag.Parse()
+
+ r := mux.NewRouter()
+ r.HandleFunc("/", Home)
+ r.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(*staticpath))))
+ r.HandleFunc("/chat/{chatroom}/", Chatpage)
+ r.HandleFunc("/chat/{chatroom}/socket", AcceptWebSock)
+ http.Handle("/", r)
+ http.ListenAndServe(*laddr, nil)
+}
diff --git a/websock.go b/websock.go
new file mode 100644
index 0000000..a72bf8c
--- /dev/null
+++ b/websock.go
@@ -0,0 +1,40 @@
+package main
+
+import (
+ "code.google.com/p/go.net/websocket"
+ "github.com/gorilla/mux"
+ "log"
+ "net/http"
+)
+
+func AcceptWebSock(rw http.ResponseWriter, req *http.Request) {
+ vars := mux.Vars(req)
+ room := vars["chatroom"]
+ websocket.Handler(func(ws *websocket.Conn) {
+ defer ws.Close()
+
+ var nick string
+ if websocket.Message.Receive(ws, &nick) != nil {
+ return
+ }
+
+ if err := Join(room, nick); err != nil {
+ // TODO: report error to client
+ return
+ }
+
+ usermsgs := make(chan string)
+ go func() {
+ var s string
+ for {
+ if websocket.Message.Receive(ws, &s) != nil {
+ return
+ }
+
+ if s != "" {
+ usermsgs <- s
+ }
+ }
+ }()
+ }).ServeHTTP(rw, req)
+}