-
Notifications
You must be signed in to change notification settings - Fork 5
/
chatServiceWebsocket.go
86 lines (70 loc) · 2.58 KB
/
chatServiceWebsocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2021 AccelByte Inc. All Rights Reserved.
// This is licensed software from AccelByte Inc, for limitations
// and restrictions contact your company contract manager.
package service
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/gorilla/websocket"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/model"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/repository"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils/connectionutils"
)
type ChatServiceWebsocket struct {
ConfigRepository repository.ConfigRepository
TokenRepository repository.TokenRepository
ConnectionManager connectionutils.ConnectionManager
}
func (s *ChatServiceWebsocket) JoinDefaultChannel() error {
logrus.Info("JoinDefaultChannel")
text := fmt.Sprintf("type: %s\n%s", model.TypeJoinDefaultChannelRequest, utils.GenerateMessageID())
err := s.ConnectionManager.Get().Conn.WriteMessage(websocket.TextMessage, []byte(text))
if err != nil {
return err
}
return nil
}
func (s *ChatServiceWebsocket) SendChannelChat(channelSlug, payload string) error {
logrus.Info("SendChannelChat")
text := fmt.Sprintf("type: %s\n"+
"%s\n"+
"channelSlug: %s\n"+
"payload: %s",
model.TypeSendChannelChatRequest,
utils.GenerateMessageID(),
channelSlug,
payload)
err := s.ConnectionManager.Get().Conn.WriteMessage(websocket.TextMessage, []byte(text))
if err != nil {
return err
}
return nil
}
func (s *ChatServiceWebsocket) SendPersonalChat(friendID, content string) error {
logrus.Info("SendPersonalChat")
text := fmt.Sprintf("type: %s\nid: %s\nto: %s\npayload: %s", model.TypePersonalChatRequest, utils.GenerateMessageID(), friendID, content)
err := s.ConnectionManager.Get().Conn.WriteMessage(websocket.TextMessage, []byte(text))
if err != nil {
return err
}
return nil
}
func (s *ChatServiceWebsocket) SendPartyChat(content string) error {
logrus.Info("SendPartyChat")
text := fmt.Sprintf("type: %s\nid: %s\npayload: %s", model.TypePartyChatRequest, utils.GenerateMessageID(), content)
err := s.ConnectionManager.Get().Conn.WriteMessage(websocket.TextMessage, []byte(text))
if err != nil {
return err
}
return nil
}
func (s *ChatServiceWebsocket) GetPersonalChatHistory(friendID string) error {
logrus.Info("GetPersonalChatHistory")
text := fmt.Sprintf("type: %s\n%s\nfriendId: %s", model.TypePersonalChatHistoryRequest, utils.GenerateMessageID(), friendID)
err := s.ConnectionManager.Get().Conn.WriteMessage(websocket.TextMessage, []byte(text))
if err != nil {
return err
}
return nil
}