@@ -3,14 +3,16 @@ package ws
3
3
import (
4
4
"encoding/json"
5
5
"io"
6
- "log"
7
6
"net/http"
8
7
"sync"
9
8
10
- "github.com/gorilla/websocket"
11
9
"github.com/iota-uz/iota-sdk/modules/core/domain/aggregates/user"
12
10
"github.com/iota-uz/iota-sdk/modules/core/domain/entities/session"
11
+ "github.com/iota-uz/iota-sdk/pkg/configuration"
13
12
"github.com/iota-uz/iota-sdk/pkg/constants"
13
+
14
+ "github.com/gorilla/websocket"
15
+ "github.com/sirupsen/logrus"
14
16
)
15
17
16
18
type Connection struct {
@@ -95,11 +97,13 @@ type Hub struct {
95
97
userConnections map [uint ]Set [* Connection ]
96
98
channelConnections map [string ]Set [* Connection ]
97
99
mu sync.RWMutex
100
+ log * logrus.Logger
98
101
}
99
102
100
103
var _ Huber = (* Hub )(nil )
101
104
102
105
func NewHub () * Hub {
106
+ conf := configuration .Use ()
103
107
return & Hub {
104
108
upgrader : websocket.Upgrader {
105
109
ReadBufferSize : 1024 ,
@@ -111,13 +115,14 @@ func NewHub() *Hub {
111
115
connections : make (Set [* Connection ]),
112
116
userConnections : make (map [uint ]Set [* Connection ]),
113
117
channelConnections : make (map [string ]Set [* Connection ]),
118
+ log : conf .Logger (),
114
119
}
115
120
}
116
121
117
122
func (h * Hub ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
118
123
conn , err := h .upgrader .Upgrade (w , r , nil )
119
124
if err != nil {
120
- log .Printf ("Error upgrading connection: %v" , err )
125
+ h . log .Printf ("Error upgrading connection: %v" , err )
121
126
return
122
127
}
123
128
@@ -156,13 +161,13 @@ func (h *Hub) readPump(conn *Connection) {
156
161
_ , message , err := conn .conn .ReadMessage ()
157
162
if err != nil {
158
163
if websocket .IsUnexpectedCloseError (err , websocket .CloseGoingAway , websocket .CloseAbnormalClosure ) {
159
- log .Printf ("WebSocket error: %v" , err )
164
+ h . log .Printf ("WebSocket error: %v" , err )
160
165
}
161
166
break
162
167
}
163
168
164
169
if err := h .handleMessage (conn , message ); err != nil {
165
- log .Printf ("Error handling message: %v" , err )
170
+ h . log .Printf ("Error handling message: %v" , err )
166
171
break
167
172
}
168
173
}
@@ -215,7 +220,7 @@ func (h *Hub) BroadcastToAll(message []byte) {
215
220
216
221
for conn := range h .connections {
217
222
if err := conn .SendMessage (message ); err != nil {
218
- log .Printf ("Error broadcasting message: %v" , err )
223
+ h . log .Printf ("Error broadcasting message: %v" , err )
219
224
}
220
225
}
221
226
}
@@ -227,7 +232,7 @@ func (h *Hub) BroadcastToUser(userID uint, message []byte) {
227
232
if userConns , ok := h .userConnections [userID ]; ok {
228
233
for conn := range userConns {
229
234
if err := conn .SendMessage (message ); err != nil {
230
- log .Printf ("Error broadcasting to user %d: %v" , userID , err )
235
+ h . log .Printf ("Error broadcasting to user %d: %v" , userID , err )
231
236
}
232
237
}
233
238
}
@@ -240,7 +245,7 @@ func (h *Hub) BroadcastToChannel(channel string, message []byte) {
240
245
if channelConns , ok := h .channelConnections [channel ]; ok {
241
246
for conn := range channelConns {
242
247
if err := conn .SendMessage (message ); err != nil {
243
- log .Printf ("Error broadcasting to channel %s: %v" , channel , err )
248
+ h . log .Printf ("Error broadcasting to channel %s: %v" , channel , err )
244
249
}
245
250
}
246
251
}
0 commit comments