-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebsocket_handler.go
64 lines (49 loc) · 1.44 KB
/
websocket_handler.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
/*
AUTHOR
Grant Street Group <developers@grantstreet.com>
COPYRIGHT AND LICENSE
This software is Copyright (c) 2019 by Grant Street Group.
This is free software, licensed under:
MIT License
*/
package exasol
import (
"crypto/tls"
"net/url"
"time"
"github.com/gorilla/websocket"
)
// This is the default websocket handler that uses gorilla/websocket implementation
// and conforms to the WSHandler interface
type defWSHandler struct {
ws *websocket.Conn
}
func newDefaultWSHandler() *defWSHandler {
return &defWSHandler{}
}
var defaultDialer = *websocket.DefaultDialer
func init() {
defaultDialer.Proxy = nil // TODO use proxy env
defaultDialer.EnableCompression = false
}
func (wsh *defWSHandler) Connect(url url.URL, tlsCfg *tls.Config, timeout time.Duration) error {
if timeout != time.Duration(0) {
defaultDialer.HandshakeTimeout = timeout
}
defaultDialer.TLSClientConfig = tlsCfg
// According to documentation:
// > It is safe to call Dialer's methods concurrently.
ws, _, err := defaultDialer.Dial(url.String(), nil)
if err != nil {
return err
}
wsh.ws = ws
return nil
}
func (wsh *defWSHandler) WriteJSON(req interface{}) error { return wsh.ws.WriteJSON(req) }
func (wsh *defWSHandler) ReadJSON(resp interface{}) error { return wsh.ws.ReadJSON(resp) }
func (wsh *defWSHandler) EnableCompression(e bool) { wsh.ws.EnableWriteCompression(e) }
func (wsh *defWSHandler) Close() {
wsh.ws.Close()
wsh.ws = nil
}