-
Notifications
You must be signed in to change notification settings - Fork 1
/
socketListener.go
78 lines (59 loc) · 1.99 KB
/
socketListener.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package nymsocketmanager
import (
"github.com/gorilla/websocket"
"github.com/rs/zerolog"
"golang.org/x/xerrors"
)
func NewSocketListener(socket *websocket.Conn, messageHandler func([]byte), toCallWhenClosed func(), parentLogger *zerolog.Logger) (*SocketListener, chan struct{}, error) {
if nil == socket {
err := xerrors.Errorf("websocket connection cannot be undefined")
return nil, nil, err
}
if nil == messageHandler {
err := xerrors.Errorf("processing function needs to be defined")
return nil, nil, err
}
// toCallWhenClosed function can be nil if nothing needs to be done
if nil == parentLogger {
err := xerrors.Errorf("logger needs to be defined")
return nil, nil, err
}
closedSocketChan := make(chan struct{}, 1)
localLogger := parentLogger.With().Str(ComponentField, "SocketListener").Logger()
return &SocketListener{
socket: socket,
closedSocketChan: closedSocketChan,
logger: &localLogger,
messageHandler: messageHandler,
toCallWhenClosed: toCallWhenClosed,
}, closedSocketChan, nil
}
type SocketListener struct {
socket *websocket.Conn
messageHandler func([]byte)
toCallWhenClosed func()
closedSocketChan chan struct{}
logger *zerolog.Logger
}
func (s *SocketListener) Listen() {
// If provided, execute some cleaning code from parent after closing
if s.toCallWhenClosed != nil {
s.logger.Trace().Msg("socketListener instructed to call function when shutting down")
defer s.toCallWhenClosed()
}
for nil != s.socket {
_, receivedMessage, e := s.socket.ReadMessage()
if nil != e {
s.logger.Debug().Msgf("Read: \"%v\"", e)
break
}
// Process msg: start a goroutine to handle the request
s.logger.Trace().Msgf("recv: \"%s\"", string(receivedMessage))
go s.messageHandler(receivedMessage)
}
// When the connection will be closed, will close the chan
// so that main process can know that ws has been closed.
close(s.closedSocketChan)
s.closedSocketChan = nil
s.logger.Debug().Msg("socketListener shut down")
}