File size: 671 Bytes
aa2e6af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let token = '';

function updateTokenWebsocket(newToken) {
  console.log('Token:', newToken);
  token = newToken;
}

function sendTextToWebsocket(ws, onDataReceived) {
  if (token === '[DONE]') {
    ws.send(' ');
    return;
  }

  if (ws.readyState === WebSocket.OPEN) {
    ws.send(token);

    ws.onmessage = function (event) {
      console.log('Received:', event.data);
      if (onDataReceived) {
        onDataReceived(event.data); // Pass the received data to the callback function
      }
    };
  } else {
    console.error('WebSocket is not open. Ready state is: ' + ws.readyState);
  }
}

module.exports = {
  updateTokenWebsocket,
  sendTextToWebsocket,
};