-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsocket.go
147 lines (131 loc) · 3.15 KB
/
socket.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package socketio
import (
"errors"
"io"
"time"
"github.com/doquangtan/socket.io/v4/engineio"
"github.com/doquangtan/socket.io/v4/socket_protocol"
"github.com/gofiber/websocket/v2"
gWebsocket "github.com/gorilla/websocket"
)
type Conn struct {
fasthttp *websocket.Conn
http *gWebsocket.Conn
}
func (c *Conn) NextWriter(messageType int) (io.WriteCloser, error) {
if c.http != nil {
return c.http.NextWriter(messageType)
}
if c.fasthttp != nil {
return c.fasthttp.NextWriter(messageType)
}
return nil, errors.New("not found http or fasthttp socket")
}
func (c *Conn) SetReadDeadline(t time.Time) error {
if c.http != nil {
return c.http.SetReadDeadline(t)
}
if c.fasthttp != nil {
return c.fasthttp.SetReadDeadline(t)
}
return errors.New("not found http or fasthttp socket")
}
func (c *Conn) Close() error {
if c.http != nil {
return c.http.Close()
}
if c.fasthttp != nil {
return c.fasthttp.Close()
}
return errors.New("not found http or fasthttp socket")
}
type Socket struct {
Id string
Nps string
Conn *Conn
rooms roomNames
listeners listeners
pingTime time.Duration
dispose []func()
Join func(room string)
Leave func(room string)
To func(room string) *Room
}
func (s *Socket) On(event string, fn eventCallback) {
s.listeners.set(event, fn)
}
func (s *Socket) Emit(event string, agrs ...interface{}) error {
c := s.Conn
if c == nil {
return errors.New("socket has disconnected")
}
agrs = append([]interface{}{event}, agrs...)
return s.writer(socket_protocol.EVENT, agrs)
}
func (s *Socket) ack(ackEvent string, agrs ...interface{}) error {
c := s.Conn
if c == nil {
return errors.New("socket has disconnected")
}
agrs = append([]interface{}{ackEvent}, agrs...)
return s.writer(socket_protocol.ACK, agrs)
}
func (s *Socket) Ping() error {
c := s.Conn
if c == nil {
return errors.New("socket has disconnected")
}
w, err := c.NextWriter(websocket.TextMessage)
if err != nil {
c.Close()
return err
}
engineio.WriteByte(w, engineio.PING, []byte{})
return w.Close()
}
func (s *Socket) Disconnect() error {
c := s.Conn
if c == nil {
return errors.New("socket has disconnected")
}
s.writer(socket_protocol.DISCONNECT)
return c.SetReadDeadline(time.Now())
}
func (s *Socket) Rooms() []string {
return s.rooms.all()
}
func (s *Socket) disconnect() {
s.Conn.Close()
s.Conn = nil
// s.rooms = []string{}
if len(s.dispose) > 0 {
for _, dispose := range s.dispose {
dispose()
}
}
}
func (s *Socket) engineWrite(t engineio.PacketType, arg ...interface{}) error {
w, err := s.Conn.NextWriter(websocket.TextMessage)
if err != nil {
return err
}
engineio.WriteTo(w, t, arg...)
return w.Close()
}
func (s *Socket) writer(t socket_protocol.PacketType, arg ...interface{}) error {
w, err := s.Conn.NextWriter(websocket.TextMessage)
if err != nil {
return err
}
nps := ""
if s.Nps != "/" {
nps = s.Nps + ","
}
if t == socket_protocol.ACK {
agrs := append([]interface{}{}, arg[0].([]interface{})[1:])
socket_protocol.WriteToWithAck(w, t, nps, arg[0].([]interface{})[0].(string), agrs...)
} else {
socket_protocol.WriteTo(w, t, nps, arg...)
}
return w.Close()
}