summaryrefslogtreecommitdiff
path: root/web-static/socket.html
blob: c0939da581c4b7760faa07f27fb582fd5a788271 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<!DOCTYPE HTML>
<html>
  <head>
    <title>rhctl Websocket Testclient</title>
    <meta charset="utf-8">
    <style type="text/css">
      body {
        background-color: #555;
      }

      div.data {
        background-color: white;
        border: 1px solid;
        padding: 1em;
        font-family: monospace;
        margin-top: 1em;
        margin-bottom: 1em;
      }

      #progress {
        background-color: white;
        border: 1px solid;
        padding: 1em;
        font-family: monospace;
        margin-top: 2em;
        margin-bottom: 0em;
      }

      #progress span.caption {
        font-weight: bold;
      }

      td {
        text-align: right;
      }
    </style>
    <script src="jquery.min.js"></script>
    <script type="text/javascript">
      function State(req) {
        this.req = req
        this.sock = new WebSocket("ws://localhost:4080/socket");
        this.sock_onmessage = function (event) {
          $('#statemsg').text(event.data);
        }
        this.sock.onmessage = this.sock_onmessage.bind(this);

        this.sock_onopen = function() {
          this.sock.send(JSON.stringify(this.req));
          $('#buttonstate').attr('disabled','disabled')
        }
        this.sock.onopen = this.sock_onopen.bind(this);
      }

      var s;

      function state() {
        req = { COMMAND: "state" };
        s = new State(req);
      }


      function Subscription(req) {
        this.req = req
        this.sock = new WebSocket("ws://localhost:4080/socket");
        this.sock_onmessage = function (event) {
          $('#rawmsg').append(event.data + "\n\n");
        }
        this.sock.onmessage = this.sock_onmessage.bind(this);

        this.sock_onopen = function() {
          this.sock.send(JSON.stringify(this.req));
          $('#buttonsub').attr('disabled','disabled')
        }
        this.sock.onopen = this.sock_onopen.bind(this);
      }

      var sub;

      function sub() {
        req = { COMMAND: "subscribe", ARGS: [ $('#subtype').val() ]};
        sub = new Subscription(req);
      }

      function init() {
        $('#buttonstate').removeAttr('disabled','disabled');
        $('#buttonsub').removeAttr('disabled','disabled');
      }

    </script>
  </head>
  <body onload="init()">

    <h1>rhctl Websocket Testclient</h1>

    <div>
      <button id="buttonstate" onclick="state()">get state</button>
      <div id="statemsg" class="data"></div>
    </div>

    <div>
      <input id="subtype" type="text" size="30" value="state">
      <button id="buttonsub" onclick="sub()">subscribe</button>
      <div id="rawmsg" class="data"></div>
    </div>

  </body>
</html>