-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
executable file
·191 lines (174 loc) · 5.22 KB
/
client.js
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const WebSocket = require('ws');
const program = require('commander');
const {PacketModel} = require('./build/model/packet/PacketModel');
const {API_FROM, API_MSG_TYPE, API_TYPE} = require('./build/const/Common');
const {CommonTools, TimeTools} = require('./build/common/Utility');
program
.option('-u, --uid [uid]', `Add uid [999999]`, '999999')
.option('-t, --token [token]', `Add token [1q2w3e4r]`, '1q2w3e4r')
.option('-p, --path [path]', `Add login path [ws://127.0.0.1:8080]`, 'ws://127.0.0.1:8080')
.parse(process.argv);
console.log('----------------------------------------------------------------');
console.log(' TCP Client Commander:');
console.log(' - uid: %s', program.uid);
console.log(' - token: %s', program.token);
console.log(' - path: %s', program.path);
console.log('----------------------------------------------------------------');
// 创建WekSocket连接
class ClientUser {
_conn;
constructor(uid) {
this._uid = uid;
this._conn = null;
this.connect();
}
connect() {
let ip = CommonTools.eth0();
let time = TimeTools.getTime();
let token = (program.mod === 'strict') ? program.token : CommonTools.genString(`Y#K&D*H.ply_${this._uid}`, ip, time);
let ws = new WebSocket(program.path, `${this._uid}|${token}|${ip}|${time}`);
ws.on('open', () => {
console.log('connected succeed');
this._heartbeat();
});
ws.on('message', (r) => {
console.log('------------------------message------------------------');
console.log(r);
console.log('------------------------message------------------------');
});
ws.on('close', (code, reason) => {
console.log('------------------------close------------------------');
console.log(code, reason);
console.log('------------------------close------------------------');
process.exit();
});
ws.on('error', (e) => {
console.log('------------------------error------------------------');
console.log(e);
console.log('------------------------error------------------------');
process.exit();
});
this._conn = ws;
}
stdin() {
setTimeout(() => {
if (this._conn == null || this._conn.readyState !== WebSocket.OPEN) {
this.stdin();
} else {
// 监控控制台输入
process.stdin.resume();
process.stdin.on('data', (text) => {
const action = text.toString().replace(/\r?\n|\r/g, '').trim();
switch (action) {
case 'chat':
this._chat();
break;
case 'pchat':
this._pChat();
break;
case 'gjoin':
this._gJoin();
break;
case 'gchat':
this._gChat();
break;
case 'gaction':
this._gAction();
break;
case 'close':
this._logout();
break;
}
});
}
}, 1000);
}
_heartbeat() {
setTimeout(() => {
this._conn.send(PacketModel.create(
API_TYPE.IM_UPDATE_INFO,
(this._uid == '0') ? API_FROM.IM_FROM_TYPE_SYSTEM : API_FROM.IM_FROM_TYPE_USER,
1,
{
uid: this._uid,
data: {
heartbeat: TimeTools.getTime()
}
}
).format());
this._heartbeat();
}, 30000);
}
_chat() {
this._conn.send(PacketModel.create(
API_TYPE.IM_WORLD_CHAT,
(this._uid == '0') ? API_FROM.IM_FROM_TYPE_SYSTEM : API_FROM.IM_FROM_TYPE_USER,
1,
{type: API_MSG_TYPE.IM_CHAT, msg: 'This is world message from' + this._uid}
).format());
}
_gJoin() {
this._conn.send(PacketModel.create(
API_TYPE.IM_GROUP_JOIN,
API_FROM.IM_FROM_TYPE_USER,
1,
{groupId: '1_12_1'}
).format());
}
_gChat() {
this._conn.send(PacketModel.create(
API_TYPE.IM_GROUP_CHAT,
(this._uid == '0') ? API_FROM.IM_FROM_TYPE_SYSTEM : API_FROM.IM_FROM_TYPE_USER,
1,
{type: API_MSG_TYPE.IM_CHAT, groupId: '1_12_1', msg: 'This is group message from' + this._uid}
).format());
}
_gAction() {
this._conn.send(PacketModel.create(
API_TYPE.IM_GROUP_ACTION,
(this._uid == '0') ? API_FROM.IM_FROM_TYPE_SYSTEM : API_FROM.IM_FROM_TYPE_USER,
1,
{
type: API_MSG_TYPE.IM_ACTION,
action: 1,
groupId: '1_1',
data: {
answerAddUp: 1,
answerIsRight: 1
}
}
).format());
}
_pChat() {
this._conn.send(PacketModel.create(
API_TYPE.IM_PRIVATE_CHAT,
(this._uid == '0') ? API_FROM.IM_FROM_TYPE_SYSTEM : API_FROM.IM_FROM_TYPE_USER,
1,
{type: API_MSG_TYPE.IM_CHAT, receive: 999999, msg: 'This is private message from' + this._uid}
).format());
}
_pAction() {
this._conn.send(PacketModel.create(
API_TYPE.IM_PRIVATE_ACTION,
API_FROM.IM_FROM_TYPE_USER,
1,
{
type: API_MSG_TYPE.IM_ACTION,
action: 21,
receive: 10001,
data: {
XXX: 'XXX'
}
}
).format());
}
_logout() {
this._conn.send(PacketModel.create(
API_TYPE.IM_LOGOUT,
API_FROM.IM_FROM_TYPE_USER,
1,
{uid: this._uid}
).format());
}
}
new ClientUser(program.uid).stdin();