-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.js
49 lines (43 loc) · 1.33 KB
/
websocket.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
47
48
49
#!/usr/bin/env node
var WebSocketClient = require('websocket').client;
const axios = require('axios')
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);
connection.sendUTF(number.toString());
setTimeout(sendNumber, 1000);
}
}
connection.sendUTF(JSON.stringify({id: 1, type:'subscribe', event:'transactions'}))
//sendNumber();
});
axios
.post('https://krist.ceriat.net/ws/start', {
//todo: 'Buy the milk'
})
.then(res => {
console.log(`statusCode: ${res.statusCode}`)
console.log(res)
client.connect(res.data.url, 'echo-protocol');
})
.catch(error => {
console.error(error)
})
//