From 5d53167775ca110e4454ae4016141c3ec38c91be Mon Sep 17 00:00:00 2001 From: Elara Musayelyan Date: Mon, 12 Dec 2022 18:40:55 -0800 Subject: [PATCH] Join WebSocket contexts on reconnect --- go.mod | 2 +- go.sum | 4 ++-- main.go | 23 ++++++++++++++--------- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index a45737b..0ec265d 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/pelletier/go-toml/v2 v2.0.6 github.com/spf13/pflag v1.0.5 github.com/vmihailenco/msgpack/v5 v5.3.5 - go.arsenm.dev/go-lemmy v0.0.0-20221213021222-a4a015a4cc2a + go.arsenm.dev/go-lemmy v0.0.0-20221213023656-b38c6b0a5455 go.arsenm.dev/logger v0.0.0-20221007032343-cbffce4f4334 go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e ) diff --git a/go.sum b/go.sum index 9fb55e8..b40909e 100644 --- a/go.sum +++ b/go.sum @@ -45,8 +45,8 @@ github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8= github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -go.arsenm.dev/go-lemmy v0.0.0-20221213021222-a4a015a4cc2a h1:iBeRt2sU8Y3pwKPKTFnkIcTwFTvifyCLUtjjsj7CaWM= -go.arsenm.dev/go-lemmy v0.0.0-20221213021222-a4a015a4cc2a/go.mod h1:/XgX+cQqs9a2VJWwZ67wArjK2CVXWKdEEpHdAFbFRNQ= +go.arsenm.dev/go-lemmy v0.0.0-20221213023656-b38c6b0a5455 h1:AUeVhWSB6Wzz6TDiMSi6TGrzXyPGg3flXdASs+42vTA= +go.arsenm.dev/go-lemmy v0.0.0-20221213023656-b38c6b0a5455/go.mod h1:hLkcWy1iV2PlzZZVWjuV3Y3hMynM0k8g9Ul94CcusUM= go.arsenm.dev/logger v0.0.0-20221007032343-cbffce4f4334 h1:S98LJOBmj1pAKSw94spJk6+n8ERlBNTxi4lt5B67nQo= go.arsenm.dev/logger v0.0.0-20221007032343-cbffce4f4334/go.mod h1:RV2qydKDdoyaRkhAq8JEGvojR8eJ6bjq5WnSIlH7gYw= go.arsenm.dev/pcre v0.0.0-20220530205550-74594f6c8b0e h1:4XwLmFDvAKt7ZvS3E3hD2R++0wr75fBUEvXkK9dLXzk= diff --git a/main.go b/main.go index 3bd7217..6981d49 100644 --- a/main.go +++ b/main.go @@ -45,17 +45,22 @@ func main() { log.Info("Successfully logged in to Lemmy instance").Send() - err = c.Request(types.UserOpUserJoin, nil) - if err != nil { - log.Fatal("Error joining WebSocket user context").Err(err).Send() - } + c.SetConnectHandler(func() error { + err = c.Request(types.UserOpUserJoin, nil) + if err != nil { + log.Fatal("Error joining WebSocket user context").Err(err).Send() + } - err = c.Request(types.UserOpCommunityJoin, types.CommunityJoin{ - CommunityID: 0, + err = c.Request(types.UserOpCommunityJoin, types.CommunityJoin{ + CommunityID: 0, + }) + if err != nil { + log.Fatal("Error joining WebSocket community context").Err(err).Send() + } + return nil }) - if err != nil { - log.Fatal("Error joining WebSocket community context").Err(err).Send() - } + + c.ConnectHandler() replyCh := make(chan replyJob, 200)