-
Notifications
You must be signed in to change notification settings - Fork 2
/
exampleClient.js
36 lines (31 loc) · 1.03 KB
/
exampleClient.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
#!/usr/bin/env node
var WebSocketClient = require("websocket").client;
var client = new WebSocketClient();
client.on("connectFailed", function (error) {
console.log("Connect Error: " + error.toString());
});
client.on("connect", function (connection) {
console.log("WebSocket Client Connected");
connection.on("error", function (error) {
console.log("Connection Error: " + error.toString());
});
connection.on("close", function () {
console.log("echo-protocol Connection Closed");
});
connection.on("message", function (message) {
if (message.type === "utf8") {
console.log("Received: '" + message.utf8Data + "'");
}
});
function sendNumber() {
if (connection.connected) {
var number = Math.round(Math.random() * 0xffffff);
console.log(`sending number: ${number}`);
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}
// sendNumber();
});
// client.connect('ws://159.122.217.91/ws', 'echo-protocol');
client.connect("ws://localhost:8080", "echo-protocol");