Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2.         // This object will be sent everytime you submit a message in the sendMessage function.
  3.         var clientInformation = {
  4.             username: "{{ doctor }}",
  5.             token: "{{ token }}"
  6.             // You can add more information in a static object
  7.         };
  8.  
  9.         // START SOCKET CONFIG
  10.         /**
  11.          * Note that you need to change the "sandbox" for the URL of your project.
  12.          * According to the configuration in Sockets/Chat.php , change the port if you need to.
  13.          * @type WebSocket
  14.          */
  15.         var conn = new WebSocket('ws://localhost:8081');
  16.  
  17.         conn.onopen = function (e) {
  18.             console.info("Connection established succesfully");
  19.         };
  20.  
  21.         conn.onmessage = function (e) {
  22.             var data = JSON.parse(e.data);
  23.  
  24.             //Check token
  25.             if (data.token == 'dsdsa') {
  26.                 Chat.appendMessage(data.username, data.message);
  27.             }
  28.  
  29.             console.log(data.token);
  30.         };
  31.  
  32.         conn.onerror = function (e) {
  33.             alert("Error: something went wrong with the socket.");
  34.             console.error(e);
  35.         };
  36.         // END SOCKET CONFIG
  37.  
  38.  
  39.         /// Some code to add the messages to the list element and the message submit.
  40.         document.getElementById("form-submit").addEventListener("click", function () {
  41.             var msg = document.getElementById("form-message").value;
  42.  
  43.             if (!msg) {
  44.                 alert("Please send something on the chat");
  45.             }
  46.  
  47.             Chat.sendMessage(msg);
  48.             // Empty text area
  49.             document.getElementById("form-message").value = "";
  50.         }, false);
  51.  
  52.         // Mini API to send a message with the socket and append a message in a UL element.
  53.         var Chat = {
  54.             appendMessage: function (username, message) {
  55.                 var from;
  56.  
  57.                 if (username == clientInformation.username) {
  58.                     from = "me";
  59.                 } else {
  60.                     from = clientInformation.username;
  61.                 }
  62.  
  63.                 // Append List Item
  64.                 var ul = document.getElementById("chat-list");
  65.                 var li = document.createElement("li");
  66.                 li.appendChild(document.createTextNode(from + " : " + message));
  67.                 ul.appendChild(li);
  68.             },
  69.             sendMessage: function (text) {
  70.                 clientInformation.message = text;
  71.                 // Send info as JSON
  72.                 conn.send(JSON.stringify(clientInformation));
  73.                 // Add my own message to the list
  74.                 this.appendMessage(clientInformation.username, clientInformation.message);
  75.             }
  76.         };
  77.     </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement