forked from googollee/go-engine.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
168 lines (151 loc) · 3.09 KB
/
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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package engineio
import (
"errors"
"io"
"net"
"net/http"
"net/url"
"sync"
"time"
"github.com/googollee/go-engine.io/base"
"github.com/googollee/go-engine.io/transport"
)
// Dialer is dialer configure.
type Dialer struct {
Transports []transport.Transport
}
// Dial returns a connection which dials to url with requestHeader.
func (d *Dialer) Dial(urlStr string, requestHeader http.Header) (Conn, error) {
u, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
query := u.Query()
query.Set("EIO", "3")
u.RawQuery = query.Encode()
var conn base.Conn
for i := len(d.Transports) - 1; i >= 0; i-- {
if conn != nil {
conn.Close()
}
t := d.Transports[i]
conn, err = t.Dial(u, requestHeader)
if err != nil {
continue
}
var params base.ConnParameters
if p, ok := conn.(transport.Opener); ok {
params, err = p.Open()
if err != nil {
continue
}
} else {
var pt base.PacketType
var r io.ReadCloser
_, pt, r, err = conn.NextReader()
if err != nil {
continue
}
func() {
defer r.Close()
if pt != base.OPEN {
err = errors.New("invalid open")
return
}
params, err = base.ReadConnParameters(r)
if err != nil {
return
}
}()
}
if err != nil {
continue
}
ret := &client{
conn: conn,
params: params,
transport: t.Name(),
close: make(chan struct{}),
}
go ret.serve()
return ret, nil
}
return nil, err
}
type client struct {
conn base.Conn
params base.ConnParameters
transport string
context interface{}
close chan struct{}
closeOnce sync.Once
}
func (c *client) SetContext(v interface{}) {
c.context = v
}
func (c *client) Context() interface{} {
return c.context
}
func (c *client) ID() string {
return c.params.SID
}
func (c *client) Transport() string {
return c.transport
}
func (c *client) Close() error {
c.closeOnce.Do(func() {
close(c.close)
})
return c.conn.Close()
}
func (c *client) NextReader() (FrameType, io.ReadCloser, error) {
for {
ft, pt, r, err := c.conn.NextReader()
if err != nil {
return 0, nil, err
}
switch pt {
case base.PONG:
c.conn.SetReadDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout))
case base.CLOSE:
c.Close()
return 0, nil, io.EOF
case base.MESSAGE:
return FrameType(ft), r, nil
}
r.Close()
}
}
func (c *client) NextWriter(typ FrameType) (io.WriteCloser, error) {
return c.conn.NextWriter(base.FrameType(typ), base.MESSAGE)
}
func (c *client) URL() url.URL {
return c.conn.URL()
}
func (c *client) LocalAddr() net.Addr {
return c.conn.LocalAddr()
}
func (c *client) RemoteAddr() net.Addr {
return c.conn.RemoteAddr()
}
func (c *client) RemoteHeader() http.Header {
return c.conn.RemoteHeader()
}
func (c *client) serve() {
defer c.conn.Close()
for {
select {
case <-c.close:
return
case <-time.After(c.params.PingInterval):
}
w, err := c.conn.NextWriter(base.FrameString, base.PING)
if err != nil {
return
}
if err := w.Close(); err != nil {
return
}
c.conn.SetWriteDeadline(time.Now().Add(c.params.PingInterval + c.params.PingTimeout))
}
}