Adding ability to listen to user types chat messages using WA.onChatMessage
This commit is contained in:
parent
5178dff108
commit
e927e0fa16
7 changed files with 82 additions and 10 deletions
|
@ -1,4 +1,7 @@
|
|||
import {ChatEvent} from "./Api/Events/ChatEvent";
|
||||
import {ChatEvent, isChatEvent} from "./Api/Events/ChatEvent";
|
||||
import {isIframeEventWrapper} from "./Api/Events/IframeEvent";
|
||||
import {isUserInputChatEvent, UserInputChatEvent} from "./Api/Events/UserInputChatEvent";
|
||||
import {Subject} from "rxjs";
|
||||
|
||||
interface WorkAdventureApi {
|
||||
sendChatMessage(message: string, author: string): void;
|
||||
|
@ -10,6 +13,11 @@ declare global {
|
|||
var WA: WorkAdventureApi
|
||||
}
|
||||
|
||||
type ChatMessageCallback = (message: string) => void;
|
||||
|
||||
const userInputChatStream: Subject<UserInputChatEvent> = new Subject();
|
||||
|
||||
|
||||
window.WA = {
|
||||
/**
|
||||
* Send a message in the chat.
|
||||
|
@ -27,7 +35,25 @@ window.WA = {
|
|||
/**
|
||||
* Listen to messages sent by the local user, in the chat.
|
||||
*/
|
||||
onChatMessage(callback: (message: string) => void): void {
|
||||
|
||||
onChatMessage(callback: ChatMessageCallback): void {
|
||||
userInputChatStream.subscribe((userInputChatEvent) => {
|
||||
callback(userInputChatEvent.message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('message', message => {
|
||||
if (message.source !== window.parent) {
|
||||
console.log('MESSAGE SKIPPED!!!')
|
||||
return; // Skip message in this event listener
|
||||
}
|
||||
|
||||
const payload = message.data;
|
||||
if (isIframeEventWrapper(payload)) {
|
||||
if (payload.type === 'userInputChat' && isUserInputChatEvent(payload.data)) {
|
||||
userInputChatStream.next(payload.data);
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue