-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.go
62 lines (50 loc) · 1.24 KB
/
sockets.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
package main
import (
"context"
"fmt"
"net"
"os"
)
type ISocket interface {
Subscribe(context.Context,string) (conn net.Conn, err error)
Publish(context.Context, string) (err error)
Disconnect(context.Context) (confirm string, err error)
Retry(context.Context)
}
type Socket struct {
conn net.Conn
listener net.Listener
}
func NewSocketConn(prot, host, port string) (ISocket, error) {
conn, err := net.Dial(prot, host + ":" + port)
if err != nil {
return nil, fmt.Errorf(err.Error())
}
return &Socket{
conn: conn,
}, nil
}
func (s *Socket) Subscribe(ctx context.Context, target string) (conn net.Conn, err error) {
listener, err := net.Listen("tcp", target)
if err != nil {
return nil, fmt.Errorf(err.Error())
}
newlistener, err := listener.Accept()
return newlistener, nil
}
func (s *Socket) Publish(ctx context.Context, msg string) (err error) {
_, err = os.Stdout.Write([]byte(msg + "\n"))
if err != nil {
fmt.Println(fmt.Errorf("error: Publish() - %s", err))
}
return
}
func (s *Socket) Disconnect(ctx context.Context) (confirm string, err error){
if err := s.listener.Close(); err != nil {
return "", err
}
return "Successfully closed socket", nil
}
func (s *Socket) Retry(ctx context.Context) {
return
}