forked from graarh/golang-socketio
/
polling_client.go
189 lines (153 loc) · 5.57 KB
/
polling_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package transport
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/mtfelian/golang-socketio/logging"
"github.com/mtfelian/golang-socketio/protocol"
)
var (
errResponseIsNotOK = errors.New("response body is not OK")
errAnswerNotOpenSequence = errors.New("not opensequence answer")
errAnswerNotOpenMessage = errors.New("not openmessage answer")
)
// PollingClientConnection represents XHR polling client connection
type PollingClientConnection struct {
transport *PollingClientTransport
client *http.Client
url string
sid string
}
// GetMessage performs a GET request to wait for the following message
func (polling *PollingClientConnection) GetMessage() (string, error) {
logging.Log().Debug("PollingConnection.GetMessage() fired")
resp, err := polling.client.Get(polling.url)
if err != nil {
logging.Log().Debug("PollingConnection.GetMessage() error polling.client.Get():", err)
return "", err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
logging.Log().Debug("PollingConnection.GetMessage() error ioutil.ReadAll():", err)
return "", err
}
bodyString := string(bodyBytes)
logging.Log().Debug("PollingConnection.GetMessage() bodyString:", bodyString)
index := strings.Index(bodyString, ":")
body := bodyString[index+1:]
return body, nil
}
// WriteMessage performs a POST request to send a message to server
func (polling *PollingClientConnection) WriteMessage(m string) error {
mWrite := withLength(m)
logging.Log().Debug("PollingConnection.WriteMessage() fired, msgToWrite:", mWrite)
mJSON := []byte(mWrite)
resp, err := polling.client.Post(polling.url, "application/json", bytes.NewBuffer(mJSON))
if err != nil {
logging.Log().Debug("PollingConnection.WriteMessage() error polling.client.Post():", err)
return err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
logging.Log().Debug("PollingConnection.WriteMessage() error ioutil.ReadAll():", err)
return err
}
resp.Body.Close()
bodyString := string(bodyBytes)
if bodyString != "ok" {
return errResponseIsNotOK
}
return nil
}
// Close the client connection gracefully
func (polling *PollingClientConnection) Close() error {
return polling.WriteMessage(protocol.MessageClose)
}
// PingParams returns PingInterval and PingTimeout params
func (polling *PollingClientConnection) PingParams() (time.Duration, time.Duration) {
return polling.transport.PingInterval, polling.transport.PingTimeout
}
// PollingClientTransport represents polling client transport parameters
type PollingClientTransport struct {
PingInterval time.Duration
PingTimeout time.Duration
ReceiveTimeout time.Duration
SendTimeout time.Duration
Headers http.Header
sessions sessions
}
// HandleConnection for the polling client is a placeholder
func (t *PollingClientTransport) HandleConnection(w http.ResponseWriter, r *http.Request) (Connection, error) {
return nil, nil
}
// Serve for the polling client is a placeholder
func (t *PollingClientTransport) Serve(w http.ResponseWriter, r *http.Request) {}
// SetSid for the polling client is a placeholder
func (t *PollingClientTransport) SetSid(sid string, conn Connection) {}
// openSequence represents a connection open sequence parameters
type openSequence struct {
Sid string `json:"sid"`
Upgrades []string `json:"upgrades"`
PingInterval time.Duration `json:"pingInterval"`
PingTimeout time.Duration `json:"pingTimeout"`
}
// Connect to server, perform 3 HTTP requests in connecting sequence
func (t *PollingClientTransport) Connect(url string) (Connection, error) {
polling := &PollingClientConnection{transport: t, client: &http.Client{}, url: url}
resp, err := polling.client.Get(polling.url)
if err != nil {
logging.Log().Debug("PollingConnection.Connect() error polling.client.Get() 1:", err)
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
logging.Log().Debug("PollingConnection.Connect() error ioutil.ReadAll() 1:", err)
return nil, err
}
resp.Body.Close()
bodyString := string(bodyBytes)
logging.Log().Debug("PollingConnection.Connect() bodyString 1:", bodyString)
body := bodyString[strings.Index(bodyString, ":")+1:]
if string(body[0]) != protocol.MessageOpen {
return nil, errAnswerNotOpenSequence
}
bodyBytes2 := []byte(body[1:])
var openSequence openSequence
if err := json.Unmarshal(bodyBytes2, &openSequence); err != nil {
logging.Log().Debug("PollingConnection.Connect() error json.Unmarshal() 1:", err)
return nil, err
}
polling.url += "&sid=" + openSequence.Sid
logging.Log().Debug("PollingConnection.Connect() polling.url 1:", polling.url)
resp, err = polling.client.Get(polling.url)
if err != nil {
logging.Log().Debug("PollingConnection.Connect() error plc.client.Get() 2:", err)
return nil, err
}
bodyBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
logging.Log().Debug("PollingConnection.Connect() error ioutil.ReadAll() 2:", err)
return nil, err
}
resp.Body.Close()
bodyString = string(bodyBytes)
logging.Log().Debug("PollingConnection.Connect() bodyString 2:", bodyString)
body = bodyString[strings.Index(bodyString, ":")+1:]
if body != protocol.MessageEmpty {
return nil, errAnswerNotOpenMessage
}
return polling, nil
}
// DefaultPollingClientTransport returns client polling transport with default params
func DefaultPollingClientTransport() *PollingClientTransport {
return &PollingClientTransport{
PingInterval: PlDefaultPingInterval,
PingTimeout: PlDefaultPingTimeout,
ReceiveTimeout: PlDefaultReceiveTimeout,
SendTimeout: PlDefaultSendTimeout,
}
}