Skip to content

Commit

Permalink
feat: update message handling in ChatSession to include user informat…
Browse files Browse the repository at this point in the history
…ion in callbacks
  • Loading branch information
milyin committed Dec 12, 2024
1 parent 79f8381 commit 2dc25bd
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions zenoh-ts/examples/chat/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ChatSession {
message_subscriber: Subscriber | null = null;

usersCallback: (() => void) | null = null;
messageCallback: ((message: string) => void) | null = null;
messageCallback: ((user: ChatUser, message: string) => void) | null = null;

user: ChatUser;
users: ChatUser[] = [];
Expand Down Expand Up @@ -73,12 +73,17 @@ class ChatSession {

this.messages_publisher = this.session.declare_publisher(keyexpr, {});

this.message_subscriber = await this.session.declare_subscriber(keyexpr, (sample) => {
this.message_subscriber = await this.session.declare_subscriber("user/*", (sample) => {
let message = deserialize_string(sample.payload().buffer());
log(`[Subscriber] Received message: ${message} from ${sample.keyexpr().toString()}`);
if (this.messageCallback) {
this.messageCallback(message);
}
let user = ChatUser.fromKeyexpr(sample.keyexpr());
if (user) {
if (this.messageCallback) {
this.messageCallback(user, message);
}
} else {
log(`Invalid user keyexpr: ${sample.keyexpr().toString()}`);
};
return Promise.resolve();
});

Expand Down Expand Up @@ -122,7 +127,7 @@ class ChatSession {
this.usersCallback = callback;
}

onNewMessage(callback: (message: string) => void) {
onNewMessage(callback: (user: ChatUser, message: string) => void) {
this.messageCallback = callback;
}

Expand Down Expand Up @@ -204,9 +209,9 @@ document.addEventListener('DOMContentLoaded', () => {
usersList.appendChild(li);
});
});
chatSession.onNewMessage((message) => {
chatSession.onNewMessage((user, message) => {
const messageElement = document.createElement('div');
messageElement.textContent = message;
messageElement.textContent = `${user.toString()}: ${message}`;
chatLog.appendChild(messageElement);
chatLog.scrollTop = chatLog.scrollHeight; // Scroll to the latest message
});
Expand Down

0 comments on commit 2dc25bd

Please sign in to comment.