Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File tg2_pyamf.patch,
5.3 KB
(added by thijs, 3 years ago) |
|
|
-
|
|
|
|
| 8 | 8 | .. contents:: Table of Contents |
| 9 | 9 | :depth: 2 |
| 10 | 10 | |
| 11 | | PyAMF provides a simple way to talk to Flex applications using the binary AMF protocol. The main advantages of AMF are that it: |
| | 11 | PyAMF provides a simple way to talk to Flash Player applications using the binary AMF protocol. The main advantages of AMF are that it: |
| 12 | 12 | |
| 13 | 13 | #. Provides native Flash ActionScript representations of your data, so instantiating it on the client side is almost instantaneous. |
| 14 | 14 | #. Is understood by the RemoteObject in Flex, so it is very easy to implement remote procedure calls from the Flex client. |
| 15 | 15 | |
| 16 | | PyAMF provides serialization of a variety of native python types, from strings, lists, and dictionaries to datetime objects, elementtree elements, and custom classes. And a lot of this is automatic, and very cool. Return a dictionary from your pyamf service, and you'll get an ActionScript hash/object on the other side. |
| | 16 | PyAMF provides serialization of a variety of native Python types, from strings, lists, and dictionaries to datetime objects, elementtree elements, and custom classes. And a lot of this is automatic, and very cool. Return a dictionary from your PyAMF service, and you'll get an ActionScript hash/object on the other side. |
| 17 | 17 | |
| 18 | | PyAMF provides a simple WSGI Application that can be used to setup RPC style service easily in Python. And because TG2 supports WSGI from top-to-bottom, it's very simple to setup a TG2 app that contains web-services for your flex applications. All you need to do is: |
| | 18 | PyAMF provides a simple WSGI Application that can be used to setup RPC style service easily in Python. And because TG2 supports WSGI from top-to-bottom, it's very simple to setup a TG2 app that contains web-services for your Flex applications. All you need to do is: |
| 19 | 19 | |
| 20 | 20 | #. Install a bunch of stuff and setup a TG2 app |
| 21 | 21 | #. Create a PyAMF gateway for your services |
| … |
… |
|
| 27 | 27 | Installing Stuff: |
| 28 | 28 | ---------------------- |
| 29 | 29 | |
| 30 | | If you haven't installed TG2, you'll need to do that first (http:docs.turbogears.org/2.0/RoughDocs/DownloadInstall). You'll need TG2 version that's newer than February 20, 2007 for this to work. All released versions of TG2 should work, but early SVN versions may need to be updated. Once you've got an up-to-date version of TG2, you'll need to install PyAMF, which you can do by:: |
| | 30 | If you haven't installed TG2, you'll need to do that first (http:docs.turbogears.org/2.0/main/DownloadInstall). You'll need TG2 version that's newer than February 20, 2007 for this to work. All released versions of TG2 should work, but early SVN versions may need to be updated. Once you've got an up-to-date version of TG2, you'll need to install PyAMF, which you can do by:: |
| 31 | 31 | |
| 32 | 32 | easy_install pyamf |
| 33 | 33 | |
| … |
… |
|
| 48 | 48 | |
| 49 | 49 | from pyamf.remoting.gateway.wsgi import WSGIGateway |
| 50 | 50 | |
| 51 | | Class Services(object): |
| | 51 | class Services(object): |
| 52 | 52 | |
| 53 | | def echo(data): |
| | 53 | def echo(self, data): |
| 54 | 54 | return "Turbogears gateway says:" + str(data) |
| 55 | 55 | |
| 56 | | def sum(a, b): |
| | 56 | def sum(self, a, b): |
| 57 | 57 | return a + b |
| 58 | 58 | |
| 59 | | def scramble(text): |
| | 59 | def scramble(self, text): |
| 60 | 60 | from random import shuffle |
| 61 | 61 | s = [x for x in text] |
| 62 | 62 | shuffle(s) |
| … |
… |
|
| 68 | 68 | GatewayController = WSGIGateway(services) |
| 69 | 69 | |
| 70 | 70 | This sets up a GatewayController WSGI app that has three services that |
| 71 | | can be called from flex: echo, sum, and scramble, which each do exactly what |
| | 71 | can be called from Flex: echo, sum, and scramble, which each do exactly what |
| 72 | 72 | they say they do. |
| 73 | 73 | |
| 74 | 74 | Setup a controller that uses the GatewayController WSGI app: |
| … |
… |
|
| 88 | 88 | Of course, you'll need to import use_wsgi_app from tg, and your |
| 89 | 89 | GatewayController from wherever you put it. But once you've done those |
| 90 | 90 | things you'll have a AMF Gateway mounted at /gateway which you can use from |
| 91 | | flex. |
| | 91 | Flex. |
| 92 | 92 | |
| 93 | 93 | |
| 94 | 94 | Create a Flex Client |
| … |
… |
|
| 114 | 114 | <mx:Button click="remoteObj.scramble(c.text)" label="Scramble"/> |
| 115 | 115 | <mx:TextInput id="c" text="She sells seashells by the seashore" width="100%"/> |
| 116 | 116 | </mx:HBox> |
| 117 | | <mx:Text id="result" width="100%" height="100%"/> |
| | 117 | <mx:Text id="result" width="100%" height="100%" /> |
| 118 | 118 | |
| 119 | 119 | <mx:Script> |
| 120 | 120 | <![CDATA[ |
| … |
… |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | private function remoteFault(fault:FaultEvent): void { |
| 134 | | result.text = ObjectUtil.toString(fault); |
| | 134 | result.text = fault.fault.faultDetail; |
| 135 | 135 | } |
| 136 | 136 | ]]> |
| 137 | 137 | </mx:Script> |
| 138 | 138 | </mx:Application> |
| 139 | 139 | |
| 140 | | You can paste that into a new Flex Builder project (or use the free SDK to create a project with the text editor of your choice). You can then put the HTML and SWF files generated by the builder into your TG2 project's static directory (wherever you want them to be available) at which point you should be able to browse there, get your Flex app, and use it to connect to the web services you just created. |
| | 140 | You can paste that into a new Flex Builder project (or use the free SDK to create a project with the text editor of your choice). You can then put the HTML and SWF files generated by Flex Builder into your TG2 project's static directory (wherever you want them to be available) at which point you should be able to browse there, get your Flex app, and use it to connect to the web services you just created. |
| 141 | 141 | |
| 142 | 142 | |
| 143 | 143 | |
Download in other formats: