From bd86d38a7655f4416ba14e0af66e92e1c9657964 Mon Sep 17 00:00:00 2001 From: Eva Balycheva Date: Thu, 7 Jan 2016 03:13:45 +0300 Subject: [PATCH] Make websocket.html process notifications Currently Websocket html client example can only process responses from Zaqar. It can't handle notifications from Zaqar. When notification is received by Websocket html client example (examples/websocket.html), javascript console throws an Exception: TypeError: data.request is undefined That's because Websocket html client example always expects received payload('data' object) to have 'request', 'headers' and 'body' properties. All these properties exist in payload only when response from Zaqar is received. But when notification is received, 'data' object do not have 'request' and 'headers' properties. This patch makes Websocket html client example able to process notifications from Zaqar by examining 'data' object's properties and, if notification is detected, processing 'data' differently. Closes-Bug: 1531671 Change-Id: I3ea4d092f097d22784f21bf9c38657ff4e12c32d --- examples/websocket.html | 47 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 19 deletions(-) diff --git a/examples/websocket.html b/examples/websocket.html index 498d52083..37134554c 100644 --- a/examples/websocket.html +++ b/examples/websocket.html @@ -53,25 +53,34 @@ var node = document.createElement('div'); var msg = new Date().toUTCString(); var data = JSON.parse(evt.data); - var action = data["request"]["action"]; - msg += " action: " + action; - msg += " status: " + data["headers"]["status"]; - msg += " body: " + JSON.stringify(data["body"]); - node.appendChild(document.createTextNode(msg)); - $('#log').append(node); - - if (action == 'queue_list') { - var queues = data['body']['queues']; - display_queues(queues); - } else if (action == 'message_list') { - var messages = data['body']['messages']; - display_messages(messages); - } else if (action == 'queue_create' || action == 'queue_delete') { - list_queues(); - } else if (action == 'authenticate' && data["headers"]["status"] == 200) { - list_queues(); - } else if (action == 'message_post' || action == 'message_delete') { - list_messages(); + if ('request' in data && 'headers' in data) { + // Response received + var action = data["request"]["action"]; + msg += " action: " + action; + msg += " status: " + data["headers"]["status"]; + msg += " body: " + JSON.stringify(data["body"]); + node.appendChild(document.createTextNode(msg)); + $('#log').append(node); + if (action == 'queue_list') { + var queues = data['body']['queues']; + display_queues(queues); + } else if (action == 'message_list') { + var messages = data['body']['messages']; + display_messages(messages); + } else if (action == 'queue_create' || action == 'queue_delete') { + list_queues(); + } else if (action == 'authenticate' && data["headers"]["status"] == 200) { + list_queues(); + } else if (action == 'message_post' || action == 'message_delete') { + list_messages(); + } + } else { + // Notification received + msg += " Got notification." + msg += " body: " + JSON.stringify(data["body"]); + node.appendChild(document.createTextNode(msg)); + $('#log').append(node); + list_messages(); } } login = function(frm) {