Skip to content

Commit 07343c2

Browse files
committed
Allow passing http:// and https:// URLS to Dial
1 parent 0d9471d commit 07343c2

File tree

9 files changed

+29
-29
lines changed

9 files changed

+29
-29
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# websocket
22

3-
[![godoc](https://godoc.org/nhooyr.io/websocket?status.svg)](https://godoc.org/nhooyr.io/websocket)
3+
[![godoc](https://godoc.org/nhooyr.io/websocket?status.svg)](https://pkg.go.dev/nhooyr.io/websocket)
44

55
websocket is a minimal and idiomatic WebSocket library for Go.
66

@@ -16,8 +16,8 @@ go get nhooyr.io/websocket
1616
- First class [context.Context](https://blog.golang.org/context) support
1717
- Fully passes the WebSocket [autobahn-testsuite](https://github.com/crossbario/autobahn-testsuite)
1818
- Thorough unit tests with [90% coverage](https://coveralls.io/github/nhooyr/websocket)
19-
- [Minimal dependencies](https://godoc.org/nhooyr.io/websocket?imports)
20-
- JSON and protobuf helpers in the [wsjson](https://godoc.org/nhooyr.io/websocket/wsjson) and [wspb](https://godoc.org/nhooyr.io/websocket/wspb) subpackages
19+
- [Minimal dependencies](https://pkg.go.dev/nhooyr.io/websocket?tab=imports)
20+
- JSON and protobuf helpers in the [wsjson](https://pkg.go.dev/nhooyr.io/websocket/wsjson?tab=doc) and [wspb](https://pkg.go.dev/nhooyr.io/websocket/wspb?tab=doc) subpackages
2121
- Zero alloc reads and writes
2222
- Concurrent writes
2323
- [Close handshake](https://godoc.org/nhooyr.io/websocket#Conn.Close)
@@ -98,7 +98,7 @@ Advantages of nhooyr.io/websocket:
9898
- [net.Conn](https://godoc.org/nhooyr.io/websocket#NetConn) wrapper
9999
- Zero alloc reads and writes ([gorilla/websocket#535](https://github.com/gorilla/websocket/issues/535))
100100
- Full [context.Context](https://blog.golang.org/context) support
101-
- Dial uses [net/http.Client](https://golang.org/pkg/net/http/#Client)
101+
- Dials use [net/http.Client](https://golang.org/pkg/net/http/#Client)
102102
- Will enable easy HTTP/2 support in the future
103103
- Gorilla writes directly to a net.Conn and so duplicates features of net/http.Client.
104104
- Concurrent writes

accept_js.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
type AcceptOptions struct {
1010
Subprotocols []string
1111
InsecureSkipVerify bool
12+
OriginPatterns []string
1213
CompressionMode CompressionMode
1314
CompressionThreshold int
1415
}

chat-example/chat_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Test_chatServer(t *testing.T) {
6161

6262
const nmessages = 128
6363
const maxMessageSize = 128
64-
const nclients = 10
64+
const nclients = 16
6565

6666
url, closeFn := setupTest(t)
6767
defer closeFn()
@@ -191,8 +191,7 @@ type client struct {
191191
}
192192

193193
func newClient(ctx context.Context, url string) (*client, error) {
194-
wsURL := strings.Replace(url, "http://", "ws://", 1)
195-
c, _, err := websocket.Dial(ctx, wsURL+"/subscribe", nil)
194+
c, _, err := websocket.Dial(ctx, url+"/subscribe", nil)
196195
if err != nil {
197196
return nil, err
198197
}

close_notjs.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ func (c *Conn) Close(code StatusCode, reason string) error {
3434
func (c *Conn) closeHandshake(code StatusCode, reason string) (err error) {
3535
defer errd.Wrap(&err, "failed to close WebSocket")
3636

37-
err = c.writeClose(code, reason)
38-
if err != nil && CloseStatus(err) == -1 && err != errAlreadyWroteClose {
39-
return err
37+
writeErr := c.writeClose(code, reason)
38+
closeHandshakeErr := c.waitCloseHandshake()
39+
40+
if writeErr != nil {
41+
return writeErr
4042
}
4143

42-
err = c.waitCloseHandshake()
43-
if CloseStatus(err) == -1 {
44-
return err
44+
if CloseStatus(closeHandshakeErr) == -1 {
45+
return closeHandshakeErr
4546
}
4647
return nil
4748
}
@@ -50,10 +51,10 @@ var errAlreadyWroteClose = errors.New("already wrote close")
5051

5152
func (c *Conn) writeClose(code StatusCode, reason string) error {
5253
c.closeMu.Lock()
53-
closing := c.wroteClose
54+
wroteClose := c.wroteClose
5455
c.wroteClose = true
5556
c.closeMu.Unlock()
56-
if closing {
57+
if wroteClose {
5758
return errAlreadyWroteClose
5859
}
5960

conn_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func TestWasm(t *testing.T) {
298298
defer cancel()
299299

300300
cmd := exec.CommandContext(ctx, "go", "test", "-exec=wasmbrowsertest", "./...")
301-
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm", fmt.Sprintf("WS_ECHO_SERVER_URL=%v", wstest.URL(s)))
301+
cmd.Env = append(os.Environ(), "GOOS=js", "GOARCH=wasm", fmt.Sprintf("WS_ECHO_SERVER_URL=%v", s.URL))
302302

303303
b, err := cmd.CombinedOutput()
304304
if err != nil {

dial.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ type DialOptions struct {
5858
// This function requires at least Go 1.12 as it uses a new feature
5959
// in net/http to perform WebSocket handshakes.
6060
// See docs on the HTTPClient option and https://github.com/golang/go/issues/26937#issuecomment-415855861
61+
//
62+
// URLs with http/https schemes will work and translated into ws/wss.
6163
func Dial(ctx context.Context, u string, opts *DialOptions) (*Conn, *http.Response, error) {
6264
return dial(ctx, u, opts, nil)
6365
}
@@ -145,6 +147,7 @@ func handshakeRequest(ctx context.Context, urls string, opts *DialOptions, copts
145147
u.Scheme = "http"
146148
case "wss":
147149
u.Scheme = "https"
150+
case "http", "https":
148151
default:
149152
return nil, fmt.Errorf("unexpected url scheme: %q", u.Scheme)
150153
}

example_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// +build !js
2-
31
package websocket_test
42

53
import (
@@ -187,3 +185,8 @@ func ExampleGrace() {
187185
s.Shutdown(ctx)
188186
g.Shutdown(ctx)
189187
}
188+
189+
// This example demonstrates full stack chat with an automated test.
190+
func Example_fullStackChat() {
191+
// https://github.com/nhooyr/websocket/tree/master/chat-example
192+
}

internal/test/wstest/url.go

Lines changed: 0 additions & 11 deletions
This file was deleted.

ws_js.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"reflect"
1111
"runtime"
12+
"strings"
1213
"sync"
1314
"syscall/js"
1415

@@ -257,6 +258,9 @@ func dial(ctx context.Context, url string, opts *DialOptions) (*Conn, *http.Resp
257258
opts = &DialOptions{}
258259
}
259260

261+
url = strings.Replace(url, "http://", "ws://", 1)
262+
url = strings.Replace(url, "https://", "wss://", 1)
263+
260264
ws, err := wsjs.New(url, opts.Subprotocols)
261265
if err != nil {
262266
return nil, nil, err

0 commit comments

Comments
 (0)