forked from openimsdk/openim-sdk-core
/
simulationClient.go
116 lines (107 loc) · 3.03 KB
/
simulationClient.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
package test
import (
"encoding/json"
"net/url"
"sync"
"time"
"github.com/erbaner/be-core/pkg/log"
"github.com/erbaner/be-core/pkg/server_api_params"
"github.com/erbaner/be-core/pkg/utils"
"github.com/erbaner/be-core/sdk_struct"
"github.com/erbaner/be-core/ws_wrapper/test/client"
"github.com/erbaner/be-core/ws_wrapper/ws_local_server"
"github.com/gorilla/websocket"
)
var totalConnNum int
var lock sync.Mutex
var TotalSendMsgNum int
func StartSimulationJSClient(api, jssdkURL, userID string, i int, userIDList []string) {
// 模拟登录 认证 ws连接初始化
user := client.NewIMClient("", userID, api, jssdkURL, 5)
var err error
user.Token, err = user.GetToken()
if err != nil {
log.NewError("", "generate token failed", userID, api, err.Error())
user.Token, err = user.GetToken()
if err != nil {
log.NewError("", "generate token failed", userID, api, err.Error())
return
}
}
v := url.Values{}
v.Set("sendID", userID)
v.Set("token", user.Token)
v.Set("platformID", utils.IntToString(5))
c, _, err := websocket.DefaultDialer.Dial(jssdkURL+"?"+v.Encode(), nil)
if err != nil {
log.NewInfo("", "dial:", err.Error(), "userID", userID, "i: ", i)
return
}
lock.Lock()
totalConnNum += 1
log.NewInfo("", "connect success", userID, "total conn num", totalConnNum)
lock.Unlock()
user.Conn = c
// user.WsLogout()
user.WsLogin()
time.Sleep(time.Second * 2)
// 模拟登录同步
go user.GetSelfUserInfo()
go user.GetAllConversationList()
go user.GetBlackList()
go user.GetFriendList()
go user.GetRecvFriendApplicationList()
go user.GetRecvGroupApplicationList()
go user.GetSendFriendApplicationList()
go user.GetSendGroupApplicationList()
// 模拟监听回调
go func() {
for {
resp := ws_local_server.EventData{}
_, message, err := c.ReadMessage()
if err != nil {
log.NewError("", "read:", err, "error an connet failed", userID, i)
return
}
// log.Printf("recv: %s", message)
_ = json.Unmarshal(message, &resp)
if resp.Event == "CreateTextMessage" {
msg := sdk_struct.MsgStruct{}
_ = json.Unmarshal([]byte(resp.Data), &msg)
type Data struct {
RecvID string `json:"recvID"`
GroupID string `json:"groupID"`
OfflinePushInfo string `json:"offlinePushInfo"`
Message string `json:"message"`
}
offlinePushBytes, _ := json.Marshal(server_api_params.OfflinePushInfo{Title: "push offline"})
messageBytes, _ := json.Marshal(msg)
data := Data{RecvID: userID, OfflinePushInfo: string(offlinePushBytes), Message: string(messageBytes)}
err = user.SendMsg(userID, data)
//fmt.Println(msg)
lock.Lock()
TotalSendMsgNum += 1
lock.Unlock()
}
}
}()
// 模拟给随机用户发消息
go func() {
for {
err = user.CreateTextMessage(userID)
if err != nil {
log.NewError("", err, i, userID)
}
time.Sleep(time.Second * 2)
}
}()
// 模拟获取登陆状态
go func() {
for {
if err = user.GetLoginStatus(); err != nil {
log.NewError("", err, i, userID)
}
time.Sleep(time.Second * 10)
}
}()
}