summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDeepak Kodihalli <dkodihal@in.ibm.com>2017-10-18 04:30:41 -0500
committerDeepak Kodihalli <dkodihal@in.ibm.com>2017-10-19 00:33:09 -0500
commitfb3825ceb1d79e9f143e294ff9d20f0392061f2c (patch)
tree3c571119943316af59814f6eda94a418e79dce49
parent639b502e29e7be86a45c3a66fb29d4bcf7b0c793 (diff)
downloadphosphor-rest-server-fb3825ceb1d79e9f143e294ff9d20f0392061f2c.zip
phosphor-rest-server-fb3825ceb1d79e9f143e294ff9d20f0392061f2c.tar.gz
Add websocket client example
Add an example client html/js to denote subscription to BMC events via websockets. Change-Id: I9d07c7f720965de0272af31fc1a84faa49e901f5 Signed-off-by: Deepak Kodihalli <dkodihal@in.ibm.com>
-rw-r--r--module/obmc/wsgi/examples/websockets/client_simple.html59
1 files changed, 59 insertions, 0 deletions
diff --git a/module/obmc/wsgi/examples/websockets/client_simple.html b/module/obmc/wsgi/examples/websockets/client_simple.html
new file mode 100644
index 0000000..7d75035
--- /dev/null
+++ b/module/obmc/wsgi/examples/websockets/client_simple.html
@@ -0,0 +1,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