Connection will be closed after 1 minute
'); 10 | closeTimeout = window.setTimeout(function () { 11 | ws.close(); 12 | }, 120 * 1000); 13 | }, 60 * 1000); 14 | } 15 | 16 | function socket() { 17 | //Determine whether the current browser supports WebSocket 18 | if ('WebSocket' in window) { 19 | const ws = new WebSocket("ws://" + serverAddress + ":" + serverPort + "/customer/" + id); 20 | let before = null; 21 | //Connect failed callback 22 | ws.onerror = function () { 23 | $('#coco-dialog').append('Connection error
'); 24 | }; 25 | //Connect succeed callback 26 | ws.onopen = function () { 27 | $('#coco-dialog').append('Connection succeeded
'); 28 | handleTimeout(ws); 29 | }; 30 | //Receive message callback 31 | ws.onmessage = function (event) { 32 | let json = JSON.parse(event.data); 33 | /*if (json.message != null) { 34 | alert(json.message) 35 | }*/ 36 | if (before == null) { 37 | before = json.customerInQueue; 38 | } 39 | let dialog = $('#coco-dialog'); 40 | let waitingBeforeYouObj = $("#waitingBeforeYou"); 41 | switch (json.type) { 42 | case "MESSAGE": 43 | dialog.append(''
44 | + json.serviceName + ' ' + getTime() + '
'
45 | + json.message + '
' + 'Connected service' + '
') 54 | .append('' + json.serviceName + ' ' + getTime() + '
'
55 | + json.message + '
' + json.message + '
'); 64 | ws.close(); 65 | break; 66 | } 67 | let scrollHeight = dialog.prop('scrollHeight'); 68 | dialog.scrollTop(scrollHeight); 69 | }; 70 | //Connect closed callback 71 | ws.onclose = function () { 72 | $('#coco-dialog').append('Connection closed
'); 73 | }; 74 | //Close the websocket connection when the window is closed, preventing throwing exceptions. 75 | window.onbeforeunload = function () { 76 | ws.close(); 77 | }; 78 | //Send message 79 | $('#send').click(function () { 80 | let messageObj = $('#message'); 81 | let messageVal = messageObj.val(); 82 | let dialog = $('#coco-dialog'); 83 | if (messageVal !== '') { 84 | ws.send(messageVal); 85 | dialog.append('' + getTime() + '
' + messageVal + '
Connection error
'); 14 | }; 15 | //Connect succeed callback 16 | ws.onopen = function () { 17 | //$('.coco-dialog').append('Connection succeeded
'); 18 | }; 19 | //Receive message callback 20 | ws.onmessage = function (event) { 21 | let json = JSON.parse(event.data); 22 | if (waiting == null) { 23 | waiting = json.customerInQueue; 24 | } 25 | let cocoDialogObject = $("#" + json.customerId).children(".coco-dialog"); 26 | let customerId = json.customerId; 27 | let hrefCustomerId = $("[href='#" + customerId + "']"); 28 | let waitingObj = $("#waiting"); 29 | switch (json.type) { 30 | case "MESSAGE": 31 | cocoDialogObject.append('' + getTime() + '
' + json.message + '
' + 'Connected service' + '
'); 66 | resize(); 67 | if (waiting !== 0) { 68 | waitingObj.text(--waiting); 69 | } 70 | $("#list-tab").on("click", "[href='#" + customerId + "']", function () { 71 | $(".tab-pane").removeClass("active"); 72 | $("#" + customerId).addClass("active"); 73 | $("[href='#" + customerId + "']").children(".badge") 74 | .removeClass("badge-danger").addClass("badge-warning").text(""); 75 | $("#message_" + customerId).focus(); 76 | }); 77 | $(".tab-pane").on("click", ".send", function () { 78 | let customerId = ($(this).attr("id").split("_"))[1]; 79 | let messageObj = $(this).parent().prev(".message"); 80 | if (messageObj.val() !== '') { 81 | let msg = {"customerId": customerId, "message": messageObj.val()}; 82 | ws.send(JSON.stringify(msg)); 83 | $("#" + customerId).children('.coco-dialog').append('' + getTime() + '
' + messageObj.val() + '
The service time: 12m
\n' + 105 | ' Close dialog\n' + 106 | 'Connection closed
'); 123 | }; 124 | //Close the websocket connection when the window is closed, preventing throwing exceptions. 125 | window.onbeforeunload = function () { 126 | ws.close(); 127 | }; 128 | //Send message 129 | $('.send').click(function () { 130 | let dialog = $('.coco-dialog'); 131 | let customerId = ($(this).attr("id").split("_"))[1]; 132 | // alert(customerId) 133 | let message = $(this).parent().prev(".message"); 134 | if (message !== '') { 135 | let msg = {"customerId": customerId, "message": message.val()}; 136 | ws.send(JSON.stringify(msg)); 137 | $("#" + customerId).children('.coco-dialog').append('' + getTime() + '
' + message + '