SHARE
TWEET

websocket-example1

selnomeria Nov 4th, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. function initWebsocket(symbol, res)
  3. {
  4.     var symbol = symbol.toLowerCase();
  5.     var msgPrefix= "!!! SOCKET !!!";
  6.     var conn = new WebSocket("wss://stream.binance.com:9443/ws/btcusdt@kline_"+res);
  7.    
  8.     conn.onopen = function(evt) {
  9.       console.log(msgPrefix+ "[open] Connection established");
  10.     };
  11.  
  12.     conn.onmessage = function(event) {
  13.       console.log(msgPrefix+ `[message] Data received from server: ${event.data}`);
  14.     };
  15.  
  16.     conn.onclose = function(event) {
  17.         console.log(msgPrefix+  "CLOSED");
  18.       if (event.wasClean) {
  19.         console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
  20.       } else {
  21.         // e.g. server process killed or network down .. event.code is usually 1006 in this case
  22.         console.log('[close] Connection died');
  23.       }
  24.     };
  25.  
  26.     conn.onerror = function(error) {
  27.       console.log(msgPrefix+  `[error] ${error.message}`);
  28.     };
  29.    
  30.     window.onbeforeunload = function() {
  31.         conn.onclose = function () {}; // disable onclose handler first
  32.         conn.close();
  33.     };
  34.     return conn;
  35. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top