Skip to content

Commit

Permalink
Separate sockets for reading and writing(#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascdev committed Jul 5, 2024
1 parent bf50f98 commit 5465749
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 7 additions & 11 deletions fyne_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 1 addition & 6 deletions hasherino/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -214,7 +212,6 @@ func (h *Helix) GetUsers(token string, usernames []string) (*HelixUsers, error)
}

return &users, nil

}

type ChatMessagesJson struct {
Expand All @@ -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
Expand Down Expand Up @@ -270,7 +266,6 @@ func GetChatHistory(channel string, limit int) (*[]ChatMessage, error) {
}

return &messages, nil

}

type EmoteSet struct {
Expand Down
72 changes: 42 additions & 30 deletions hasherino/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()

Expand All @@ -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{
Expand All @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion hasherino/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5465749

Please sign in to comment.