diff options
author | Kevin Chabowski <kevin@kch42.de> | 2014-03-25 13:31:58 +0100 |
---|---|---|
committer | Kevin Chabowski <kevin@kch42.de> | 2014-03-25 13:31:58 +0100 |
commit | d3ad46faf520291037d6803e8547cdafe8b0aff3 (patch) | |
tree | 2545ec36bcc0f4f7c6b72a49a80c42924277606b | |
parent | 74f01b3117ae49d2d42facc362d8c932f5769d9d (diff) | |
download | simplechat-d3ad46faf520291037d6803e8547cdafe8b0aff3.tar.gz simplechat-d3ad46faf520291037d6803e8547cdafe8b0aff3.tar.bz2 simplechat-d3ad46faf520291037d6803e8547cdafe8b0aff3.zip |
Joining a room should work now
-rw-r--r-- | rooms.go | 7 | ||||
-rw-r--r-- | websock.go | 33 |
2 files changed, 34 insertions, 6 deletions
@@ -40,6 +40,13 @@ func (r *Room) Broadcast() { } } +func (r *Room) ListBuddies() (buddies []string) { + for nick := range r.Buddies { + buddies = append(buddies, nick) + } + return +} + var rooms = make(map[string]Room) func Join(room, nick string) (*Buddy, *Room, error) { @@ -7,10 +7,18 @@ import ( "net/http" ) +type JoinResponse struct { + OK bool `json:"ok"` + Error string `json:"error,omitempty"` + Buddies []string `json:"buddies,omitempty"` +} + func AcceptWebSock(rw http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) - room := vars["chatroom"] + roomname := vars["chatroom"] websocket.Handler(func(ws *websocket.Conn) { + send := func(v interface{}) error { return websocket.JSON.Send(ws, v) } + defer ws.Close() var nick string @@ -18,12 +26,23 @@ func AcceptWebSock(rw http.ResponseWriter, req *http.Request) { return } - if err := Join(room, nick); err != nil { - // TODO: report error to client + buddy, room, err := Join(roomname, nick) + if err != nil { + send(JoinResponse{ + OK: false, + Error: err.Error(), + }) + return + } + defer buddy.Leave() + + if send(JoinResponse{ + OK: true, + Buddies: room.ListBuddies(), + }) != nil { return } - usermsgs := make(chan string) go func() { var s string for { @@ -31,9 +50,11 @@ func AcceptWebSock(rw http.ResponseWriter, req *http.Request) { return } - if s != "" { - usermsgs <- s + if s == "" { + continue } + + // TODO: Broadcast messsage } }() }).ServeHTTP(rw, req) |