Commit 1cda55d0 by Sixong.Zhu

notify

parent 4a06dee9
Showing with 24 additions and 2 deletions
...@@ -38,6 +38,10 @@ const chatType = "group"; ...@@ -38,6 +38,10 @@ const chatType = "group";
export class Xim { export class Xim {
private eventBus = new Vue(); private eventBus = new Vue();
private readonly messageNotifyActions: {
key: string;
action: (m: Message) => void;
}[] = [];
private client?: XChatClient; private client?: XChatClient;
...@@ -420,6 +424,17 @@ export class Xim { ...@@ -420,6 +424,17 @@ export class Xim {
private handleMsg(kind: any, msg: any) { private handleMsg(kind: any, msg: any) {
this.debug(`收到消息 ${new Date().getTime()}`, kind, msg); this.debug(`收到消息 ${new Date().getTime()}`, kind, msg);
if (
(kind === "chat" || kind === "chat_notify") &&
msg &&
msg.msg_type !== "read" &&
msg.msg_type !== "user.input"
) {
for (const item of this.messageNotifyActions) {
item.action(msg);
}
}
switch (kind) { switch (kind) {
case "chat": case "chat":
this.emit(`msg`, msg); this.emit(`msg`, msg);
...@@ -453,8 +468,15 @@ export class Xim { ...@@ -453,8 +468,15 @@ export class Xim {
} }
public registerOnMessage(vue: Vue, action: (e: Message) => void) { public registerOnMessage(vue: Vue, action: (e: Message) => void) {
this.on("msg", action); const key = `${new Date().valueOf()}-${Math.random()}`;
vue.$once("hook:beforeDestroy", () => this.off("msg", action)); this.messageNotifyActions.push({ key, action });
vue.$once("hook:beforeDestroy", () => {
const t = this.messageNotifyActions.find((i) => i.key === key);
if (t) {
const index = this.messageNotifyActions.indexOf(t);
this.messageNotifyActions.splice(index, 1);
}
});
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment