buzzk 1.9.4 → 1.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Emin-G
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Emin-G
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
21
  SOFTWARE.
package/lib/chat.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const { getStatus } = require("./live.js");
2
2
  const { reqChzzk, reqGame } = require("./tool.js");
3
+ const vm = require("./vm.js");
3
4
 
4
5
  const { WebSocket } = require("ws");
5
6
 
@@ -19,8 +20,15 @@ class chzzkChat {
19
20
  #chatID;
20
21
  #callbacks = new Map();
21
22
 
22
- #pollingStatus = false;
23
- #pingStatus = false;
23
+ #status = {
24
+ polling: false,
25
+ pollingID: 0,
26
+ ping: false,
27
+ pingID: 0,
28
+ ws: false,
29
+ wsID: 0,
30
+ reconnect: false
31
+ }
24
32
  //Private
25
33
 
26
34
  /**
@@ -28,29 +36,32 @@ class chzzkChat {
28
36
  */
29
37
  connect() {
30
38
  return new Promise(async (resolve, reject) => {
31
- if (this.#ws) return resolve(null);
32
-
33
- //Get UID
34
- let myInfo = await reqGame("nng_main/v1/user/getUserStatus");
35
- if (myInfo.code != 200) return resolve(null);
36
- this.#uid = myInfo.content.userIdHash;
37
-
38
- //SetPolling
39
- if (!this.#pollingStatus) {
40
- this.#pollingStatus = true;
41
-
42
- setTimeout(async () => {
43
- this.#polling();
44
- }, 30000);
45
- }
46
- //SetPolling
39
+ if (this.#status.ws) return resolve(null);
40
+ if (this.#status.polling && !this.#status.reconnect) return resolve(null);
41
+ else this.#status.reconnect = false;
47
42
 
48
43
  //Get ChatID
49
44
  let cidRes = await getStatus(this.channelID);
50
- if (!cidRes) return resolve(null);
45
+ if (!cidRes) {
46
+ if (!this.#status.polling) {
47
+ this.#status.polling = true;
48
+ this.#polling();
49
+ }
50
+ return resolve(null);
51
+ }
51
52
  this.#chatID = cidRes.chatID;
52
53
  //Get ChatID
53
54
 
55
+ //Set Status
56
+ this.#status.ws = true;
57
+ //Set Status
58
+
59
+ //Get UID
60
+ let myInfo = await reqGame("nng_main/v1/user/getUserStatus");
61
+ if (myInfo.code != 200) return resolve(null);
62
+ this.#uid = myInfo.content.userIdHash;
63
+ //Get UID
64
+
54
65
  //Get accTkn
55
66
  let accRes = await reqGame("nng_main/v1/chats/access-token?channelId=" + this.#chatID + "&chatType=STREAMING");
56
67
  if (accRes.code != 200) return resolve(null);
@@ -64,6 +75,7 @@ class chzzkChat {
64
75
 
65
76
  //Connect Web Socket
66
77
  this.#ws = new WebSocket("wss://kr-ss" + this.#ssID + ".chat.naver.com/chat");
78
+ this.#status.wsID++;
67
79
  //Connect Web Socket
68
80
 
69
81
  //WS Open
@@ -72,7 +84,7 @@ class chzzkChat {
72
84
 
73
85
  //WS First Connect
74
86
  let connectOpt = {
75
- "ver": "2",
87
+ "ver": "3",
76
88
  "cmd": 100,
77
89
  "svcid": "game",
78
90
  "cid": this.#chatID,
@@ -80,7 +92,11 @@ class chzzkChat {
80
92
  "uid": this.#uid,
81
93
  "devType":2001,
82
94
  "accTkn": this.#accTkn,
83
- "auth":"SEND"
95
+ "auth":"SEND",
96
+ "devName": "BUZZK/" + vm.getVersion(),
97
+ "locale": "ko",
98
+ "osVer": "Windows/10",
99
+ "timezone": "Asia/Seoul"
84
100
  },
85
101
  "tid": 1
86
102
  };
@@ -94,6 +110,13 @@ class chzzkChat {
94
110
 
95
111
  this.#ws.on("close", () => {
96
112
  console.log("[WS] Disconnect!");
113
+ this.#status.ws = false;
114
+ this.#status.wsID++;
115
+ this.#status.ping = false;
116
+ this.#status.pingID++;
117
+ setTimeout(() => {
118
+ this.connect();
119
+ }, 2000);
97
120
  });
98
121
 
99
122
  //WS Message
@@ -124,7 +147,21 @@ class chzzkChat {
124
147
 
125
148
  //Connected
126
149
  else if (data.cmd == 10100) {
127
- if (!this.#pingStatus) this.#ping();
150
+ if (!this.#status.ping) {
151
+ this.#status.ping = true;
152
+ this.#status.pingID++;
153
+ this.#ping(this.#status.pingID);
154
+ }
155
+ if (!this.#status.polling) {
156
+ this.#status.polling = true;
157
+ this.#polling();
158
+ }
159
+
160
+ for (let o in this.#callbacks) {
161
+ if (this.#callbacks[o].type === "message") this.#onMessageHandler(this.#status.wsID, this.#callbacks[o].callback);
162
+ else if (this.#callbacks[o].type === "donation") this.#onDonationHandler(this.#status.wsID, this.#callbacks[o].callback);
163
+ }
164
+
128
165
  resolve(true);
129
166
  }
130
167
  //Connected
@@ -140,7 +177,7 @@ class chzzkChat {
140
177
  */
141
178
  send(message) {
142
179
  return new Promise(async (resolve, reject) => {
143
- if (!this.#ws) return resolve(null);
180
+ if (!this.#status.ws) return resolve(null);
144
181
 
145
182
  let extras = {
146
183
  "chatType":"STREAMING",
@@ -204,13 +241,15 @@ class chzzkChat {
204
241
  type: "message",
205
242
  callback: callback
206
243
  };
207
- this.#onMessageHandler(callback);
244
+ this.#onMessageHandler(this.#status.wsID, callback);
208
245
  }
209
246
 
210
- #onMessageHandler (callback) {
211
- if (!this.#ws) return callback(null);
247
+ #onMessageHandler (wsID, callback) {
248
+ if (!this.#status.ws) return callback(null);
212
249
 
213
250
  this.#ws.on("message", async (data) => {
251
+ if (wsID != this.#status.wsID) return;
252
+
214
253
  try {
215
254
  data = await JSON.parse(data);
216
255
  }
@@ -278,7 +317,7 @@ class chzzkChat {
278
317
  }
279
318
 
280
319
  #onDonationHandler (callback) {
281
- if (!this.#ws) return callback(null);
320
+ if (!this.#status.ws) return callback(null);
282
321
 
283
322
  this.#ws.on("message", async (data) => {
284
323
  try {
@@ -345,7 +384,7 @@ class chzzkChat {
345
384
  * @param {function} callback
346
385
  */
347
386
  onDisconnect(callback) {
348
- if (!this.#ws) return callback(null);
387
+ if (!this.#status.ws) return callback(null);
349
388
 
350
389
  this.#ws.on("close", () => {
351
390
  callback();
@@ -358,10 +397,10 @@ class chzzkChat {
358
397
  */
359
398
  getRecentChat(size) {
360
399
  return new Promise(async (resolve, reject) => {
361
- if (!this.#ws) return resolve(null);
400
+ if (!this.#status.ws) return resolve(null);
362
401
 
363
402
  let reqOpt = {
364
- "ver": "2",
403
+ "ver": "3",
365
404
  "cmd": 5101,
366
405
  "svcid": this.#svcid,
367
406
  "cid": this.#chatID,
@@ -466,17 +505,18 @@ class chzzkChat {
466
505
  */
467
506
  async disconnect() {
468
507
  return new Promise(async (resolve, reject) => {
469
- if (!this.#ws) return resolve(null);
508
+ if (!this.#status.ws) return resolve(null);
470
509
 
471
510
  await this.#ws.close();
472
511
  this.#ws = null;
512
+ this.#status.ws = false;
513
+ this.#status.wsID++;
473
514
  return resolve(true);
474
515
  });
475
516
  }
476
517
 
477
518
  async #polling () {
478
- //if (!this.#ws) return this.#pollingStatus = false;
479
- this.#pollingStatus = true;
519
+ if (!this.#status.polling) return;
480
520
 
481
521
  //Get ChatID
482
522
  let cidRes = await getStatus(this.channelID);
@@ -484,16 +524,9 @@ class chzzkChat {
484
524
  this.#chatID = cidRes.chatID;
485
525
 
486
526
  //Reconnect
487
- this.#pollingStatus = false;
527
+ this.#status.reconnect = true;
488
528
  await this.disconnect();
489
- let connectRes = await this.connect();
490
- if (connectRes) {
491
- for (let o in this.#callbacks) {
492
- if (this.#callbacks[o].type === "message") this.#onMessageHandler(this.#callbacks[o].callback);
493
- else if (this.#callbacks[o].type === "donation") this.#onDonationHandler(this.#callbacks[o].callback);
494
- }
495
- return;
496
- }
529
+ await this.connect();
497
530
  //Reconnect
498
531
  }
499
532
  //Get ChatID
@@ -507,18 +540,25 @@ class chzzkChat {
507
540
  }, interval);
508
541
  }
509
542
 
510
- async #ping () {
511
- //if (!this.#ws) return this.#pingStatus = false;
512
- this.#pingStatus = true;
543
+ async #ping (pingID) {
544
+ if (!this.#status.ws) return this.#status.ping = false;
545
+ if (!this.#status.ping || pingID != this.#status.pingID) return;
513
546
 
514
547
  let pingOpt = {
515
- "ver": "2",
548
+ "ver": "3",
516
549
  "cmd": 0
517
550
  };
518
- if (this.#ws && this.#ws.readyState == WebSocket.OPEN) this.#ws.send(JSON.stringify(pingOpt));
551
+ try {
552
+ await this.#ws.send(JSON.stringify(pingOpt));
553
+ }
554
+ catch (error) {
555
+ return setTimeout(async () => {
556
+ return this.#ping(pingID);
557
+ }, 20000);
558
+ }
519
559
 
520
560
  setTimeout(() => {
521
- return this.#ping();
561
+ return this.#ping(pingID);
522
562
  }, 20000);
523
563
  }
524
564
  }
package/lib/live.js CHANGED
@@ -83,7 +83,7 @@ async function getStatus (channelID) {
83
83
  return new Promise(async (resolve, reject) => {
84
84
 
85
85
  let res = await reqChzzk("polling/v2/channels/" + channelID + "/live-status");
86
- if (res.code != 200) return resolve(null);
86
+ if (res.code != 200 || !res.content) return resolve(null);
87
87
  res = res.content;
88
88
 
89
89
  try {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "buzzk",
3
3
  "displayName": "BUZZK",
4
- "version": "1.9.4",
4
+ "version": "1.9.6",
5
5
  "description": "뿌지직 (BUZZK) - 치지직(CHZZK) 챗봇을 더욱 쉽게 개발할 수 있도록 돕는 비공식 라이브러리.",
6
6
  "main": "lib/index.js",
7
7
  "type": "commonjs",