amfast文档翻译——messagingserver(一)

因为要用到amfast,所以尝试着将其文档翻译为中文,供大家参考。

 

MessagingServer(消息服务):

1、overview(概述):

  • 频道集(ChannelSet)对象负责路由(route)受到的请求信息(request message)和派发、发送(dispatch)回复信息(reponse message)。
  • 每个频道集(ChannelSet)对象包含有一个或者多个频道(Channel)对象,频道负责处理消息接受和发送的技术细节。
  • Amfast包含的内置频道(built-in channel)对象有CherryPy , Twisted ,GAE,Django 和 纯WSGI频道。

 

from amfast.remoting.wsgi_channel import WsgiChannelSet, WsgiChannel

# Each ChannelSet contains one or more Channels.
# Channels handle the actual receiving and sending of messages.
channel_set = WsgiChannelSet()

# Create a WsgiChannel, which uses
# WSGI to handle messages.
channel_set.mapChannel(WsgiChannel('channel-name'))

# Serve the WSGI channel
server = simple_server((domain, port), simple_server.WSGIRequestHandler)
server.set_app(channel_set)
server.serve_forever()

# Our messaging server is up and running!

2、映射RPC目的地(Mapping RPC Destination)

  • 目标对象(Target Object)的作用是:将可供客户端调用的方法(callable) 与 RPC请求目的地(RPC Destination)对应(映射)起来,以供客户端使用。(simomo:如此,flex客户端只需向RPC目的地发送请求,至于远端到底是哪个方法在处理,就是完全不需关心的,传说中的松耦合啊)
  • 服务对象(Service Object)是一系列目标对象(Target Object)的集合
  • 服务映射器对象(ServiceMapper Object)是一系列服务(对象)的集合
  • 每个频道集(ChannelSet)包含有一个服务映射器对象(ServiceMapper Object),这个服务映射器对象就是用来路由(route)RPC 消息
  • simomo:有点复杂,解释一下。具体响应处理用户请求的函数或者方法(callable)是不向客户端暴露的,客户端要想调用某个服务器端的某个函数,必须是通过这个函数对应的RPC目的地,而target的作用就是将可供客户端调用的方法与客户端知道的目的地联系起来、对应起来、映射起来,总之是 mapping!然后,一个服务对象包含几个target,这个服务对象也有名字,这个名字是要暴露给客户端知道的;再然后,就是一个服务映射器对象包含几个服务;一个频道集包含一个服务映射器,靠他来路由受到的请求信息。最终,客户端要想调用服务器端的某个函数(方法),只需要调用一个rpc目的地,根本不用理会服务器端里藏了多复杂的东西,变化了多少次实现方法,总之认准这个rpc目的地就总能获得想要的东西。一个rpc目的地可能的样子是:”ServiceName.destinationname“。

 

from amfast import remoting

# This function will be exposed to remote invocation.
# The arguments are passed from the client.
def remote_function(arg1, arg2):
    print "%s:%s" % (arg1, arg2)

# A CallableTarget contains a callable
# that gets invoked when a request message
# is received.
target = remoting.CallableTarget(remote_function, 'exposed_method_name')

# A Service object is a collection of Targets.
service = remoting.Service('exposed_service_name')

# Add a target to a service.
service.mapTarget(target)

# A ServiceMapper object keeps track
# of all exposed services and targets.
service_mapper = remoting.ServiceMapper()

# Add a service to a ServiceMapper.
service_mapper.mapService(service)

# Expose the ServiceMapper's Services
channel_set.service_mapper = service_mapper

# The mapped target can be accessed from a client
# with a NetConnection or RemoteObject request to:
# 'exposed_service_name.exposed_method_name'

 3、将AMF包或消息解析给目标对象(Exposing AMF Packet and Message to Targets)

  • 使用ExtCallableTargetClass(而不是上文中CallableTarget class)将AMF包 和 AMF消息 解析给 目标对象(Target Object)
  • 如果你需要获取AMF包或消息的头(header),这个设计会很有用
  • simomo:按照上下文的意思,CallableTarget只能将amf消息的消息体解析给方法,但是ExtCallableTarget可以将amf消息的消息头也解析给方法以备使用。下面的示例代码可以看出,一个amf消息经过ExtCallableTarget解析后,传给处理函数的是多个参数,不仅仅有客户端调用服务器端方法时传递过来的参数,更有本次AMF 消息本身。
from amfast import remoting

# This function will be exposed.
# In addition to any arguments supplied
# by the client, the AMF Packet and Message
# objects that invoked the Target are passed.
def remote_function(packet, message, arg1, arg2):
    my_flex_message = message.body[0]
    print "%s:%s" % (arg1, arg2)

# Use ExtCallableTarget to expose
# the request Packet and Message to
# the callable.
target = remoting.ExtCallableTarget(remoting_function, 'ext_exposed_method_name')

 

4、将AMF 包头交由目标对象处理(Mapping AMF Packet Headers to Targets)

  • 包含有包头的amf请求一到达服务器端,就会交由与AMF包头向关联的目标对象处理
  • simomo:这个功能的存在,使得开发人员编写一些不去理会AMF包的内容,只用来处理AMF包头的方法。

 

from amfast import remoting

# This function will be invoked when
# the header it is mapped to is present
# in an AMF request Packet.
# The Header object is passed to the function.
def function_exposed_to_header(header_val):
    print header_val

# The target's name must be the name of the header.
target = remoting.CallableTarget(function_exposed_to_header, 'header_name')

# Use the special attribute
# 'packet_header_service' to map
# a header Target.
service_mapper.packet_header_service.mapTarget(target)

 5、将命令消息交给目标对象处理(Mapping AMF CommandMessage to Targets)

  • 目标方法通过获取命令消息(CommandMessage)的操作数(Operation Number)来处理AMF 命令信息
  • simomo:在flex4.1语言参考中,mx.messaging.messages.CommandMessage有操作数这个静态常量。例如,这个常量等于5,表示这个消息是ping操作;如果等于12,则表示该通道已断开……等等,可以参见:http://help.adobe.com/zh_CN/AS3LCR/Flex_4.0/mx/messaging/messages/CommandMessage.html
from amfast import remoting

# This function will be invoked when
# a CommandMessage is received.
# The CommandMessage object body will be passed.
def function_exposed_to_command(command_body):
    print command_body

# Use the CommandMessage's
# 'operation' value to create
# a target.
target = remoting.CallableTarget(function_exposed_to_command, OPERATION_NUMBER)

# Use the special attribute
# 'command_service' to map the target.
service_mapper.command_service.mapTarget(target)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值