node-nim
Version:
NetEase IM nodejs wrapper based on NetEase IM C++ SDK
259 lines • 10.6 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NIMTalk = void 0;
const loader_1 = __importDefault(require("../loader"));
const eventemitter3_1 = require("eventemitter3");
const msglog_def_1 = require("../nim_def/msglog_def");
class NIMTalk extends eventemitter3_1.EventEmitter {
constructor() {
super();
this.talk = new loader_1.default.NIMTalk({ emit: this.emit.bind(this) });
}
/** 注册全局回调 */
initEventHandlers() {
return this.talk.InitEventHandlers();
}
/** 发送消息
* @param json_msg 消息体Json字符串,可以通过CreateXXXMessage方法自动创建
* @param jsonExtension json扩展参数(备用,目前不需要)
* @param pcb 上传进度的回调函数, 如果发送的消息里包含了文件资源,则通过此回调函数通知上传进度
* @return void 无返回值
*/
sendMsg(msg, jsonExtension, progressCb) {
return this.talk.SendMsg(msg, jsonExtension, progressCb);
}
/**
* 携带配置参数发送消息
* @param msg 消息体
* @param option 配置参数
* @param progressCb 上传进度的回调函数
* @return void 无返回值
*/
sendMessageWithOption(msg, option, progressCb) {
return this.talk.SendMessageWithOption(msg, option, progressCb);
}
/** 停止正在发送中的消息(目前只支持发送文件消息时的终止)
* @param client_msg_id 停止发送的消息客户端id
* @param type 停止发送的消息类型
* @param jsonExtension json扩展参数(备用,目前不需要)
* @return void 无返回值
*/
stopSendMsg(clientMsgId, type, jsonExtension) {
return this.talk.StopSendMsg(clientMsgId, type, jsonExtension);
}
/** 撤回消息
* @param msg 消息
* @param notify_msg 自定义通知消息
* @param cb 回调
* @param param 额外的参数,包含apnstext、pushpayload、jsonExtension、env_config、user_data
* @note
* <pre>
* 200:成功
* 414:参数错误
* 508:撤回时间超过配制有效期,默认是2分钟
* 10414:本地错误码,参数错误
* 10508:本地错误码,超过配置有效期或者所需参数不存在
* </pre>
*/
recallMsg(msg, notify_msg, cb, apnstext, pushpayloadconst, jsonExtension) {
return new Promise((resolve) => {
this.talk.RecallMsg(msg, notify_msg, (rescode, result) => {
if (cb) {
cb(rescode, result);
}
resolve([rescode, result]);
}, apnstext, pushpayloadconst, jsonExtension);
});
}
/**
* @brief 回复消息 thread 聊天场景,携带指定配置
* @param formerMsg 被回复消息的消息体
* @param replyMsg 回复消息的消息体,可通过各种 createxxxmessage 接口创建
* @param option 配置参数
* @param progressCb 传进度的回调函数, 如果发送的消息里包含了文件资源,则通过此回调函数通知上传进度
* @return void 无返回值
*/
replyMessageWithOption(formerMsg, replyMsg, option, progressCb) {
return this.talk.ReplyMessageWithOption(formerMsg, replyMsg, option, progressCb);
}
/** 创建文本消息 */
createTextMessage(receiver_id, session_type, client_msg_id, content, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
content_: content,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeText
};
}
/** 创建文件消息 */
createFileMessage(receiver_id, session_type, client_msg_id, file, file_path, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
attach_: JSON.stringify(file),
local_res_path_: file_path,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeFile
};
}
/** 创建图片消息 */
createImageMessage(receiver_id, session_type, client_msg_id, image, file_path, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
attach_: JSON.stringify(image),
local_res_path_: file_path,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeImage
};
}
/** 创建语音消息 */
createAudioMessage(receiver_id, session_type, client_msg_id, audio, file_path, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
attach_: JSON.stringify(audio),
local_res_path_: file_path,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeAudio
};
}
/** 创建视频消息 */
createVideoMessage(receiver_id, session_type, client_msg_id, video, file_path, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
attach_: JSON.stringify(video),
local_res_path_: file_path,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeVideo
};
}
/** 创建位置消息 */
createLocationMessage(receiver_id, session_type, client_msg_id, location, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
attach_: JSON.stringify(location),
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeLocation
};
}
/** 创建提醒消息 */
createTipMessage(receiver_id, session_type, client_msg_id, tip, msg_setting, timetag, sub_type) {
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
content_: tip,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sub_type,
type_: msglog_def_1.NIMMessageType.kNIMMessageTypeTips
};
}
/** 创建转发消息 */
createRetweetMessage(sourceMessage, client_msg_id, session_type, receiver_id, msg_setting, timetag) {
let attachStr = '';
if (sourceMessage.attach_) {
let attach = JSON.parse(sourceMessage.attach_);
attach['retweeted_msg_id'] = sourceMessage.client_msg_id_;
attachStr = JSON.stringify(attach);
}
return {
receiver_accid_: receiver_id,
session_type_: session_type,
client_msg_id_: client_msg_id,
content_: sourceMessage.content_,
msg_setting_: msg_setting,
timetag_: timetag,
sub_type_: sourceMessage.sub_type_,
type_: sourceMessage.type_,
attach_: attachStr
};
}
/** 从消息体中获取附件(图片、语音、视频等)的本地路径
* @param msg 消息
* @return string 消息如果有附件,不管是否已下载,返回附件的本地路径;消息如果没有附件,返回空字符串。
*/
getAttachmentPathFromMsg(msg) {
return this.talk.GetAttachmentPathFromMsg(msg);
}
/** 回复消息thread 聊天场景
* @param formerMsg 被回复消息的消息体
* @param replyMsg 回复消息的消息体,可通过各种createxxxmessage接口创建
* @param progressCb 传进度的回调函数, 如果发送的消息里包含了文件资源,则通过此回调函数通知上传进度
* @return void 无返回值
*/
replyMessage(formerMsg, replyMsg, progressCb) {
return this.talk.ReplyMessage(formerMsg, replyMsg, progressCb);
}
/** (全局回调)注册消息过滤接口 (堵塞线程,谨慎使用,避免耗时行为)
* @param[in] filter 过滤接口
* @param[in] json_extension json扩展参数(备用,目前不需要)
* @return void 无返回值
*/
regMessageFilter(cb, jsonExtension) {
return this.talk.RegMessageFilter(cb, jsonExtension);
}
/** (全局回调)注册群通知过滤接口 (堵塞线程,谨慎使用,避免耗时行为)
* @param[in] json_extension json扩展参数(备用,目前不需要)
* @param[in] filter 过滤接口
* @return void 无返回值
*/
regTeamNotificationFilter(cb, jsonExtension) {
return this.talk.RegTeamNotificationFilter(cb, jsonExtension);
}
/**
* @brief 停止流式消息输出
* @param message 要停止的消息体
* @param parameter 停止参数,@see StopStreamingMessageParam
* @return Promise<NIMResCode>
* @since v10.8.30
*/
stopStreamingMessage(message, parameter) {
return new Promise((resolve) => {
this.talk.StopStreamingMessage(message, parameter, (resultCode) => {
resolve(resultCode);
});
});
}
/**
* @brief 重发流式消息
* @param message 要重发的消息体
* @param parameter 重发参数,@see RedoAIMessageParam
* @return Promise<NIMResCode>
* @since v10.8.30
*/
redoAIMessage(message, parameter) {
return new Promise((resolve) => {
this.talk.RedoAIMessage(message, parameter, (resultCode) => {
resolve(resultCode);
});
});
}
}
exports.NIMTalk = NIMTalk;
//# sourceMappingURL=talk.js.map
;