forked from openimsdk/openim-sdk-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
176 lines (153 loc) · 4.7 KB
/
client.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package client
import (
"encoding/json"
"sync"
"github.com/erbaner/be-core/pkg/network"
"github.com/erbaner/be-core/pkg/server_api_params"
"github.com/erbaner/be-core/pkg/utils"
"github.com/gorilla/websocket"
)
type IMClient struct {
Token string
UserID string
ApiURL string
JssdkURL string
Platform int
Conn *websocket.Conn
Lock sync.Mutex
}
func NewIMClient(token, userID, apiURL, jssdkURL string, platform int) *IMClient {
return &IMClient{
Token: token,
UserID: userID,
ApiURL: apiURL,
JssdkURL: jssdkURL,
Platform: platform,
}
}
func (i *IMClient) GetToken() (string, error) {
req := struct {
Secret string `json:"secret"`
Platform int32 `json:"platform"`
UserID string `json:"userID"`
OperationID string `json:"operationID"`
}{Secret: "tuoyun", Platform: int32(i.Platform), UserID: i.UserID, OperationID: utils.OperationIDGenerator()}
content, err := network.Post2Api(i.ApiURL+"/auth/user_token", req, "")
if err != nil {
return "", err
}
type respToken struct {
Data struct {
ExpiredTime int64 `json:"expiredTime"`
Token string `json:"token"`
Uid string `json:"uid"`
}
ErrCode int `json:"errCode"`
ErrMsg string `json:"errMsg"`
}
resp := respToken{}
err = json.Unmarshal(content, &resp)
return resp.Data.Token, err
}
func (i *IMClient) GetALLUserIDList() ([]string, error) {
req := struct {
OperationID string `json:"operationID"`
}{OperationID: utils.OperationIDGenerator()}
content, err := network.Post2Api(i.ApiURL+"/user/get_all_users_uid", req, i.Token)
if err != nil {
return nil, err
}
type Resp struct {
UserIDList []string `json:"data"`
}
resp := Resp{}
err = json.Unmarshal(content, &resp)
return resp.UserIDList, err
}
func (i *IMClient) WsLogin() error {
loginContent := struct {
UserID string `json:"userID"`
Token string `json:"token"`
}{UserID: i.UserID, Token: i.Token}
return i.writeMessage(i.getWsReq("Login", 1, loginContent))
}
func (i *IMClient) WsLogout() error {
return i.writeMessage(i.getWsReq("Logout", 0, nil))
}
func (i *IMClient) GetLoginStatus() error {
return i.writeMessage(i.getWsReq("GetLoginStatus", 0, nil))
}
func (i *IMClient) CreateTextMessage(userID string) error {
msg := server_api_params.MsgData{
SendID: i.UserID,
RecvID: "MTc3MjYzNzg0Mjg=",
GroupID: "",
SenderPlatformID: int32(i.Platform),
ClientMsgID: utils.GetMsgID(i.UserID),
CreateTime: utils.GetCurrentTimestampByMill(),
SendTime: utils.GetCurrentTimestampByMill(),
SessionType: 1,
MsgFrom: 100,
ContentType: 101,
OfflinePushInfo: &server_api_params.OfflinePushInfo{Title: "offlinePush"},
}
return i.writeMessage(i.getWsReq("CreateTextMessage", 1, msg))
}
func (i *IMClient) SendMsg(userID string, msg interface{}) error {
return i.writeMessage(i.getWsReq("SendMessage", 1, msg))
}
func (i *IMClient) GetSelfUserInfo() error {
return i.writeMessage(i.getWsReq("GetSelfUserInfo", 0, nil))
}
func (i *IMClient) GetAllConversationList() error {
return i.writeMessage(i.getWsReq("GetAllConversationList", 0, nil))
}
func (i *IMClient) GetFriendList() error {
return i.writeMessage(i.getWsReq("GetFriendList", 0, nil))
}
func (i *IMClient) GetRecvFriendApplicationList() error {
return i.writeMessage(i.getWsReq("GetRecvFriendApplicationList", 0, nil))
}
func (i *IMClient) GetSendFriendApplicationList() error {
return i.writeMessage(i.getWsReq("GetSendFriendApplicationList", 0, nil))
}
func (i *IMClient) GetJoinedGroupList() error {
return i.writeMessage(i.getWsReq("GetJoinedGroupList", 0, nil))
}
func (i *IMClient) GetRecvGroupApplicationList() error {
return i.writeMessage(i.getWsReq("GetRecvFriendApplicationList", 0, nil))
}
func (i *IMClient) GetSendGroupApplicationList() error {
return i.writeMessage(i.getWsReq("GetSendFriendApplicationList", 0, nil))
}
func (i *IMClient) GetBlackList() error {
return i.writeMessage(i.getWsReq("GetJoinedGroupList", 0, nil))
}
func (i *IMClient) writeMessage(bytes []byte) error {
i.Lock.Lock()
defer i.Lock.Unlock()
return i.Conn.WriteMessage(1, bytes)
}
func (i *IMClient) getWsReq(event string, batch int, data interface{}) []byte {
type Req struct {
ReqFuncName string `json:"reqFuncName" `
OperationID string `json:"operationID"`
Data string `json:"data"`
UserID string `json:"userID"`
Batch int `json:"batchMsg,omitempty"`
}
req := Req{
ReqFuncName: event,
OperationID: i.UserID + utils.OperationIDGenerator(),
UserID: i.UserID,
}
req.Batch = batch
var bytes []byte
if data != nil {
bytes, _ := json.Marshal(data)
req.Data = string(bytes)
}
bytes, _ = json.Marshal(req)
// log.Println("send:", string(bytes))
return bytes
}