forked from ereyes01/socketio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
87 lines (73 loc) · 2.15 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package socketio
import (
"errors"
"io/ioutil"
"net/http"
"time"
"log"
"encoding/json"
)
// 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()
// 如果结果正确,则返回数据如下
// [ ?0{"sid":"E4Z4PNc4LZ30as06AAAa","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}]
// 不包含中括号,中括号是用来标明字符串前面的空格符号
// @TODO 不明确返回数据头部空格是如何产生的,后续反解析JSON数据的过程中需要去除此部分数据
type HandShake struct {
Sid string
Upgrades []string
PingInterval int
PingTimeout int
}
var shake HandShake
err = json.Unmarshal(body[5:], &shake)
if err != nil {
log.Println(err.Error())
return nil, errors.New("Handshaking failed!")
}
/*
sessionVars := strings.Split(string(body), ":")
if len(sessionVars) != 4 {
return nil, errors.New("Session variables is not 4")
}
*/
id := shake.Sid
heartbeatTimeoutSec := shake.PingInterval/1000
connectionTimeoutSec := shake.PingTimeout/1000
heartbeatTimeout := time.Duration(heartbeatTimeoutSec) * time.Second
connectionTimeout := time.Duration(connectionTimeoutSec) * time.Second
supportedProtocols := shake.Upgrades //strings.Split(string(body), ",")
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
}