-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathsession.go
66 lines (55 loc) · 1.55 KB
/
session.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
package socketio
import (
"errors"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)
// Session holds the configuration variables received from the socket.io
// server.
type Session struct {
ID string
HeartbeatTimeout time.Duration
ConnectionTimeout time.Duration
SupportedProtocols []string
}
// NewSession receives the configuraiton variables from the socket.io
// server.
func NewSession(url string) (*Session, error) {
urlParser, err := newURLParser(url)
if err != nil {
return nil, err
}
response, err := http.Get(urlParser.handshake())
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
response.Body.Close()
sessionVars := strings.Split(string(body), ":")
if len(sessionVars) != 4 {
return nil, errors.New("Session variables is not 4")
}
id := sessionVars[0]
heartbeatTimeoutSec, _ := strconv.Atoi(sessionVars[1])
connectionTimeoutSec, _ := strconv.Atoi(sessionVars[2])
heartbeatTimeout := time.Duration(heartbeatTimeoutSec) * time.Second
connectionTimeout := time.Duration(connectionTimeoutSec) * time.Second
supportedProtocols := strings.Split(string(sessionVars[3]), ",")
return &Session{id, heartbeatTimeout, connectionTimeout, supportedProtocols}, nil
}
// SupportProtocol checks if the given protocol is supported by the
// socket.io server.
func (session *Session) SupportProtocol(protocol string) bool {
for _, supportedProtocol := range session.SupportedProtocols {
if protocol == supportedProtocol {
return true
}
}
return false
}