---
title: "amfast文档翻译——messagingserver（二）"
author: "simomo"
date: "2010-12-09T17:04:11Z"
tags: []
categories: ["编程语言"]
slug: "amfast-messagingserver"
draft: false
lastmod: "2010-12-10T15:27:58Z"
id: 837804
url: "https://simomo.iteye.com/blog/837804"
---

# 存储session数据

- 使用connection对象存储session数据

## 1、RemoteObject 的session 数据

- 客户端使用RemoteObject连接服务器端时，AMFast 会自动为 RemoteObject 创建一个 connection对象 。
- simomo：这里connection是用来存储session数据的。注意：需要用ExtCallableTarget而不是CallableTarget来解析AMF 包或消息，这样才能使用connection对象。

 

```
# Use ExtCallableTarget to expose Packet and Message objects.
def remote_function(packet, message, *args):
    # Get a Connection object from a Flex message.
    my_flex_message = message.body[0]
    connection = my_flex_message.connection

    connection.setSessionAttr('new_attr_name', 'value')
    val = connection.getSessionAttr('new_attr_nam')
```

## 2、NetConnection的 session 数据

- 在客户端使用NetConnection连接服务器端时，也可以使用connection对象存储session数据，不过需要你手动创建connection对象。
- simomo：connection\_id貌似是对应的flex NetConnection对象中id属性

 

```
# To store session data with NetConnection,
# the Connection object will need to be created manually,
# and the connection id will have to be passed as an argument
# or as a Packet header.
def remote_function(packet, message, connection_id):
    try:
        connection = packet.channel.channel_set.getConnection(connection_id)
    except amfast.remoting.channel.NotConnectedError:
        connection_id = packet.channel.channel_set.generateId()
        connection = packet.channel.connect(connection_id)
```

## 3、持久连接和订阅数据（Persistent Connection and Subscription Data）

- 默认情况下，通道集（ChannelSet）使用基于内存的连接管理器（MemoryConnectionManager）和基于内存的订阅管理器（MemorySubscriptionManager），将连接（Connection）信息和消息（Message）信息存储在内存中。simomo：以下的三组连接管理器/订阅管理器也是可供选择的。
- 使用GAE连接管理器（GAEConnectionManager）和GAE订阅管理器（GAESubscriptionManager）将链接信息和消息信息存储在GAE DataStore中。
- 使用Sa连接管理器（SaConnectionManager）和Sa订阅管理（SaSubscriptionManger）将连接信息和消息信息存储在SQL数据库中
- 使用Memcache连接管理器（MemcacheConnectionManager）和Memcache订阅管理器（MemcacheSubscriptionManager）将链接信息和消息信息存储在Memcache缓存系统中
- simomo：总共四种备选方案。

 

# 生产者/消费者消息传递（Consumer/Producer Messaging)

- 通道集（ChannelSet）负责该通道集中的所有通道（Channel）的Flex 消息的订阅和广播。
- 在下面的例子中，消息的消息体是一个字符串，这个消息体也可以是一个python对象。
- simomo：由例子可见，从服务器端直接广播消息也是很容易实现的

# 认证（Authentiction）

- 不论是RemoteObject连接 还是 NetConnection连接，都可以使用认证功能
- 目标对象（Target）和 消息客户端（MessageAgent）都可以很安全
- simomo：channelset需要使用AuthChannelSet，target的secure属性要为True，Channel\_set的subscription\_manager.secure 也要为True

# 配置AMF服务器端（Config Endpoint）

- 通道（Channel）对象使用端点（Endpoint）对象编码/解码自己收到的消息
- 通过配置通道的端点对象，使得每个通道以用户自定义的方式进行对象的序列化和反序列化

## 1、Amf端点（AmfEndpoint）

- Amf端点（AmfEndpoint）使用Amfast内置的编码/解码器
- 在 编码与解码 内容中有详细介绍编码、解码和自定义对象的编码、解码

## 2、PyAmf端点（PyAmfEndpoint)

- PyAmf端点使用纯python编写的编码器、解码器
- 可以去PyAmf的官网了解其编码器、解码器的细节（simomo：被墙了，需要翻\*墙才能上）
- 在 编码与解码 内容中有详细介绍编码、解码和自定义对象的编码、解码

 

 

 随后补充例子代码。该死的360浏览器，例子代码没法自动换行！