diff src/server/server.py @ 995:f88325b56a6a

server: dynamic pages first draft: /!\ new dependency: autobahn This patch introduce server part of dynamic pages. Dynamic pages use websockets to establish constant connection with a Libervia page, allowing to receive real time data or update it. The feature is activated by specifying "dynamic = true" in the page. Once activated, page can implement "on_data" method which will be called when data are sent by the page. To send data the other way, the page can use request.sendData. The new "registerSignal" method allows to use an "on_signal" method to be called each time given signal is received, with automatic (and optional) filtering on profile. New renderPartial and renderAndUpdate method allow to append new HTML elements to the dynamic page.
author Goffi <goffi@goffi.org>
date Wed, 03 Jan 2018 01:10:12 +0100
parents 6daa59d44ee2
children 05cc33d8e328
line wrap: on
line diff
--- a/src/server/server.py	Wed Dec 13 00:37:12 2017 +0100
+++ b/src/server/server.py	Wed Jan 03 01:10:12 2018 +0100
@@ -51,6 +51,7 @@
 import urllib
 from httplib import HTTPS_PORT
 import libervia
+from libervia.server import websockets
 from libervia.server.pages import LiberviaPage
 from libervia.server.utils import quote
 from functools import partial
@@ -830,7 +831,7 @@
             defer.returnValue(avatar)
         else:
             filename = os.path.basename(avatar)
-            avatar_url = os.path.join(C.CACHE_DIR, session_data.uuid, filename)
+            avatar_url = os.path.join(session_data.cache_dir, filename)
             defer.returnValue(avatar_url)
 
     def jsonrpc_getAccountDialogUI(self):
@@ -1364,6 +1365,7 @@
 
 
 class Libervia(service.Service):
+    debug = defer.Deferred.debug  # True if twistd/Libervia is launched in debug mode
 
     def __init__(self, options):
         self.options = options
@@ -1452,9 +1454,20 @@
         self.putChild('blog', MicroBlog(self))
         self.putChild(C.THEMES_URL, ProtectedFile(self.themes_dir))
 
+        # websocket
+        if self.options['connection_type'] in ('https', 'both'):
+            wss = websockets.LiberviaPageWSProtocol.getResource(self, secure=True)
+            self.putChild('wss', wss)
+        if self.options['connection_type'] in ('http', 'both'):
+            ws = websockets.LiberviaPageWSProtocol.getResource(self, secure=False)
+            self.putChild('ws', ws)
+
+        # Libervia pages
         LiberviaPage.importPages(self)
         LiberviaPage.setMenu(self.options['menu_json'])
+        ## following signal is needed for cache handling in Libervia pages
         self.bridge.register_signal("psEventRaw", partial(LiberviaPage.onNodeEvent, self), "plugin")
+        self.bridge.register_signal("messageNew", partial(LiberviaPage.onSignal, self, "messageNew"))
 
         # media dirs
         # FIXME: get rid of dirname and "/" in C.XXX_DIR
@@ -1794,6 +1807,31 @@
         else:
             return (iface(session) for iface in args)
 
+    ## Websocket (dynamic pages) ##
+
+    def getWebsocketURL(self, request):
+        if request.isSecure():
+            ws = 'wss'
+        else:
+            ws = 'ws'
+
+        if self.base_url_ext:
+            base_url = self.base_url_ext
+        else:
+            o = self.options
+            if request.isSecure():
+                port = o['port_https_ext'] or o['port_https']
+            else:
+                port = o['port']
+            base_url = request.getRequestHostname().decode('utf-8') + u':' + unicode(port)+ u'/'
+
+        return u'{ws}://{base_url}{ws}'.format(
+            ws = ws,
+            base_url = base_url)
+
+    def registerWSToken(self, token, page, request):
+        websockets.LiberviaPageWSProtocol.registerToken(token, page, request)
+
     ## TLS related methods ##
 
     def _TLSOptionsCheck(self):