- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
Latest commit
executable file
·129 lines (112 loc) · 3.23 KB
/
Copy pathcli.js
File metadata and controls
executable file
·129 lines (112 loc) · 3.23 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const{ Command }=require("commander");
const{ Client }=require("@stomp/stompjs");
constreadline=require("readline");
const{ WebSocket }=require("ws");
Object.assign(global,{ WebSocket });
constprogram=newCommand();
program
.version("0.0.1")
.description("Interactive CLI for STOMP server")
.argument("<brokerURL>","STOMP server host (ws:// or wss://)");
program.parse(process.argv);
constbrokerURL=program.args[0];
constclient=newClient({
brokerURL: brokerURL,
onConnect: ()=>{
console.log("Connected to the STOMP server.",brokerURL);
showPrompt();
},
onStompError: (frame)=>{
console.error("Broker error: ",frame.headers["message"]);
console.error("Additional details: ",frame.body);
},
onWebSocketClose: ()=>{
console.log("WebSocket is closed.");
},
});
client.activate();
constrl=readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: "stompjs-cli> ",
});
letsubscriptions={};
functionshowPrompt(){
console.log(`
Available commands:
sub <destination> - Subscribe to a destination, example 'sub /topic/bar'
pub <destination> <message> - Publish a message to a destination, example 'pub /topic/bar hello'
list - List current subscriptions
exit - Exit the CLI
`);
rl.prompt();
}
rl.on("line",(line)=>{
constinput=line.trim().split(" ");
constcommand=input[0];
switch(command){
case"sub": {
constdestination=input[1];
if(!destination){
console.log("Error: Missing destination.");
break;
}
if(subscriptions[destination]){
console.log(`Already subscribed to ${destination}.`);
break;
}
subscriptions[destination]=client.subscribe(destination,(message)=>{
console.log(`Received on ${destination}:`,message.body);
rl.prompt();
});
console.log(`Subscribed to ${destination}.`);
break;
}
case"pub": {
constdestination=input[1];
constmessage=input.slice(2).join(" ");
if(!destination){
console.log("Error: Missing destination");
break;
}
if(!message){
console.log("Error: Missing message.");
break;
}
try{
letmessageBody;
try{
messageBody=JSON.stringify(JSON.parse(message));
}catch(error){
messageBody=message;
}
client.publish({destination: destination,body: messageBody});
console.log(`Published to ${destination}: ${messageBody}`);
}catch(error){
console.log("Error: Invalid JSON format.");
}
break;
}
case"list": {
console.log("Current subscriptions:");
Object.keys(subscriptions).forEach((destination)=>{
console.log(` - ${destination}`);
});
break;
}
case"exit": {
console.log("Exiting...");
Object.values(subscriptions).forEach((sub)=>sub.unsubscribe());
client.deactivate();
rl.close();
break;
}
default:
console.log("Unknown command:",command);
break;
}
rl.prompt();
}).on("close",()=>{
console.log("CLI closed.");
process.exit(0);
});