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

Reconnecting after disconnect #51

Open
gilphilbert opened this issue Oct 29, 2019 · 1 comment
Open

Reconnecting after disconnect #51

gilphilbert opened this issue Oct 29, 2019 · 1 comment

Comments

@gilphilbert
Copy link

gilphilbert commented Oct 29, 2019

I'm trying to set up a persistent connection to a socketio server that might restart. If that happens, I want to reconnect without needing to redefine my methods, etc.

I've added a new item to Client to store the URL:

type Client struct {
        ....
        url string  //stores the url for this connection
        ....

And added a line to dial so it stores the URL:

func Dial(url string, tr transport.Transport) (*Client, error) {
        ...
        c.url = url 
        ...

And added a new redial function (I know it's an endless loop and it needs some waits, but it's OK for testing...)

func Redial(c *Client) {
        var err error
        tr := transport.GetDefaultWebsocketTransport()
        for {
              c.conn, err = tr.Connect(c.url)
              if err == nil {
                    break
              }
        }
        go inLoop(&c.Channel, &c.methods)
        go outLoop(&c.Channel, &c.methods)
        go pinger(&c.Channel)
}

Then, I've added this to my code:

socket.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
    log.Println("Disconnected from server, trying to reconnect")
    gosocketio.Redial(socket)
}

This works, other than the socket.IsAlive() method returns false. I'm so close, but I just can't seem to get this right! I'm happy if there's a much better way to handle this, but this is what I came up with. Any help would be appreciated

@jackleibest
Copy link

hi, I have solved this problem, just change your some codes

func Redial(c *Client) (*Client, error) {
var err error
tr := transport.GetDefaultWebsocketTransport()
for {
c.conn, err = tr.Connect(c.url)
if err == nil {
break
}
}
go inLoop(&c.Channel, &c.methods)
go outLoop(&c.Channel, &c.methods)
go pinger(&c.Channel)
return c, nil
}

socket.On(gosocketio.OnDisconnection, func(c *gosocketio.Channel) {
log.Println("Disconnected from server, trying to reconnect")
socket,_ = gosocketio.Redial(socket)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants