From 54657499bbd496a0a9b7155b3622c6549ed87e0c Mon Sep 17 00:00:00 2001 From: douglascdev Date: Fri, 5 Jul 2024 15:07:25 -0300 Subject: [PATCH] Separate sockets for reading and writing(#2) --- CHANGELOG.md | 1 + fyne_ui.go | 18 ++++------- hasherino/api.go | 7 +--- hasherino/controller.go | 72 ++++++++++++++++++++++++----------------- hasherino/ws.go | 2 +- 5 files changed, 52 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cea0643..f1fc9e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,4 @@ Changes: - Emote search - Reconnect on EOF - Close app when main window is closed but others are still open +- Separate sockets for reading and writing(#2) diff --git a/fyne_ui.go b/fyne_ui.go index fb25974..1f73f81 100644 --- a/fyne_ui.go +++ b/fyne_ui.go @@ -232,7 +232,7 @@ func NewEmoteCanvasObject(emote *hasherino.Emote) (*fyne.CanvasObject, error) { func NewChatTab( channel string, - sendMsg func(string) (string, error), + sendMsg func(string) error, getEmotes func(string) ([]*hasherino.Emote, error), window fyne.Window, settingsFunc func() (*hasherino.AppSettings, error), @@ -307,14 +307,13 @@ func NewChatTab( return } - author, err := sendMsg(text) + err = sendMsg(text) if err != nil { dialog.ShowError(err, window) return } msgEntry.SetText("") - data = append(data, author+": "+text) messageList.ScrollToBottom() messageList.Refresh() } @@ -449,19 +448,16 @@ func main() { chatTabs.Refresh() } - sendMessage := func(message string) (string, error) { + sendMessage := func(message string) error { currentTab, err := hc.GetSelectedTab() if err != nil { - return "", err + return err } - if !hc.IsChannelJoined(currentTab.Login) { - return "", errors.New("Channel not joined. Please make sure you have an active account on settings.") - } - ac, err := hc.GetActiveAccount() + _, err = hc.GetActiveAccount() if err != nil { - return "", err + return err } - return ac.Login, hc.SendMessage(currentTab.Login, message) + return hc.SendMessage(currentTab.Login, message) } savedTabs, err := hc.GetTabs() diff --git a/hasherino/api.go b/hasherino/api.go index b61b070..47a1f90 100644 --- a/hasherino/api.go +++ b/hasherino/api.go @@ -27,7 +27,6 @@ func NewTwitchOAuth() *TwitchOAuth { func (t *TwitchOAuth) IsTokenValid(token string) bool { req, err := http.NewRequest("GET", "https://id.twitch.tv/oauth2/validate", nil) - if err != nil { log.Printf("Failed to create request for token validation: %s", err) return false @@ -136,7 +135,7 @@ func (t *TwitchOAuth) ListenForOAuthRedirect(hc *HasherinoController) { } w.WriteHeader(200) - w.Write([]byte("Account added")) + w.Write([]byte("Account added, refresh your accounts list.")) }) err := http.ListenAndServe(":17563", nil) @@ -185,7 +184,6 @@ func (h *Helix) GetUsers(token string, usernames []string) (*HelixUsers, error) } req, err := http.NewRequest("GET", url+"?"+params, nil) - if err != nil { log.Printf("Failed to create request for helix users: %s Params: %s", err, params) return nil, err @@ -214,7 +212,6 @@ func (h *Helix) GetUsers(token string, usernames []string) (*HelixUsers, error) } return &users, nil - } type ChatMessagesJson struct { @@ -229,7 +226,6 @@ func GetChatHistory(channel string, limit int) (*[]ChatMessage, error) { url += "?limit=" + strconv.Itoa(limit) req, err := http.NewRequest("GET", url, nil) - if err != nil { log.Printf("Failed to create request for chat history: %s", err) return nil, err @@ -270,7 +266,6 @@ func GetChatHistory(channel string, limit int) (*[]ChatMessage, error) { } return &messages, nil - } type EmoteSet struct { diff --git a/hasherino/controller.go b/hasherino/controller.go index b4f7cf7..c0bc1fe 100644 --- a/hasherino/controller.go +++ b/hasherino/controller.go @@ -22,13 +22,15 @@ type HasherinoController struct { selectedTab string callbackMap map[string]func(ChatMessage) twitchOAuth *TwitchOAuth - chatWS *TwitchChatWebsocket + readWS *TwitchChatWebsocket + writeWS *TwitchChatWebsocket memDB *gorm.DB permDB *gorm.DB } func (hc *HasherinoController) New(callbackMap map[string]func(ChatMessage)) (*HasherinoController, error) { - chatWS := &TwitchChatWebsocket{} + writeWS := &TwitchChatWebsocket{} + readWS := &TwitchChatWebsocket{} dataFolder, err := GetDataFolder() if err != nil { @@ -51,7 +53,8 @@ func (hc *HasherinoController) New(callbackMap map[string]func(ChatMessage)) (*H appId: "hvmj7blkwy2gw3xf820n47i85g4sub", callbackMap: callbackMap, twitchOAuth: NewTwitchOAuth(), - chatWS: chatWS, + readWS: readWS, + writeWS: writeWS, memDB: memDB, permDB: permDB, } @@ -89,7 +92,7 @@ func (hc *HasherinoController) RemoveAccount(id string) error { return result.Error } if account.Active { - hc.chatWS.Close() + hc.writeWS.Close() } result = tx.Delete(&account) if result.Error != nil { @@ -135,8 +138,8 @@ func (hc *HasherinoController) SetActiveAccount(id string) error { return result.Error } - if hc.chatWS != nil { - hc.chatWS.Close() + if hc.writeWS != nil { + hc.writeWS.Close() } go hc.Listen() @@ -162,21 +165,21 @@ func (hc *HasherinoController) OpenOAuthPage() { func (hc *HasherinoController) AddTab(channel string) error { err := hc.permDB.Transaction(func(tx *gorm.DB) error { + _, exists := hc.readWS.channels[channel] + if exists { + return errors.New("already joined channel " + channel) + } + activeAccount := &Account{} result := hc.permDB.Take(&activeAccount, "Active = ?", true) if result.Error != nil { - return errors.New("No active account") - } - - _, exists := hc.chatWS.channels[channel] - if exists { - return errors.New("Already joined channel " + channel) + return errors.New("no active account") } helix := NewHelix(hc.appId) users, err := helix.GetUsers(activeAccount.Token, []string{channel}) if err != nil || len(users.Data) != 1 { - return errors.New("Failed to obtain channel's id and login") + return errors.New("failed to obtain channel's id and login") } tab := &Tab{ @@ -188,10 +191,10 @@ func (hc *HasherinoController) AddTab(channel string) error { result = tx.Create(&tab) - err = hc.chatWS.Join(channel) + err = hc.readWS.Join(channel) if err != nil { - log.Printf("Failed to join channel %s: %s", channel, err) - return errors.New("Failed to join channel " + channel) + log.Printf("failed to join channel %s: %s", channel, err) + return errors.New("failed to join channel " + channel) } err = hc.AddTempTabs(&[]string{users.Data[0].ID}) @@ -401,7 +404,7 @@ func (hc *HasherinoController) RemoveTab(id string) error { tab := &Tab{} result := tx.Take(&tab, "Id = ?", id) if result.Error != nil { - return errors.New("Tab not found for id " + id) + return errors.New("tab not found for id " + id) } result = tx.Delete(&tab) @@ -414,9 +417,9 @@ func (hc *HasherinoController) RemoveTab(id string) error { return result.Error } - err := hc.chatWS.Part(tab.Login) + err := hc.readWS.Part(tab.Login) if err != nil { - return errors.New("Failed to part channel" + tab.Login) + return errors.New("failed to part channel" + tab.Login) } return nil @@ -468,15 +471,24 @@ func (hc *HasherinoController) GetSelectedTab() (*Tab, error) { func (hc *HasherinoController) Listen() error { activeAccount, err := hc.GetActiveAccount() - if err != nil { - return err + if err == nil { + if hc.writeWS == nil || hc.writeWS.State == Disconnected { + hc.writeWS, err = hc.writeWS.New("oauth:"+activeAccount.Token, activeAccount.Login) + if err != nil { + return err + } + err = hc.writeWS.Connect() + if err != nil { + return err + } + } } - if hc.chatWS == nil || hc.chatWS.State == Disconnected { - hc.chatWS, err = hc.chatWS.New(activeAccount.Token, activeAccount.Login) + if hc.readWS == nil || hc.readWS.State == Disconnected { + hc.readWS, err = hc.readWS.New("SCHMOOPIIE", "justinfan71097") if err != nil { return err } - err = hc.chatWS.Connect() + err = hc.readWS.Connect() if err != nil { return err } @@ -498,27 +510,27 @@ func (hc *HasherinoController) Listen() error { } for channel := range hc.callbackMap { - _, joinedChannel := hc.chatWS.channels[channel] + _, joinedChannel := hc.writeWS.channels[channel] if !joinedChannel { - hc.chatWS.Join(channel) + hc.readWS.Join(channel) } } - go hc.chatWS.Listen(callbackWrapper) + go hc.readWS.Listen(callbackWrapper) return nil } func (hc *HasherinoController) IsChannelJoined(channel string) bool { - if hc.chatWS == nil || hc.chatWS.State == Disconnected { + if hc.readWS == nil || hc.readWS.State == Disconnected { return false } - _, joinedChannel := hc.chatWS.channels[channel] + _, joinedChannel := hc.readWS.channels[channel] return joinedChannel } func (hc *HasherinoController) SendMessage(channel string, message string) error { - err := hc.chatWS.Send(channel, message) + err := hc.writeWS.Send(channel, message) return err } diff --git a/hasherino/ws.go b/hasherino/ws.go index e76a829..ef07d52 100644 --- a/hasherino/ws.go +++ b/hasherino/ws.go @@ -31,7 +31,7 @@ type TwitchChatWebsocket struct { func (w *TwitchChatWebsocket) New(token string, user string) (*TwitchChatWebsocket, error) { initial_messages := []string{ "CAP REQ :twitch.tv/commands twitch.tv/tags", - "PASS oauth:" + token, + "PASS " + token, "NICK " + user, } url := "wss://irc-ws.chat.twitch.tv"