-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatService.js
More file actions
51 lines (46 loc) · 1.75 KB
/
chatService.js
File metadata and controls
51 lines (46 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// MockChatService class (re-defined for React Native context)
// This class simulates the backend chat service.
// It immediately sends a reply after receiving a message.
class MockChatService {
constructor(onNewMessageCallback) {
this.onNewMessageCallback = onNewMessageCallback;
this.isConnected = false;
console.log("MockChatService: Initialized.");
}
// Simulates connecting to the chat service.
connect() {
return new Promise(resolve => {
setTimeout(() => {
this.isConnected = true;
console.log("MockChatService: Connected.");
this.onNewMessageCallback({ sender: 'System', text: 'Welcome to the chat! Type a message to start.' });
resolve(true);
}, 500); // Simulate network delay
});
}
// Simulates sending a message to the service.
sendMessage(messageText) {
if (!this.isConnected) {
console.error("MockChatService: Not connected. Cannot send message.");
return;
}
console.log(`MockChatService: Received message: "${messageText}"`);
// Simulate an immediate reply from the service.
// In a real scenario, this would involve an API call and a response.
setTimeout(() => {
const replyText = `Echo: "${messageText}" (Received at ${new Date().toLocaleTimeString()})`;
this.onNewMessageCallback({ sender: 'Service', text: replyText });
console.log(`MockChatService: Sent reply: "${replyText}"`);
}, 300); // Small delay to simulate processing
}
// Simulates disconnecting from the chat service.
disconnect() {
return new Promise(resolve => {
setTimeout(() => {
this.isConnected = false;
console.log("MockChatService: Disconnected.");
resolve(true);
}, 300); // Simulate network delay
});
}
}