-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
50 lines (37 loc) · 884 Bytes
/
client.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
package goeventbus
import (
"encoding/json"
"log/slog"
"net"
)
type Client interface {
Connect()
}
type tcpClient struct {
address string
eventbus EventBus
}
func (s *tcpClient) Connect() {
var conn net.Conn
var err error
for conn, err = net.Dial("tcp", s.address); err != nil; {
conn, err = net.Dial("tcp", s.address)
}
slog.Info("Connected to server", slog.String("address", conn.LocalAddr().String()))
defer conn.Close()
for {
var msg request
d := json.NewDecoder(conn)
err = d.Decode(&msg)
if err != nil {
slog.Error(err.Error())
return
}
channel := msg.Channel
message := NewMessageBuilder().SetPayload(msg.Payload).SetHeaders(msg.Headers).Build()
s.eventbus.Channel(channel).Publisher().Publish(message)
}
}
func newClient(address string, eventbus EventBus) Client {
return &tcpClient{address: address, eventbus: eventbus}
}