Commits

Jeffrey Gelens  committed 0ee0f91

Simplified protocol loading

  • Participants
  • Parent commits abef34c

Comments (0)

Files changed (4)

File examples/wamp_example.html

+<!DOCTYPE html>
+<html>
+    <head>
+
+        <!-- include AutobahnJS .. that's all you need -->
+        <script src="http://autobahn.s3.amazonaws.com/js/autobahn.min.js"></script>
+
+        <script>
+            // WAMP session object
+        var sess = null;
+
+        window.onload = function() {
+
+            var wsuri;
+            if (window.location.protocol === "file:") {
+                wsuri = "ws://localhost:8000";
+            } else {
+                wsuri = "ws://" + window.location.hostname + ":8000";
+            }
+
+            // connect to WAMP server
+            ab.connect(wsuri,
+
+                       // WAMP session was established
+                       function (session) {
+
+                           sess = session;
+
+                           console.log("Connected to " + wsuri);
+                           test();
+                       },
+
+                       // WAMP session is gone
+                       function (code, reason) {
+
+                           sess = null;
+
+                           if (code == ab.CONNECTION_UNSUPPORTED) {
+                               window.location = "http://autobahn.ws/unsupportedbrowser";
+                           } else {
+                               console.log(reason);
+                           }
+                       }
+                      );
+        };
+
+        function test() {
+
+            // Call a remote procedure
+            //
+            sess.call("http://localhost:8000/calc#add", 23, 7).then(
+                function (res) {
+                    // RPC returned successfully
+                    console.log("RPC result: " + res);
+                },
+                function (error) {
+                    // An error occured:
+                    //    error.uri      : The URI of the error
+                    //    error.desc     :  Human readable, for developers and logging
+                    //    error.details  :  Any error details (optional)
+                    console.log("RPC error: " + error.desc);
+                }
+            );
+
+            // Call a remote procedure, and a 2nd one with the result of the first
+            //
+            //sess.call("http://example.com/simple/calc#sub", 15, 5).then(
+            //    function (res) {
+            //        // call a remote procedure
+            //        sess.call("http://example.com/simple/calc#mul", res, res).then(
+            //            function (res) {
+            //                console.log("RPC result: " + res);
+            //            }
+            //        );
+            //    }
+            //);
+        };
+        </script>
+    </head>
+    <body>
+        <h1>RPCs with AutobahnJS - Example 1</h1>
+        <noscript>
+            <span style="color: #f00; font-weight: bold;">
+                You need to turn on JavaScript.
+            </span>
+        </noscript>
+        <p>
+            Open development console (press F12) to watch.
+        </p>
+    </body>
+</html>

File examples/wamp_example.py

 
 
 class WampApplication(WebSocketApplication):
+    protocol_class = WampProtocol
+
     def on_open(self):
-        self.wamp.register_procedure("http://localhost:8000/calc#add", self.add)
-        self.wamp.register_object("http://localhost:8000/test#", RPCTestClass())
-        self.wamp.register_pubsub("http://localhost:8000/somechannel")
+        wamp = self.protocol
+        wamp.register_procedure("http://localhost:8000/calc#add", self.add)
+        wamp.register_object("http://localhost:8000/test#", RPCTestClass())
+        wamp.register_pubsub("http://localhost:8000/somechannel")
 
         print "opened"
 
     def on_close(self):
         print "closed"
 
-    def add(self, var, has):
-        has.update({'bloep': var})
-        return has
+    def add(self, var1, var2):
+        return var1 + var2
 
-    def build_protocol(self):
-        self.wamp = WampProtocol(self)
-        return self.wamp
 
-    @classmethod
-    def protocol(cls):
-        return WampProtocol.PROTOCOL_NAME
-
+def static_wsgi_app(environ, start_response):
+    start_response("200 OK", [("Content-Type", "text/html")])
+    return open("wamp_example.html").readlines()
 
 if __name__ == "__main__":
     resource = Resource({
+        '/page': static_wsgi_app,
         '/': WampApplication
     })
 

File geventwebsocket/protocols/base.py

 class BaseProtocol(object):
-    PROTOCOL_NAME = ""
+    PROTOCOL_NAME = ''
 
     def __init__(self, app):
         self._app = app

File geventwebsocket/resource.py

 
 
 class WebSocketApplication(object):
+    protocol_class = BaseProtocol
+
     def __init__(self, ws):
-        self.protocol = self.build_protocol()
+        self.protocol = self.protocol_class(self)
         self.ws = ws
 
     def handle(self):
             try:
                 message = self.ws.receive()
             except WebSocketError:
-                break
+                self.protocol.on_close()
 
-            if message is None:
-                self.protocol.on_close()
-                break
-            else:
-                self.protocol.on_message(message)
+            self.protocol.on_message(message)
 
     def on_open(self, *args, **kwargs):
         pass
     def on_message(self, message, *args, **kwargs):
         self.ws.send(message, **kwargs)
 
-    def build_protocol(self):
-        return BaseProtocol(self)
-
     @classmethod
-    def protocol(self):
-        return ''
+    def protocol_name(cls):
+        return cls.protocol_class.PROTOCOL_NAME
 
 
 class Resource(object):
 
     def app_protocol(self, path):
         if path in self.apps:
-            return self.apps[path].protocol()
+            return self.apps[path].protocol_name()
         else:
             return ''