aboutsummaryrefslogtreecommitdiffstats
path: root/fg21sim/webui/static/js/websocket.js
blob: 08f128f800fc90839f7926a2c08551327b90995e (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Copyright (c) 2016 Weitian LI <liweitianux@live.com>
 * MIT license
 *
 * WebSocket codes for the Web UI of "fg21sim"
 */


$(document).ready(function () {
  /**
   * Check `WebSocket` support
   */
  if (window.WebSocket) {
    // WebSocket supported
    var createWebSocket = function (uri) {
      $("#ws-status .text").text("Supported");
      var proto = {"http:": "ws:", "https:": "wss:"}[location.protocol];
      var host = location.hostname;
      var port = location.port;
      var url = proto + "//" + host + ":" + port + uri;
      var socket = new WebSocket(url);
      return socket;
    };

    var ws = createWebSocket("/ws");
    ws.onopen = function () {
      $("#ws-status").removeClass("label-default label-danger")
                     .addClass("label-success");
      $("#ws-status .icon").removeClass("fa-question-circle fa-warning")
                           .addClass("fa-check-circle");
      $("#ws-status .text").text("Connected");
    };
    ws.onclose = function () {
      $("#ws-status").removeClass("label-default label-success")
                     .addClass("label-danger");
      $("#ws-status .icon").removeClass("fa-question-circle fa-check-circle")
                           .addClass("fa-warning");
      $("#ws-status .text").text("Disconnected!");
    };
    ws.onmessage = function (e) {
      console.log("WS received: " + e.data);
    };

  } else {
    // WebSocket NOT supported
    $("#ws-status").removeClass("label-default").addClass("label-danger");
    $("#ws-status .icon").removeClass("fa-question-circle")
                         .addClass("fa-times-circle");
    $("#ws-status .text").text("Unsupported!!");
  }
});