'running websocket in google sheet without using HTML code

I find this test for Binance websocket in app script the code in this URL:

running websocket in google sheet

I want to remove code HTML and run websocket without using HTML code.

I want just use:

  

function onOpen(e) {
  SpreadsheetApp.getUi()
    .createMenu('BINANCE')
    .addItem('RUN', 'runwebsocket')
    .addToUi();
  
}

to run websocket

any suggest for the problem .



Solution 1:[1]

One solution is to embed the html javascript in a blank page

index.html

<!DOCTYPE html>
<meta charset="utf-8" />

<head>
  <script language="javascript" type="text/javascript">
    var wsUri = ("wss://stream.binance.com:9443/ws");
  <? var myparam = param(); ?>
  
  function init()  {
    websocket = new WebSocket(wsUri);
    websocket.onopen = function(evt) { onOpen(evt) };
    websocket.onclose = function(evt) { onClose(evt) };
    websocket.onmessage = function(evt) { onMessage(evt) };
    websocket.onerror = function(evt) { onError(evt) };
    nbr=0;
  }

  function onOpen(evt)  {
    doSend(<?= myparam ?>);
  }

  function onClose(evt)  {
  }

  function onMessage(evt)  {
    google.script.run.getJSON( evt.data );
  }

  function onError(evt)  {
  }

  function doSend(message)  {
    websocket.send(message);
  }

  window.addEventListener("load", init, false);
  window.addEventListener("beforeunload", websocket.close, false);
  
  </script>
</head>

<body></body>

</html>

you can lunch this way

function modal() {
  var html = HtmlService
    .createTemplateFromFile("index")
    .evaluate()
    .setWidth(100)
    .setHeight(50);
  SpreadsheetApp.getUi().showModelessDialog(html, 'web socket');
}

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1