Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Re connection (client) #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Socket.io client representation
type Client struct {
methods
Channel
url string
}

/**
Expand All @@ -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()
Expand All @@ -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
*/
Expand Down