forked from cloudevents/sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
46 lines (39 loc) · 1.22 KB
/
client.js
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
/* eslint-disable no-console */
const readline = require("readline");
const WebSocket = require("ws");
const ws = new WebSocket("ws://localhost:8080");
const { CloudEvent } = require("cloudevents");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("close", (_) => console.log("\n\nConnection closed! Press CTL-C to exit."));
ws.on("message", function incoming(message) {
const event = new CloudEvent(JSON.parse(message));
if (event.type === "weather.error") {
console.error(`Error: ${event.data}`);
} else {
print(event.data);
}
ask();
});
function ask() {
rl.question("Would you like to see the current weather? Provide a zip code: ", function (zip) {
console.log("Fetching weather data from server...");
const event = new CloudEvent({
type: "weather.query",
source: "/weather.client",
data: { zip },
});
ws.send(event.toString());
});
}
function print(data) {
console.log(`
Current weather for ${data.name}: ${data.weather[0].main}
------------------------------------------
With ${data.weather[0].description}, the temperature is ${Math.round(data.main.temp)}F
and the wind is blowing at ${Math.round(data.wind.speed)}mph.
`);
}
ask();