-
Notifications
You must be signed in to change notification settings - Fork 1
/
marketcapws.js
38 lines (32 loc) · 918 Bytes
/
marketcapws.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
var websocket;
function createWebSocketConnection() {
connect("wss://stream.coinmarketcap.com/price/latest");
}
//Make a websocket connection with the server.
function connect(host) {
if (websocket === undefined) {
// port.postMessage("open");
websocket = new WebSocket(host);
}
websocket.onopen = function () {
websocket.send(
'{"method":"subscribe","id":"price","data":{"cryptoIds":[1,1027,1839,52,5994],"index":null}}'
);
};
websocket.onmessage = function (event) {
console.log(event.data);
};
//If the websocket is closed but the session is still active, create new connection again
websocket.onclose = function () {
websocket.send(
'{"method":"unsubscribe","id":"unsubscribePrice"}'
)
websocket = undefined;
};
}
function close(){
websocket.send(
'{"method":"unsubscribe","id":"unsubscribePrice"}'
)
}
// createWebSocketConnection();