Commits

Christian Krebs committed 1fe0b62

Web socket support in the http scope interface.

  • Participants
  • Parent commits f648dff
  • Branches fixes

Comments (0)

Files changed (1)

File templates/js/js-http-interface.mako

   var _event_map = null;
   var _status_map = null;
   var _type_map = null;
+  var _socket = null;
 
   var _get_maps = function()
   {
     _proxy.GET( "/get-message?time=" + new Date().getTime(), _receive_dragonkeeper);
   }
 
+  var _receive_dragonkeeper_STP_1_websocket = function(message)
+  {
+    // message format: "[" SERVICE "," COMMAND_ID "," STATUS "," TAG "," PAYLOAD "]"
+    const SERVICE = 0, COMMAND_ID = 1, STATUS = 2, TAG = 3, PAYLOAD = 4;
+    message = JSON.parse(message.data);
+    % if create_test_framework:
+    logger.log(
+        '\nreceived:',
+        '\n  service: ' + message[SERVICE],
+        '\n  command: '+ (_event_map[message[SERVICE]] && _event_map[message[SERVICE]][message[COMMAND_ID]] || "<" + command + ">"),
+        '\n  status: ' + _status_map[message[STATUS]],
+        '\n  tag: ' + message[TAG],
+        '\n  payload: ' + JSON.stringify(message[PAYLOAD])
+      );
+    % endif
+    try
+    {
+      _receive_callback(message[SERVICE], 
+                        message[PAYLOAD], 
+                        message[COMMAND_ID], 
+                        message[STATUS], 
+                        message[TAG]);
+    }
+    catch(e)
+    {
+      opera.postError(
+        'failed to handle message\n' +
+        '  service: ' + message[SERVICE] + '\n' +
+        '  command: ' + message[COMMAND_ID] + '\n' +
+        '  message: ' + JSON.stringify(message[PAYLOAD]) + '\n' +
+        '  ------------------------------------\n' +
+        '  error message: ' + e.message + '\n' +
+        '  ------------------------------------\n' +
+        '  error stacktrace: \n' + e.stacktrace + '\n' +
+        '  ------------------------------------\n'
+        )
+    }
+  }
+
   var _scopeTransmit_STP_0 = function(service, message, command_id, tag)
   {
     /** 
     % endif
   }
 
+  var _scopeTransmit_STP_1_websocket = function(service, message, command_id, tag)
+  {
+    /** 
+      * message format: "[" SERVICE "," COMMAND_ID "," STATUS "," TAG "," PAYLOAD "]"
+      * format 1 is JSON structures (UMS) , encoding UTF-8
+      */
+    _socket.send(JSON.stringify([service, command_id, 0, tag, message]));
+    % if create_test_framework:
+    logger.log(
+        '\nsent:',
+        '\n  service: ' + service,
+        '\n  command: '+ (_event_map[service] && _event_map[service][command_id] || "<" + command_id + ">"),
+        '\n  tag: ' + tag,
+        '\n  payload: ' + JSON.stringify(message)
+      );
+    % endif
+  }
+
   var _receive_dragonkeeper = null;
 
   var _on_stp_version = function(xml, xhr)
       {
         _receive_dragonkeeper = _receive_dragonkeeper_STP_0;
         self.scopeTransmit = _scopeTransmit_STP_0;
+        _finalize_on_stp_version();
         break;
       }
       case "STP/1":
       {
-        _receive_dragonkeeper = _receive_dragonkeeper_STP_1;
-        self.scopeTransmit = _scopeTransmit_STP_1;
+        if (window.WebSocket)
+          _setup_stp_1_web_socket();
+        else
+          _setup_stp_1_xhr_post_get();
         break;
       }
       default:
         opera.postError("not able to handle STP version" + self.stpVersion + " in _on_stp_version");
       }
     }
+  }
+
+  var _setup_stp_1_xhr_post_get = function()
+  {
+    _receive_dragonkeeper = _receive_dragonkeeper_STP_1;
+    self.scopeTransmit = _scopeTransmit_STP_1;
+    _finalize_on_stp_version();
+  }
+  
+  var _setup_stp_1_web_socket = function()
+  {
+    _socket = new WebSocket("ws://" + window.location.host + "/stp-1-channel");
+    _socket.onopen = function()
+    {
+      this.onmessage = _receive_dragonkeeper_STP_1_websocket;
+      self.scopeTransmit = _scopeTransmit_STP_1_websocket;
+      _finalize_on_stp_version(true);
+    }
+    _socket.onclose = function()
+    {
+      _setup_stp_1_xhr_post_get();
+    }
+  }
+  
+  var _finalize_on_stp_version = function(has_web_socket)
+  {
     _connect_callback(_proxy.services.join(','));
-    _proxy.GET( "/get-message?time=" + new Date().getTime(), _receive_dragonkeeper);
+    if (!has_web_socket)
+      _proxy.GET( "/get-message?time=" + new Date().getTime(), _receive_dragonkeeper);
     if(window.ini.debug)
-    {
       cls.debug.wrap_transmit();
-    }
   }
 
   var _proxy_onsetup = function(xhr)