chzzk-z
Version:
chzzk-z is naver streaming platform Chzzk Library
182 lines • 5.78 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChzzkChat = void 0;
const ws_1 = require("ws");
const chat_types_1 = require("./types/chat.types");
class ChzzkChat {
constructor(cm, opt) {
this.messages = [];
this.cm = cm;
this.opt = opt;
}
async join(channelId) {
const status = await this.cm.live.findStatusByChannelId(channelId);
const token = await this.cm.channel.findAccessTokenById(status.chatChannelId);
this.opt.accessToken = token.accessToken;
this.opt.chatChannelId = status.chatChannelId;
this.connect();
}
async quit() {
this.disconnect();
}
async connect() {
const ssID = Math.floor(Math.random() * 10) + 1;
this.ws = new ws_1.WebSocket(`wss://kr-ss${ssID}.chat.naver.com/chat`);
this.openHandler();
this.errorHandler();
this.closeHandler();
this.messageHandler();
this.connectHandler();
}
openHandler() {
let body = {
accTkn: this.opt.accessToken,
auth: "READ",
devType: 2001,
};
if (this.opt.userId) {
body["uid"] = this.opt.userId;
body["auth"] = "SEND";
}
this.ws.on("open", () => {
this.sendMessage({
cmd: chat_types_1.MsgCmd.CONNECT,
cid: this.opt.chatChannelId,
svcid: "game",
ver: "2",
tid: 1,
bdy: body,
});
this._connected = true;
});
}
errorHandler() {
this.ws.on("error", console.error);
}
closeHandler() {
this.ws.on("close", () => {
console.log("disconeected");
this.disconnect();
});
}
messageHandler() {
this.ws.on("message", async (data) => {
data = JSON.parse(data.toString());
this.handleMessage(data);
});
}
connectHandler() {
this.pingIntervalId = setInterval(() => {
this.sendMessage({
cmd: chat_types_1.MsgCmd.PING,
ver: "2",
});
}, 10000);
}
async handleMessage(data) {
try {
let message, body;
const messageType = data["cmd"];
switch (messageType) {
case chat_types_1.MsgCmd.PING:
this.sendMessage({
cmd: chat_types_1.MsgCmd.PONG,
ver: "2",
});
break;
case chat_types_1.MsgCmd.CONNECTED:
this.opt.sid = data["bdy"]?.sid;
this.ws.emit("connect", null);
break;
case chat_types_1.MsgCmd.BLIND:
console.log(data);
body = data["bdy"];
message = {
type: chat_types_1.MsgCmd[messageType],
cid: data.cid,
ctime: body["messageTime"],
uid: body["userId"],
bdy: body,
};
this.messages.push(message);
break;
case chat_types_1.MsgCmd.CHAT:
case chat_types_1.MsgCmd.DONATION:
body = data["bdy"][0];
const profile = JSON.parse(body["profile"]);
const extras = JSON.parse(body["extras"]);
message = {
type: chat_types_1.MsgCmd[messageType],
cid: body["cid"],
ctime: body["ctime"],
msg: body["msg"],
uid: profile?.userIdHash,
profile: profile,
extras: extras,
};
this.messages.push(message);
break;
}
}
catch (e) {
console.log(data);
throw new Error(e);
}
}
pollingEvent() {
const messages = this.messages;
this.messages = [];
return messages;
}
async onEvent(messageType) { }
async disconnect() {
if (this.pingIntervalId) {
clearInterval(this.pingIntervalId);
}
if (this.ws.readyState === ws_1.WebSocket.OPEN) {
this.ws.close();
}
else {
console.log("disconnected");
}
this._connected = false;
}
sendMessage(sendMessageData) {
this.ws.send(JSON.stringify(sendMessageData));
}
chat(message) {
if (!this.cm.user.loggedIn) {
throw new Error("You must be logged in.");
}
if (!this.connected) {
throw new Error("You must be connected to one of channel.");
}
this.sendMessage({
cid: this.opt.chatChannelId,
svcid: "game",
ver: "2",
bdy: {
extras: JSON.stringify({
chatType: "STREAMING",
emojis: "",
osType: "PC",
streamingChannelId: this.opt.chatChannelId,
}),
msg: message,
msgTime: Date.now(),
msgTypeCode: chat_types_1.MsgTypeCode.TEXT,
},
retry: false,
cmd: chat_types_1.MsgCmd.SEND_CHAT,
sid: this.opt.sid,
tid: 3,
});
const extras = {};
this.ws.send(JSON.stringify({}));
}
get connected() {
return this._connected;
}
}
exports.ChzzkChat = ChzzkChat;
//# sourceMappingURL=chat.js.map