forked from googollee/go-socket.io
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
132 lines (119 loc) · 2.78 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
package socketio
import (
"code.google.com/p/go.net/websocket"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
ProtocolVersion = 1
)
type Client struct {
session *Session
endpoint string
*EventEmitter
}
func Dial(origin, ressource string) (*Client, error) {
u, err := url.Parse(origin)
if err != nil {
return nil, err
}
endpoint := parseEndpoint(u)
u.Path = ""
var url_ string
if len(ressource) == 0 {
url_ = fmt.Sprintf("%s/socket.io/%d/", u.String(), ProtocolVersion)
} else {
url_ = fmt.Sprintf("%s/%s/%d/", u.String(), ressource, ProtocolVersion)
}
r, err := http.Get(url_)
if err != nil {
return nil, err
}
defer r.Body.Close()
if r.StatusCode != 200 {
return nil, errors.New("invalid status: " + r.Status)
}
body, err := ioutil.ReadAll(r.Body)
//fmt.Println(string(body))
if err != nil {
return nil, err
}
parts := strings.SplitN(string(body), ":", 4)
if len(parts) != 4 {
return nil, errors.New("invalid handshake: " + string(body))
}
if !strings.Contains(parts[3], "websocket") {
return nil, errors.New("server does not support websockets")
}
sessionId := parts[0]
wsurl := "ws" + url_[4:]
wsurl = fmt.Sprintf("%swebsocket/%s", wsurl, sessionId)
ws, err := websocket.Dial(wsurl, "", url_)
if err != nil {
return nil, err
}
timeout, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return nil, err
}
ee := NewEventEmitter()
emitters := make(map[string]*EventEmitter)
emitters[endpoint] = ee
if endpoint != "" {
emitters[""] = NewEventEmitter()
}
session := NewSession(emitters, sessionId, int(timeout), false)
transport := newWebSocket(session)
transport.conn = ws
session.transport = transport
if endpoint != "" {
session.transport.Send(encodePacket(endpoint, new(connectPacket)))
}
return &Client{
session: session,
endpoint: endpoint,
EventEmitter: ee,
}, nil
}
func parseEndpoint(u *url.URL) string {
path := u.Path
if l := len(path); l > 0 && path[len(path)-1] == '/' {
path = path[:l-1]
}
lastPath := strings.LastIndex(path, "/")
endpoint := ""
if lastPath >= 0 {
path := path[lastPath:]
if len(path) > 0 {
endpoint = path
}
}
return endpoint
}
func (c *Client) Run() {
c.session.loop()
}
func (c *Client) Quit() error {
return nil
}
func (c *Client) Of(name string) (nameSpace *NameSpace) {
ee := c.session.emitters[name]
ns := c.session.Of(name)
if ee == nil {
c.session.transport.Send(encodePacket(name, new(connectPacket)))
ns.connected = true
}
return ns
}
func (c *Client) Call(name string, timeout time.Duration, reply []interface{}, args ...interface{}) error {
return c.Of(c.endpoint).Call(name, timeout, reply, args...)
}
func (c *Client) Emit(name string, args ...interface{}) error {
return c.Of(c.endpoint).Emit(name, args...)
}