当前位置: 首页 > 工具软件 > PyAMF > 使用案例 >

flex Django通过PyAmf通信

杜高谊
2023-12-01

通过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('降龙十八掌')


        


 类似资料: