Skip to content

Instantly share code, notes, and snippets.

@flimzy
Created February 16, 2017 10:56
Show Gist options
  • Save flimzy/253192a5f3f27075ebb770fe0262cf13 to your computer and use it in GitHub Desktop.
Save flimzy/253192a5f3f27075ebb770fe0262cf13 to your computer and use it in GitHub Desktop.
diff --git a/conn.go b/conn.go
index 1af4474..ba9f6fc 100644
--- a/conn.go
+++ b/conn.go
@@ -8,7 +8,6 @@ import (
"bytes"
"fmt"
"io"
- "net"
"net/url"
"time"
@@ -61,9 +60,29 @@ var errDeadlineReached = &deadlineErr{}
// TODO(nightexcessive): Add a Dial function that allows a deadline to be
// specified.
+// Addr is a copy of the net.Addr interface, included here to reduce import
+// dependencies
+type Addr interface {
+ Network() string
+ String() string
+}
+
+// Conn is a copy of the net.Conn interface, included here to reduce import
+// dependencies.
+type Conn interface {
+ Read(b []byte) (n int, err error)
+ Write(b []byte) (n int, err error)
+ Close() error
+ LocalAddr() Addr
+ RemoteAddr() Addr
+ SetDeadline(t time.Time) error
+ SetReadDeadline(t time.Time) error
+ SetWriteDeadline(t time.Time) error
+}
+
// Dial opens a new WebSocket connection. It will block until the connection is
// established or fails to connect.
-func Dial(url string) (net.Conn, error) {
+func Dial(url string) (Conn, error) {
ws, err := New(url)
if err != nil {
return nil, err
@@ -246,7 +265,7 @@ func (c *conn) Write(b []byte) (n int, err error) {
// LocalAddr would typically return the local network address, but due to
// limitations in the JavaScript API, it is unable to. Calling this method will
// cause a panic.
-func (c *conn) LocalAddr() net.Addr {
+func (c *conn) LocalAddr() Addr {
// BUG(nightexcessive): conn.LocalAddr() panics because the underlying
// JavaScript API has no way of figuring out the local address.
@@ -256,7 +275,7 @@ func (c *conn) LocalAddr() net.Addr {
// RemoteAddr returns the remote network address, based on
// websocket.WebSocket.URL.
-func (c *conn) RemoteAddr() net.Addr {
+func (c *conn) RemoteAddr() Addr {
wsURL, err := url.Parse(c.URL)
if err != nil {
// TODO(nightexcessive): Should we be panicking for this?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment