@@ -11,6 +11,7 @@ import (
1111 "github.com/gorilla/websocket"
1212 "github.com/labstack/echo"
1313 "github.com/labstack/echo/middleware"
14+ "github.com/rs/xid"
1415)
1516
1617var (
@@ -42,6 +43,9 @@ func WSHandler(c echo.Context) error {
4243 }
4344 })()
4445 key := c .QueryParam ("key" )
46+ if key == "" {
47+ key = "Anonymous#" + xid .New ().String ()
48+ }
4549 allowed := TriggerWebhook (Event {
4650 Action : "connect" ,
4751 Key : key ,
@@ -63,7 +67,9 @@ func WSHandler(c echo.Context) error {
6367 }
6468 closeCh := make (chan bool )
6569 closed := false
70+ debug ("New Valid Connection(" + key + ")" )
6671 conn .SetCloseHandler (func (_ int , _ string ) error {
72+ debug ("Connection(" + key + ") has been closed (by itself)" )
6773 closeCh <- true
6874 return nil
6975 })
@@ -76,13 +82,16 @@ func WSHandler(c echo.Context) error {
7682 TriggerWebhook (Event {Action : "disconnect" , Key : key })
7783 case data := <- subscriber .GetMessages ():
7884 msg := (data .GetPayload ()).(Message )
85+ debug ("Incomming message to(" + key + ") ..." )
7986 if ! msg .IsUserAllowed (key ) {
87+ debug ("The client(" + key + ") isn't allowed to see the message" )
8088 continue
8189 }
8290 msg .Topic = data .GetTopic ()
8391 msg .Time = data .GetCreatedAt ()
8492 msg .To = nil
85- if conn .WriteJSON (msg ) != nil {
93+ if err := conn .WriteJSON (msg ); err != nil {
94+ debug ("A message cannot be published to (" + key + ") because of the following error (" + err .Error () + ")" )
8695 closeCh <- true
8796 }
8897 }
@@ -94,9 +103,11 @@ func goRoutineAction(conn *websocket.Conn, closeCh chan bool, subscriber *pubsub
94103 go (func () {
95104 var action Event
96105 for {
97- if conn .ReadJSON (& action ) != nil {
106+ if err := conn .ReadJSON (& action ); err != nil {
107+ debug ("Cannot read from the connection of(" + key + "), may connection has been closed, closing ..." )
98108 break
99109 }
110+ debug ("An action (" + action .Action + ") from the client(" + key + ")" )
100111 if action .Action == "subscribe" || action .Action == "unsubscribe" {
101112 if ! TriggerWebhook (Event {Action : action .Action , Key : key , Value : action .Value }) {
102113 conn .WriteJSON (map [string ]string { // nolint: errcheck
@@ -125,6 +136,7 @@ func PublishHandler(c echo.Context) error {
125136 })
126137 }
127138 Broker .Broadcast (msg , msg .Topic )
139+ debug ("publishing a message ..." )
128140 return c .JSON (200 , map [string ]interface {}{
129141 "success" : true ,
130142 "data" : msg ,
0 commit comments