From b81173722527f223b48a9dfbaee248a35977befa Mon Sep 17 00:00:00 2001 From: Gennadii Kovalev Date: Fri, 20 May 2016 14:26:18 +0200 Subject: [PATCH] client. allow connection to any url. url generator function. --- README.md | 5 ++++- client.go | 24 ++++++++++++++++++++++-- examples/client.go | 4 +++- transport/transport.go | 2 +- transport/websocket.go | 6 ++---- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9207a54..a4063b7 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,10 @@ var socket = io('ws://yourdomain.com', {transports: ['websocket']}); ```go //connect to server, you can use your own transport settings - c, err := gosocketio.Dial("localhost:80", transport.GetDefaultWebsocketTransport()) + c, err := gosocketio.Dial( + gosocketio.GetUrl("localhost", 80, false), + transport.GetDefaultWebsocketTransport(), + ) //do something, handlers and functions are same as server ones diff --git a/client.go b/client.go index 0968347..3d76233 100644 --- a/client.go +++ b/client.go @@ -2,10 +2,12 @@ package gosocketio import ( "github.com/graarh/golang-socketio/transport" + "strconv" ) const ( webSocketProtocol = "ws://" + webSocketSecureProtocol = "wss://" socketioUrl = "/socket.io/?EIO=3&transport=websocket" ) @@ -17,16 +19,34 @@ type Client struct { Channel } +/** +Get ws/wss url by host and port + */ +func GetUrl(host string, port int, secure bool) string { + var prefix string + if secure { + prefix = webSocketSecureProtocol + } else { + prefix = webSocketProtocol + } + return prefix + host + ":" + strconv.Itoa(port) + socketioUrl +} + /** connect to host and initialise socket.io protocol + +The correct ws protocol url example: +ws://myserver.com/socket.io/?EIO=3&transport=websocket + +You can use GetUrlByHost for generating correct url */ -func Dial(host string, tr transport.Transport) (*Client, error) { +func Dial(url string, tr transport.Transport) (*Client, error) { c := &Client{} c.initChannel() c.initMethods() var err error - c.conn, err = tr.Connect(host) + c.conn, err = tr.Connect(url) if err != nil { return nil, err } diff --git a/examples/client.go b/examples/client.go index 20203ab..e92c374 100644 --- a/examples/client.go +++ b/examples/client.go @@ -31,7 +31,9 @@ func sendJoin(c *gosocketio.Client) { func main() { runtime.GOMAXPROCS(runtime.NumCPU()) - c, err := gosocketio.Dial("localhost:3811", transport.GetDefaultWebsocketTransport()) + c, err := gosocketio.Dial( + gosocketio.GetUrl("localhost", 3811, false), + transport.GetDefaultWebsocketTransport()) if err != nil { log.Fatal(err) } diff --git a/transport/transport.go b/transport/transport.go index fe7a4a3..d64dbf0 100644 --- a/transport/transport.go +++ b/transport/transport.go @@ -37,7 +37,7 @@ type Transport interface { /** Get client connection */ - Connect(host string) (conn Connection, err error) + Connect(url string) (conn Connection, err error) /** Handle one server connection diff --git a/transport/websocket.go b/transport/websocket.go index 9480777..42772f7 100644 --- a/transport/websocket.go +++ b/transport/websocket.go @@ -9,8 +9,6 @@ import ( ) const ( - webSocketProtocol = "ws://" - socketioUrl = "/socket.io/?EIO=3&transport=websocket" upgradeFailed = "Upgrade failed: " WsDefaultPingInterval = 30 * time.Second @@ -92,9 +90,9 @@ type WebsocketTransport struct { BufferSize int } -func (wst *WebsocketTransport) Connect(host string) (conn Connection, err error) { +func (wst *WebsocketTransport) Connect(url string) (conn Connection, err error) { dialer := websocket.Dialer{} - socket, _, err := dialer.Dial(webSocketProtocol+host+socketioUrl, nil) + socket, _, err := dialer.Dial(url, nil) if err != nil { return nil, err }