-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
53 lines (40 loc) · 825 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
51
52
53
package goeventbus
import (
"encoding/json"
"fmt"
"net"
)
type Client interface {
Connect()
}
type tcpClient struct {
address string
eventbus EventBus
}
type Request struct {
Channel string `json:"channel"`
Message Message `json:"message"`
}
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)
}
defer conn.Close()
for {
var msg Request
d := json.NewDecoder(conn)
err = d.Decode(&msg)
if err != nil {
fmt.Println("Error:", err)
return
}
channel := msg.Channel
message := msg.Message
s.eventbus.Channel(channel).Publisher().Publish(message)
}
}
func NewClient(address string, eventbus EventBus) Client {
return &tcpClient{address: address, eventbus: eventbus}
}