RocketChat / Rocket.Chat.Go.SDK

Go SDK for REST API and Realtime api
MIT License
59 stars 58 forks source link

Update gopackage/ddp to fix a problem with realtime client close #76

Open RegularITCat opened 1 year ago

RegularITCat commented 1 year ago

Hi! First of all, thanks, that's a great project!

In realtime api there's a problem when client closes connection.

    RocketURL, err := url.Parse(c.RocketChatAddress)
    if err != nil {
        log.Println(err)
    }
    RocketClient, err := realtime.NewClient(RocketURL, false)
    if err != nil {
        log.Println(err)
    }
    RocketCreds := RocketModels.UserCredentials{ID: c.RocketXUserID, Token: c.RocketXAuthToken}
    _, err = RocketClient.Login(&RocketCreds)
    if err != nil {
        log.Println(err)
    }
    tmp := make(chan RocketModels.Message, 10)
    err = RocketClient.SubscribeToMyMessages(tmp)
    if err != nil {
        log.Println(err)
    }
    log.Println("Started connection with rocket chat")
    for {
        select {
        case msg := <-tmp:
            log.Println(msg)
            //TODO process msg using regexps, check in db all info, give it to user.
        case <-done:
            RocketClient.Close()
            return
        default:
        }
    }

In my case, i wanted to on receiving syscall sigterm safely close everything. And when i callled client close, i received in logger a problem like this

2023/02/06 16:54:48 Server is starting to accept connections
2023/02/06 16:54:48 About to connect to: wss://localhost:3000/websocket 3000 https
2023/02/06 16:54:48 Started connection with rocket chat
^C2023/02/06 16:55:05 Got SIGINT/SIGTERM, exiting.
2023/02/06 16:55:05 Inbox worker found nil event. May be due to broken websocket. Reconnecting.
exit status 1

When i started researching this undefined behavior, found this https://github.com/gopackage/ddp/pull/13 So, all work needed, is to update a library...