From 8631548c59c3a83c6440a21ab750a5ab30c6001c Mon Sep 17 00:00:00 2001 From: BuYoungLee <52227539+buYoung@users.noreply.github.com> Date: Mon, 31 Aug 2020 16:48:20 +0900 Subject: [PATCH] Update client.go --- client.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index deded6c..a7bb618 100644 --- a/client.go +++ b/client.go @@ -17,6 +17,7 @@ Socket.io client representation type Client struct { methods Channel + url string } /** @@ -40,7 +41,7 @@ ws://myserver.com/socket.io/?EIO=3&transport=websocket You can use GetUrlByHost for generating correct url */ -func Dial(url string, tr transport.Transport) (*Client, error) { +func Dial(url string, tr transport.Transport,reconnect bool) (*Client, error) { c := &Client{} c.initChannel() c.initMethods() @@ -54,10 +55,32 @@ func Dial(url string, tr transport.Transport) (*Client, error) { go inLoop(&c.Channel, &c.methods) go outLoop(&c.Channel, &c.methods) go pinger(&c.Channel) - + if reconnect { + c.On(OnDisconnection, func(channel *Channel, msg interface{}) { + Redial(c) + }) + } return c, nil } +func Redial(c *Client) { + var err error + tr := transport.GetDefaultWebsocketTransport() + c.initChannel() + for { + c.conn, err = tr.Connect(c.url) + if err == nil { + break + } else { + time.Sleep(time.Second) + } + } + go inLoop(&c.Channel, &c.methods) + go outLoop(&c.Channel, &c.methods) + go pinger(&c.Channel) + +} + /** Close client connection */