var socket = null; var serial; var socketConnected = false; var connectionId = null; function connectToWebSocket() { var webSocketConnection = 'wss://1oghcsklgi.execute-api.us-east-1.amazonaws.com/production/'; serial = uuidv4(); socket = new WebSocket(webSocketConnection); socket.onopen = function (event) { socketConnected = true; var payload = { "msgType": "ack", "serial": serial }; socket.send(JSON.stringify(payload)); }; socket.onmessage = function (event) { try { var m = JSON.parse(event.data); var b = JSON.parse(m.body); if(b.msgType == 'ack') { connectionId = b.connection; //console.log(connectionId); } if(b.msgType == 'chunk' && b.chunk.finish_reason==null && socketConnected==true) { document.getElementById('formStreamPane').innerHTML = document.getElementById('formStreamPane').innerHTML + b.chunk.content; } } catch (e) { } }; socket.onerror = function (event) { console.error("WebSocket error observed:", event); socketConnected = false; }; socket.onclose = function (event) { connectionId = null; socketConnected = false; }; } function disconnectSocket() { socket.close(); } function uuidv4() { return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16) ); }