Browse Source

Figure out the websocket path in JS

Getty Ritter 4 years ago
parent
commit
a88f3c906f
1 changed files with 10 additions and 2 deletions
  1. 10 2
      static/main.js

+ 10 - 2
static/main.js

@@ -16,7 +16,16 @@ window.onload = function() {
         }
     }
 
-    const socket = new WebSocket("ws://localhost:8000/socket");
+    var loc = window.location, new_uri;
+    if (loc.protocol === "https:") {
+        new_uri = "wss:";
+    } else {
+        new_uri = "ws:";
+    }
+    new_uri += "//" + loc.host;
+    new_uri += loc.pathname + "socket";
+
+    const socket = new WebSocket(new_uri);
     socket.addEventListener("open", function (event) {
         console.log("Connected to server!");
         socket.send(JSON.stringify(config));
@@ -31,7 +40,6 @@ window.onload = function() {
 
 
     $('#chatbox').on('keypress', function (e) {
-        console.log('!!!');
        if (e.which === 13) {
            $(this).attr("disabled", "disabled");
            socket.send(JSON.stringify({"content": $('#chatbox').val()}))