@@ -50,7 +50,7 @@ type PinMap map[string]PinType
5050
5151// HandleMessage
5252// This is the function that handles the messages from the pubsub
53- func HandleMessage (message string ) {
53+ func HandleMessage (message string , ws * websocket. Conn ) {
5454 fmt .Println ("Message received:" , message )
5555 // JSON decode message
5656 req := Request {}
@@ -70,7 +70,7 @@ func HandleMessage(message string) {
7070 fmt .Println ("User: " + req .User )
7171 fmt .Println ("CID: " + req .CID )
7272 fmt .Println ("Pins: " + req .Pins )
73- go HandleProofOfAccess (req )
73+ go HandleProofOfAccess (req , ws )
7474 }
7575
7676 }
@@ -79,7 +79,7 @@ func HandleMessage(message string) {
7979 if nodeType == 2 {
8080 if req .Type == TypeRequestProof {
8181 fmt .Println ("Request for proof received" )
82- go HandleRequestProof (req )
82+ go HandleRequestProof (req , ws )
8383 }
8484 if req .Type == TypePingPongPong {
8585 validatorName := req .User
@@ -95,25 +95,25 @@ func HandleMessage(message string) {
9595 }
9696 if req .Type == TypePingPongPing {
9797 fmt .Println ("PingPongPing received" )
98- PingPongPong (req , req . Hash , req . User )
98+ PingPongPong (req , ws )
9999 nodeStatus := localdata .NodesStatus [req .User ]
100100 nodes := Nodes [req .User ]
101101 fmt .Println ("Node Status: " + nodeStatus )
102102 fmt .Println ("Nodes: " + fmt .Sprint (nodes ))
103103 if nodeType == 1 && ! nodes && nodeStatus != "Synced" {
104104 fmt .Println ("syncing: " + req .User )
105- go SyncNode (req )
105+ go SyncNode (req , ws )
106106 }
107107
108108 }
109109 if req .Type == "RequestCIDS" {
110110 fmt .Println ("RequestCIDS received" )
111- go SendCIDS (req .User )
111+ go SendCIDS (req .User , ws )
112112
113113 }
114114 if req .Type == "SendCIDS" {
115115 fmt .Println ("SendCIDS received" )
116- go SyncNode (req )
116+ go SyncNode (req , ws )
117117
118118 }
119119 if req .Type == "Syncing" {
@@ -148,14 +148,15 @@ func PubsubHandler(ctx context.Context) {
148148 log .Error ("Error reading from pubsub: " , err )
149149 continue
150150 }
151- HandleMessage (msg )
151+ var ws * websocket.Conn
152+ HandleMessage (msg , ws )
152153 }
153154 }
154155 } else {
155156 time .Sleep (1 * time .Second )
156157 }
157158}
158- func SendPing (hash string , user string ) {
159+ func SendPing (hash string , user string , ws * websocket. Conn ) {
159160 fmt .Println ("Sending Ping" )
160161 data := map [string ]string {
161162 "type" : TypePingPongPing ,
@@ -169,7 +170,6 @@ func SendPing(hash string, user string) {
169170 }
170171 localdata .PingTime [user ] = time .Now ()
171172 if localdata .WsPeers [user ] == user && localdata .NodeType == 1 {
172- ws := localdata .WsClients [user ]
173173 WsMutex .Lock ()
174174 ws .WriteMessage (websocket .TextMessage , jsonData )
175175 WsMutex .Unlock ()
@@ -181,11 +181,11 @@ func SendPing(hash string, user string) {
181181 pubsub .Publish (string (jsonData ), user )
182182 }
183183}
184- func PingPongPong (req Request , hash string , user string ) {
184+ func PingPongPong (req Request , ws * websocket. Conn ) {
185185 fmt .Println ("Sending PingPongPong" )
186186 data := map [string ]string {
187187 "type" : TypePingPongPong ,
188- "hash" : hash ,
188+ "hash" : req . Hash ,
189189 "user" : localdata .GetNodeName (),
190190 }
191191 jsonData , err := json .Marshal (data )
@@ -194,7 +194,6 @@ func PingPongPong(req Request, hash string, user string) {
194194 return
195195 }
196196 if localdata .WsPeers [req .User ] == req .User && localdata .NodeType == 1 {
197- ws := localdata .WsClients [req .User ]
198197 fmt .Println ("Sending PingPongPong to client" )
199198 localdata .Lock .Lock ()
200199 localdata .PeerLastActive [req .User ] = time .Now ()
@@ -209,6 +208,6 @@ func PingPongPong(req Request, hash string, user string) {
209208 localdata .WsValidators [req .User ].WriteMessage (websocket .TextMessage , jsonData )
210209 WsMutex .Unlock ()
211210 } else {
212- pubsub .Publish (string (jsonData ), user )
211+ pubsub .Publish (string (jsonData ), req . User )
213212 }
214213}
0 commit comments