forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.go
151 lines (131 loc) · 2.74 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
148
149
150
151
package socketio
import (
"net/http"
"github.com/googollee/go-engine.io"
)
// Socket is the socket object of socket.io.
type Socket interface {
// Id returns the session id of socket.
Id() string
// Rooms returns the rooms name joined now.
Rooms() []string
// Request returns the first http request when established connection.
Request() *http.Request
// On registers the function f to handle message.
On(message string, f interface{}) error
// Emit emits the message with given args.
Emit(message string, args ...interface{}) error
// Join joins the room.
Join(room string) error
// Leave leaves the room.
Leave(room string) error
// BroadcastTo broadcasts the message to the room with given args.
BroadcastTo(room, message string, args ...interface{}) error
}
type socket struct {
*socketHandler
conn engineio.Conn
namespace string
id int
}
func newSocket(conn engineio.Conn, base *baseHandler) *socket {
ret := &socket{
conn: conn,
}
ret.socketHandler = newSocketHandler(ret, base)
return ret
}
func (s *socket) Id() string {
return s.conn.Id()
}
func (s *socket) Request() *http.Request {
return s.conn.Request()
}
func (s *socket) Emit(message string, args ...interface{}) error {
if err := s.socketHandler.Emit(message, args...); err != nil {
return err
}
if message == "disconnect" {
s.conn.Close()
}
return nil
}
func (s *socket) send(args []interface{}) error {
packet := packet{
Type: _EVENT,
Id: -1,
NSP: s.namespace,
Data: args,
}
encoder := newEncoder(s.conn)
return encoder.Encode(packet)
}
func (s *socket) sendId(args []interface{}) (int, error) {
packet := packet{
Type: _EVENT,
Id: s.id,
NSP: s.namespace,
Data: args,
}
s.id++
if s.id < 0 {
s.id = 0
}
encoder := newEncoder(s.conn)
err := encoder.Encode(packet)
if err != nil {
return -1, nil
}
return packet.Id, nil
}
func (s *socket) loop() error {
defer func() {
s.LeaveAll()
p := packet{
Type: _DISCONNECT,
Id: -1,
}
s.socketHandler.onPacket(nil, &p)
}()
p := packet{
Type: _CONNECT,
Id: -1,
}
encoder := newEncoder(s.conn)
if err := encoder.Encode(p); err != nil {
return err
}
s.socketHandler.onPacket(nil, &p)
for {
decoder := newDecoder(s.conn)
var p packet
if err := decoder.Decode(&p); err != nil {
return err
}
ret, err := s.socketHandler.onPacket(decoder, &p)
if err != nil {
return err
}
switch p.Type {
case _CONNECT:
s.namespace = p.NSP
case _BINARY_EVENT:
fallthrough
case _EVENT:
if p.Id >= 0 {
p := packet{
Type: _ACK,
Id: p.Id,
NSP: s.namespace,
Data: ret,
}
encoder := newEncoder(s.conn)
if err := encoder.Encode(p); err != nil {
return err
}
}
case _DISCONNECT:
return nil
}
}
}