summaryrefslogtreecommitdiffstats
path: root/module/obmc/wsgi/examples/websockets/client_simple.html
blob: 7d7503584e796bd4ca82c1e270ce26d46629e4f3 (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
<!DOCTYPE html>
<html>
<head>
  <script type="text/javascript">
    // This simple example subscribes, via websocket, to certain events occuring
    // on the BMC. Specfically, it's interested in an d-bus events (object
    // creations, property changes), occuring in the d-bus namespaces
    // /xyz/openbmc_project/logging and /xyz/openbmc_project/sensors. It's also
    // interested in the interfaces xyz.openbmc_project.Logging.Entry and
    // xyz.openbmc_project.Sensor.Value being added, or property changes to
    // these interfaces.


    // Insert code here to log-on to the BMC. At the moment of writing this
    // example, I don't know how to do that in javascript (I had disabled the
    // the authorization plug-in in rest_dbus.py to test this example). See
    // https://github.com/openbmc/docs/blob/master/rest-api.md for the log-on
    // API.

    // Open a new secure websocket. The rest server on the BMC
    // only supports the /subscribe route with websockets as of now.
    var ws = new WebSocket("wss://<BMC IP>/subscribe");
    // Send in array of d-bus paths and interfaces of interest over the
    // websocket. Either/both of them can be an empty array. The arrays
    // need to be sent in a JSON dictionary.
    // Client will be notified of events occuring on these paths and/or
    // interfaces.
    var data = JSON.stringify(
        {
            "paths": ["/xyz/openbmc_project/logging",
                      "/xyz/openbmc_project/sensors"],
            "interfaces": ["xyz.openbmc_project.Logging.Entry",
                           "xyz.openbmc_project.Sensor.Value"]
        });
    ws.onopen = function() {
        // Send the JSON dictionary
        ws.send(data);
    };
    ws.onmessage = function (evt) {
        // Received a message on the websocket, the response is a JSON dict.
        var response = JSON.parse(evt.data);
        for (var key in response) {
            if (response.hasOwnProperty(key)) {
                var value = response[key];
                if ("path" == key) {
                    // If the d-bus path is in the response, let's do a GET on
                    // that path.
                    var xhr = new XMLHttpRequest();
                    var url = "https://<BMC IP>" + value;
                    xhr.open("GET", url, true);
                    xhr.setRequestHeader("Content-type", "application/json");
                    xhr.send();
                }
            }
        }
    };
  </script>
</head>
</html>
OpenPOWER on IntegriCloud