-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.ts
52 lines (42 loc) · 1.43 KB
/
client.ts
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
52
// TODO: add types!
const { randomBytes } = await import("node:crypto");
const ws: WebSocket = new WebSocket("ws://localhost:8000");
const generateId = (): string => {
return randomBytes(5).toString("hex").slice(0, 5);
};
const id: string = generateId();
// Prompt the user for input and send the message to the server
const promptUserForMessage = (): void => {
const userMessage = (prompt("Enter a message: ") || "").trim(); // Handle null from prompt
if (userMessage) { // Only send non-empty messages
const input = message(id, userMessage);
ws.send(input);
console.log(`Client: ${input}`);
} else {
console.log("Message was empty, not sent.");
promptUserForMessage();
}
};
const message = (id: string, content: string): string => {
const data = { id, message: content };
return JSON.stringify(data);
};
ws.onopen = (): void => {
const data: {id: string, message: string} = { id, message: "Hello"};
console.log("Connected to WebSocket server");
const msg: string = JSON.stringify(data);
ws.send(msg);
console.log(`Client: ${msg}`);
promptUserForMessage();
};
// TODO: make this client interactive
ws.onmessage = (event: MessageEvent): void => {
console.log("Server:", event.data); // Log the response
promptUserForMessage();
};
ws.onclose = (): void => {
console.log("Disconnected from WebSocket server");
};
ws.onerror = (error: Event): void => {
console.error("WebSocket error:", error);
};