@@ -16,7 +16,7 @@ Echo server:
1616
1717 r.HandleFunc("/ws", wsServer.Handler)
1818
19- wsServer.On("echo", func(c *websocket.Conn, msg *websocket.Message) {
19+ wsServer.On("echo", func(ctx context.Context, c *websocket.Conn, msg *websocket.Message) {
2020 c.Emit("echo", msg.Data)
2121 })
2222
@@ -38,7 +38,7 @@ Websocket with group:
3838
3939 ch := wsServer.NewChannel("test")
4040
41- wsServer.OnConnect(func(c *websocket.Conn) {
41+ wsServer.OnConnect(func(ctx context.Context, c *websocket.Conn) {
4242 ch.Add(c)
4343 ch.Emit("connection", []byte("new connection come"))
4444 })
@@ -75,9 +75,9 @@ type Server struct {
7575
7676 delChan []chan * Conn
7777
78- onConnect func (c * Conn )
79- onDisconnect func (c * Conn )
80- onMessage func (c * Conn , h ws.Header , b []byte )
78+ onConnect func (ctx context. Context , c * Conn )
79+ onDisconnect func (ctx context. Context , c * Conn )
80+ onMessage func (ctx context. Context , c * Conn , h ws.Header , b []byte )
8181
8282 done bool
8383 mu sync.RWMutex
@@ -94,7 +94,7 @@ type Message struct {
9494// HandlerFunc is a type for handle function all function which has callback have this struct
9595// as first element returns pointer to connection
9696// its give opportunity to close connection or emit message to exactly this connection.
97- type HandlerFunc func (c * Conn , msg * Message )
97+ type HandlerFunc func (ctx context. Context , c * Conn , msg * Message )
9898
9999// New websocket server handler with the provided options.
100100func New () * Server {
@@ -104,7 +104,7 @@ func New() *Server {
104104 broadcast : make (chan Message ),
105105 callbacks : make (map [string ]HandlerFunc ),
106106 }
107- srv .onMessage = func (c * Conn , h ws.Header , b []byte ) {
107+ srv .onMessage = func (ctx context. Context , c * Conn , h ws.Header , b []byte ) {
108108 _ = c .Write (h , b )
109109 }
110110 return srv
@@ -168,6 +168,7 @@ func (s *Server) Shutdown() error {
168168
169169// Handler get upgrade connection to RFC 6455 and starting listener for it.
170170func (s * Server ) Handler (w http.ResponseWriter , r * http.Request ) {
171+ ctx := r .Context ()
171172 var params url.Values = nil
172173
173174 conn , _ , _ , err := ws .UpgradeHTTP (r , w )
@@ -194,7 +195,7 @@ func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
194195 done : make (chan bool , 1 ),
195196 }
196197 connection .startPing ()
197- s .addConn (connection )
198+ s .addConn (ctx , connection )
198199
199200 textPending := false
200201
@@ -206,7 +207,7 @@ func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
206207 header , _ := ws .ReadHeader (conn )
207208 if err = ws .CheckHeader (header , state ); err != nil {
208209 log .Printf ("drop ws connection: %v" , err )
209- s .dropConn (connection )
210+ s .dropConn (ctx , connection )
210211 break
211212 }
212213
@@ -263,12 +264,12 @@ func (s *Server) Handler(w http.ResponseWriter, r *http.Request) {
263264 if err != nil {
264265 log .Printf ("drop ws connection: OpClose (%v)" , err )
265266 }
266- s .dropConn (connection )
267+ s .dropConn (ctx , connection )
267268 break
268269 }
269270
270271 header .Masked = false
271- if err = s .processMessage (connection , header , payload ); err != nil {
272+ if err = s .processMessage (ctx , connection , header , payload ); err != nil {
272273 log .Print (err )
273274 }
274275 }
@@ -316,21 +317,21 @@ func (s *Server) Channels() []string {
316317}
317318
318319// OnConnect function which will be called when new connections come.
319- func (s * Server ) OnConnect (f func (c * Conn )) {
320+ func (s * Server ) OnConnect (f func (ctx context. Context , c * Conn )) {
320321 s .mu .Lock ()
321322 s .onConnect = f
322323 s .mu .Unlock ()
323324}
324325
325326// OnDisconnect function which will be called when new connections come.
326- func (s * Server ) OnDisconnect (f func (c * Conn )) {
327+ func (s * Server ) OnDisconnect (f func (ctx context. Context , c * Conn )) {
327328 s .mu .Lock ()
328329 s .onDisconnect = f
329330 s .mu .Unlock ()
330331}
331332
332333// OnMessage handling byte message. This function works as echo by default
333- func (s * Server ) OnMessage (f func (c * Conn , h ws.Header , b []byte )) {
334+ func (s * Server ) OnMessage (f func (ctx context. Context , c * Conn , h ws.Header , b []byte )) {
334335 s .mu .Lock ()
335336 s .onMessage = f
336337 s .mu .Unlock ()
@@ -372,14 +373,14 @@ func (s *Server) IsClosed() bool {
372373 return s .done
373374}
374375
375- func (s * Server ) processMessage (c * Conn , h ws.Header , b []byte ) error {
376+ func (s * Server ) processMessage (ctx context. Context , c * Conn , h ws.Header , b []byte ) error {
376377 if len (b ) == 0 {
377- s .onMessage (c , h , b )
378+ s .onMessage (ctx , c , h , b )
378379 return nil
379380 }
380381
381382 if h .OpCode != ws .OpBinary && h .OpCode != ws .OpText {
382- s .onMessage (c , h , b )
383+ s .onMessage (ctx , c , h , b )
383384 return nil
384385 }
385386
@@ -393,30 +394,30 @@ func (s *Server) processMessage(c *Conn, h ws.Header, b []byte) error {
393394 if err != nil {
394395 return err
395396 }
396- s .callbacks [msg.Name ](c , & Message {
397+ s .callbacks [msg.Name ](ctx , c , & Message {
397398 Name : msg .Name ,
398399 Data : buf ,
399400 })
400401 return nil
401402 }
402- s .onMessage (c , h , b )
403+ s .onMessage (ctx , c , h , b )
403404
404405 return nil
405406}
406407
407- func (s * Server ) addConn (conn * Conn ) {
408- if ! reflect . ValueOf ( s .onConnect ). IsNil () {
409- go s .onConnect (conn )
408+ func (s * Server ) addConn (ctx context. Context , conn * Conn ) {
409+ if s .onConnect != nil {
410+ go s .onConnect (ctx , conn )
410411 }
411412
412413 s .mu .Lock ()
413414 s .connections [conn ] = true
414415 s .mu .Unlock ()
415416}
416417
417- func (s * Server ) dropConn (conn * Conn ) {
418+ func (s * Server ) dropConn (ctx context. Context , conn * Conn ) {
418419 if ! reflect .ValueOf (s .onDisconnect ).IsNil () {
419- go s .onDisconnect (conn )
420+ go s .onDisconnect (ctx , conn )
420421 }
421422
422423 go func () {
0 commit comments