@@ -2,8 +2,8 @@ package main
22
33import (
44 "context"
5+ "encoding/json"
56 "errors"
6- "io/ioutil"
77 "log"
88 "net/http"
99 "sync"
@@ -14,6 +14,26 @@ import (
1414 "nhooyr.io/websocket"
1515)
1616
17+ type clientJoin struct {
18+ ClientID string `json:"client_id"`
19+ ClientName string `json:"client_name"`
20+ }
21+
22+ type clientLeave struct {
23+ ClientID string `json:"client_id"`
24+ }
25+
26+ type clientRename struct {
27+ ClientID string `json:"client_id"`
28+ ClientName string `json:"client_name"`
29+ }
30+
31+ type messageBroadcast struct {
32+ ClientID string `json:"client_id"`
33+ Time time.Time `json:"time"`
34+ Message string `json:"msg"`
35+ }
36+
1737// chatServer enables broadcasting to a set of subscribers.
1838type chatServer struct {
1939 // subscriberMessageBuffer controls the max number
@@ -81,7 +101,7 @@ func (cs *chatServer) subscribeHandler(w http.ResponseWriter, r *http.Request) {
81101 return
82102 }
83103 if websocket .CloseStatus (err ) == websocket .StatusNormalClosure ||
84- websocket .CloseStatus (err ) == websocket .StatusGoingAway {
104+ websocket .CloseStatus (err ) == websocket .StatusGoingAway {
85105 return
86106 }
87107 if err != nil {
@@ -98,13 +118,18 @@ func (cs *chatServer) publishHandler(w http.ResponseWriter, r *http.Request) {
98118 return
99119 }
100120 body := http .MaxBytesReader (w , r .Body , 8192 )
101- msg , err := ioutil .ReadAll (body )
121+
122+ var msg message
123+ err := json .NewDecoder (body ).Decode (& msg )
102124 if err != nil {
103- http .Error (w , http .StatusText (http .StatusRequestEntityTooLarge ), http .StatusRequestEntityTooLarge )
125+ http .Error (w , http .StatusText (http .StatusBadRequest ), http .StatusBadRequest )
104126 return
105127 }
106128
107- cs .publish (msg )
129+ msg .Author =
130+
131+ // TODO improve
132+ cs .publish ([]byte (msg .Message ))
108133
109134 w .WriteHeader (http .StatusAccepted )
110135}
0 commit comments