jcelliott / turnpike

Go implementation of a WAMP (Web Application Messaging Protocol) client and router
MIT License
258 stars 88 forks source link

Client stops receiving messages #139

Open iamsaso opened 7 years ago

iamsaso commented 7 years ago

I am connecting to a websocket and randomly my app stops receiving messages. There is no message that anything failed or that websocket was disconnected or server went away... Did anyone experiance same problems and is there a way to check if everything is ok (like ping) and to reconnect if server goes down.

@gngeorgiev, @beatgammit, @digitallumens, @dcarbone sry for ping but I see you are all maintaining your own forks... has anyone experienced similar issues and maybe created a fix that I can test/use?

func (c *Client) Ticker(ctx context.Context) (<-chan *Symbol, error) {
    symbols := make(chan *Symbol)
    var once sync.Once

    client, err := turnpike.NewWebsocketClient(turnpike.JSON, "wss://api.poloniex.com", nil, nil)
    if err != nil {
        log.Fatal(err)
    }

    client.ReceiveTimeout = 30 * time.Second
    _, err = client.JoinRealm("realm1", nil)
    if err != nil {
        log.Fatal(err)
    }

    err = client.Subscribe("ticker", nil, turnpike.EventHandler(func(args []interface{}, kwargs map[string]interface{}) {
        symbol, err := toSymbol(args)
        if err != nil {
            log.Println("encountered an error converting an event to a Symbol:", err)
        }

        select {
        case <-ctx.Done():
            once.Do(func() {
                close(symbols)
                symbols = nil
                if err := client.Unsubscribe("ticker"); err != nil {
                    log.Println("encountered error during unsubscription:", err)
                }
                log.Println("Ticker disconnected")
            })
        case symbols <- symbol:
        }
    }))
    if err != nil {
        log.Println("encountered an error subscribing to the 'ticker' topic:", errors.Wrap(err, "error subscribing to 'ticker' topic"))
        return nil, errors.WithStack(err)
    }

    return symbols, nil
}
utdrmac commented 6 years ago

@sasso Any update on this? Running into a similar issue and I was also wondering about 'ping' ability. I've wrapped NewWebsocketClient and JoinRealm/Subscribe inside some infinite-for-loops so that if connection drops, it will auto-retry after a sleep timeout. I'm having "trust" issues that once I connect to the wss I really am connected.