blob: 8e6e1737b5eeaeaacd9254c66379c95f5375a433 [file] [log] [blame]
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !nacl
package websocket
import (
"net"
"net/http"
"net/url"
"time"
"github.com/gorilla/websocket"
"v.io/x/ref/runtime/internal/lib/tcputil"
)
func Dial(protocol, address string, timeout time.Duration) (net.Conn, error) {
var then time.Time
if timeout > 0 {
then = time.Now().Add(timeout)
}
tcp := mapWebSocketToTCP[protocol]
conn, err := net.DialTimeout(tcp, address, timeout)
if err != nil {
return nil, err
}
conn.SetReadDeadline(then)
if err := tcputil.EnableTCPKeepAlive(conn); err != nil {
return nil, err
}
u, err := url.Parse("ws://" + address)
if err != nil {
return nil, err
}
ws, _, err := websocket.NewClient(conn, u, http.Header{}, 4096, 4096)
if err != nil {
return nil, err
}
var zero time.Time
conn.SetDeadline(zero)
return WebsocketConn(ws), nil
}