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

Best way to implement reconnect #141

Open
utdrmac opened this issue Nov 3, 2017 · 0 comments
Open

Best way to implement reconnect #141

utdrmac opened this issue Nov 3, 2017 · 0 comments

Comments

@utdrmac
Copy link

utdrmac commented Nov 3, 2017

I frequently get "Unable to connect: Failed handshake" from the service to which I'm attempting connections. What's the best way to handle auto-rety/auto-reconnect? Right now I have this (I'm not the best Go programmer)

var c *turnpike.Client
	var err error

	for {
		c, err = turnpike.NewWebsocketClient(turnpike.JSON, WEBSOCKET_ADDRESS, nil, nil, nil)
		if err != nil {
			log.Printf("Unable to connect to Websocket. Error: %s\n", err)

			// Retry in 5 seconds
			time.Sleep(5)

			continue
		}
	
		log.Println("Connected to Websocket.")

		_, err = c.JoinRealm(WEBSOCKET_REALM, nil)
		if err != nil {
			log.Printf("Unable to join realm. Error: %s\n", err)
			c.Close()
			
			// Retry in 5 seconds
			time.Sleep(5)
			
			continue
		}

		log.Println("Joined Websocket realm.")

		// No errs; break loop
		break
	}

       // have successful connection and successful join realm
       // continue code
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

1 participant