flex Django通過PyAmf通信

通過PyAmf來通信,需要有幾點注意:

 1.自定義一個amfgateway.py

  

#_*_ coding:UTF-8 _*_

from pyamf.flex import ArrayCollection, ObjectProxy
from pyamf.remoting.gateway.django import DjangoGateway
import pyamf




def saveEmail(request, email, notify):
   print"save..................................."
   return True
   
def getEmailList(request):
   emailList = ['aaa','bbb','cccc'];
   return emailList

def echo(request, data):
    return data

   
services = {
   'myservice.getEmailList':getEmailList,
   'myservice.saveEmail':saveEmail,
    'myservice.echo': echo,
}


myGateway = DjangoGateway(services, expose_request=True)

2.設置url.py

   

from django.conf.urls.defaults import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
from testDjango.amfgateway import  myGateway
admin.autodiscover()


urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testDjango.views.home', name='home'),
    # url(r'^testDjango/', include('testDjango.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
   
       (r'^gateway/$', 'testDjango.amfgateway.myGateway'),

)

3.flex端:

 

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
			   xmlns:s="library://ns.adobe.com/flex/spark" 
			   xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
			   creationComplete="application1_creationCompleteHandler(event)">
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.FlexEvent;
			import mx.messaging.ChannelSet;
			import mx.messaging.channels.AMFChannel;
			import mx.rpc.events.ResultEvent;
			import mx.rpc.remoting.RemoteObject;
			
			private var remoteObject:RemoteObject;
			
			protected function application1_creationCompleteHandler(event:FlexEvent):void
			{
				// Create the AMF Channel
				var channel:AMFChannel = new AMFChannel( "pyamf-channel", "http://127.0.0.1:8000/gateway/" );
				
				// Create a channel set and add your channel(s) to it
				var channels:ChannelSet = new ChannelSet();
				channels.addChannel( channel );
				
				// Create a new remote object and add listener(s)
				remoteObject= new RemoteObject( "myservice" ); // this is the service id
				remoteObject.channelSet = channels;
				remoteObject.echo.addEventListener( ResultEvent.RESULT, onEchoComplete );

				
			}
			
			// Here is the result event listener
			private function onEchoComplete( event:ResultEvent ):void 
			{
				Alert.show( event.result.toString() );
			}

			
			protected function button1_clickHandler(event:MouseEvent):void
			{
				// Make a call to the remote object
				remoteObject.echo( "Hello World" );

			}
			
		]]>
	</fx:Script>
	<fx:Declarations>
		<!-- 將非可視元素(例如服務、值對象)放在此處 -->
	</fx:Declarations>
	<s:Button x="162" y="160" label="按鈕" click="button1_clickHandler(event)"/>
	
</s:Application>

4.python自測

    

#_*_ coding:UTF-8 _*_
'''
Created on 2012-4-16

@author: Administrator
'''
import logging
import string
    
logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s %(levelname)-5.5s [%(name)s] %(message)s'
)

from pyamf.remoting.client import RemotingService

url = 'http://127.0.0.1:8000/gateway/'
gw = RemotingService(url, logger=logging)
service = gw.getService('myservice')

print service.echo('降龍十八掌')


        


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章