forked from adriancooney/await
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebsocket.go
More file actions
43 lines (36 loc) · 909 Bytes
/
websocket.go
File metadata and controls
43 lines (36 loc) · 909 Bytes
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
package main
import (
"context"
"net"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
)
type websocketResource struct {
url.URL
}
func (r *websocketResource) Await(ctx context.Context) error {
netDialer := &net.Dialer{}
dial := func(network, address string) (net.Conn, error) {
return netDialer.DialContext(ctx, network, address)
}
var timeout time.Duration
if deadline, ok := ctx.Deadline(); ok {
timeout = deadline.Sub(time.Now())
}
wsDialer := &websocket.Dialer{
NetDial: dial,
HandshakeTimeout: timeout,
Proxy: http.ProxyFromEnvironment,
}
// IDEA(uwe): Use fragment to specify origin
// IDEA(uwe): Use fragment to specify subprotocol
// IDEA(uwe): Use fragment to specify cookies
conn, _, err := wsDialer.Dial(r.URL.String(), nil)
if err != nil {
return &unavailabilityError{err}
}
defer func() { _ = conn.Close() }()
return nil
}