`
oncer_L
  • 浏览: 4369 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Flex Session

阅读更多
http://forums.adobe.com/thread/295937
A client (meaning a swf) is represented on the server by a FlexClient instance. You can implement FlexClientListener and register statically with the FlexClient class to receive notification of new clients joining or being invalidated.

The connection between a client and the server is represented by a FlexSession. In the case of HTTP, this is based on a session cookie that the browser includes in all requests to your server. The thing to be aware of here is that a browser session isn't scoped to a specific swf - it's browser wide, meaning it applies to all windows/frames/tabs for the current browser process. So if you're running different swfs in different tabs the server only sees a single session. You can listen to FlexSession events by implementing FlexSessionListener and registering your listener statically with FlexSession.

FlexClient and FlexSession instances have getters to allow to access one from the other.

Subscriptions made using a client-side Consumer or DataService are represented on the server by the MessageClient class. You can implement MessageClientListener and register your listener statically with MessageClient to receive notification when subscriptions are created and invalidated.

MessageClient instances also provide getters for the associated FlexClient (the swf the subscription originiated from) and the FlexSession (the connection between the client and server) that the subscription was established over.

If you want to know when the swf is no longer connected to the server, FlexClientListener#clientDestroyed(FlexClient) lets you know.

One thing to be aware of though is that if the swf is connected to the server over HTTP, that's not a stateful connection so if the browser is closed the server-side HTTP session will remain alive for some time (however long you've configured your session timeout to be). The FlexClient instance representing the swf is not shutdown and invalidated until any and all associated connections (FlexSessions) have terminated. So you'd have a delay in this case.

You can minimize this delay by adding a Javascript onunload handler to the page that renders you swf. In your handler function, make a call into the swf that invokes ChannelSet#disconnectAll() on the ChannelSet instance that your app is using. You could store a ref to your ChannelSet in a global property, or access the 'channelSet' property on a RemoteObject or Producer or Consumer in your app. This will send a 'disconnect' notification to the server-side endpoint, and if you define the <invalidate-session-on-disconnect>true</invalidate-session-on-disconn ect> config property in your AMF or HTTP endpoint, this will invalidate (shut-down) the HTTP session for the client, which will trigger the server-side FlexClient instance to also be invalidated.








Here's an example class that implements FlexSessionListener. It doesn't do anything now but you'd fill in sessionCreated and sessionDestroyed methods to whatever you need to happen in your application.

import flex.messaging.FlexSession;
import flex.messaging.FlexSessionListener;

public class MyFlexSessionListener implements FlexSessionListener
{

public void sessionCreated(FlexSession session)
{
// Your custom code goes here.
}

public void sessionDestroyed(FlexSession session)
{
// Your custom code goes here.
}

}














No client side code is required. You could use a RemoteObject to call a Java class to add listeners but I think the best way to do this sort of thing is to use a bootstrap service (check out flex.messaging.services.AbstractBootstrapService).

Bootstrap servcies are basically Java classes that get called as BlazeDS is initializing, starting and stopping.

Te first thing to do is to let BlazeDS know that you have a bootstrap service. So let's assume that we created a MyBlazeDSListener Java class, you'll need to add the following to services-config.xml:

<services>
<service-include file-path="remoting-config.xml" />
<service-include file-path="proxy-config.xml" />
<service-include file-path="messaging-config.xml" />
<!-- MyBlazeDSListener is your custom Java class -->
<service id="listener" class="test.MyBlazeDSListener"/>
</services>

And here's how MyBlazeDSListener might look like:

package test;

import flex.messaging.FlexSession;
import flex.messaging.FlexSessionListener;
import flex.messaging.MessageClient;
import flex.messaging.MessageClientListener;
import flex.messaging.client.FlexClient;
import flex.messaging.client.FlexClientListener;
import flex.messaging.config.ConfigMap;
import flex.messaging.services.AbstractBootstrapService;

public class MyBlazeDSListener extends AbstractBootstrapService
{
/**
* This method is called as the server is initialized.
*/
public void initialize(String id, ConfigMap properties)
{
System.out.println("MyBlazeDSListener is initializing...");

// Add the FlexSession created listener.
MyFlexSessionListener sessionListener = new MyFlexSessionListener();
FlexSession.addSessionCreatedListener(sessionListener);

// Add the FlexClient created listener.
MyFlexClientListener flexClientListener = new MyFlexClientListener();
FlexClient.addClientCreatedListener(flexClientListener);

// Add the MessageClient created listener.
MyMessageClientListener messageClientListener = new MyMessageClientListener();
MessageClient.addMessageClientCreatedListener(messageClientListener);
}

/**
* This method is called as the server starts.
*/
public void start()
{
// No-op for now.
}

/**
* This method is called as the server stops.
*/
public void stop()
{
// No-op for now.
}

class MyFlexSessionListener implements FlexSessionListener
{

public void sessionCreated(FlexSession session)
{
System.out.println("FlexSession created: " + session.getId());
// Add the FlexSession destroyed listener.
session.addSessionDestroyedListener(this);
}

public void sessionDestroyed(FlexSession session)
{
System.out.println("FlexSession destroyed: " + session.getId());
}
}

class MyFlexClientListener implements FlexClientListener
{

public void clientCreated(FlexClient client)
{
System.out.println("FlexClient created: " + client.getId());
// Add the FlexClient destroyed listener.
client.addClientDestroyedListener(this);
}

public void clientDestroyed(FlexClient client)
{
System.out.println("FlexClient destroyed: " + client.getId());
}

}

class MyMessageClientListener implements MessageClientListener
{

public void messageClientCreated(MessageClient messageClient)
{
System.out.println("MessageClient created: " + messageClient.getClientId());
// Add the MessageClient destroyed listener.
messageClient.addMessageClientDestroyedListener(this);
}

public void messageClientDestroyed(MessageClient messageClient)
{
System.out.println("MessageClient destroyed: " + messageClient.getClientId());
}
}
}

Hope this helped.




分享到:
评论

相关推荐

    AMFPHP与flex通讯

    amfphp和flex通讯的例子,一个及时聊天的例子,适合学习amfphp的朋友和网络开发的朋友学习

    Flex+J2EE获取FlexSession的方法

    Flex+J2EE获取FlexSession的方法

    flex+asp制作的聊天室源码

    flex+asp制作的聊天室源码. 支持管理用户和一般用户分开.管理用户在session那赋值为1即可..可用于在线直播.

    flex+j2ee制作的RIA项目 连接 oracle sql均可以

    1.需要先安装FLEX builder3,Eclipse以及Tomcat 2.在master数据库中建立两张表,附脚本。 3.主要是学习之作,flex写前台与j2ee的后台交互。 知识点: 1.as 4.session设置 5.菜单读取

    flex3多表查询Gilead使用

    多表查询,延长session生命周期,不改配置文件……

    Flex3 界面布局教程 第二篇

    国庆期间,做了不少基于 flex 的开发工作,对 flex 的布局容器有了进一步深入的理解,也找到不少非常棒的文章,分享到这里方便一下大家。

    shiro 与 spring 整合、动态过滤链、以及认证、授权.docx

    其提供的 native-session(即把用户认证后的授权信息保存在其自身提供Session 中)机制,这样就可以和 HttpSession、EJB Session Bean 的基于容器的 Session 脱耦,到和客户端应用、Flex 应用、远程方法调用等都可以...

    shiro框架学习心得

    其提供的native-session(即把用户认证后的授权信息保存在其自身提供Session中)机制,这样就可以和HttpSession、EJB Session Bean的基于容器的Session脱耦,到到和客户端应用、Flex应用、远程方法调用等都可以使用...

    travelibrary-微信小程序实战-流动图书馆.zip

    分为两块视图层(View)和逻辑层(App Service)Flex:flex弹性布局Express : http服务框架websocket: 前后端消息的实时推送mongoose: 操作mongodb数据库pm2: 服务端使用pm2部署,常驻进程截图首页借阅书架发布的图书...

    Ajax应用程序安全

    Web security basics, including common vulnerabilities, common cures, state management and session management How to secure web technologies, such as Ajax, JavaScript, Java applets, Active X controls, ...

    ThinkSNS扩展插件之多图拼接.zip

    美图(多图拼接)是基于Flash AS (具体为Flex)开发的网页交互应用,为了让微博更充满色彩,美图提供了【拼接】、【在线美化】功能,好玩之处下载体验就知道。 倘若你想在美图组件的右上方添加“New”的标志,那么...

    BlazeDS开发者指南

    Accessing dynamic components with a Flex client application 177 Chapter 15: The Ajax client library About the Ajax client library 179 Using the Ajax client library 179 Ajax client library API ...

    asp.net知识库

    技术基础 New Folder 多样式星期名字转换 [Design, C#] .NET关于string转换的一个小Bug Regular Expressions 完整的在.net后台执行javascript脚本集合 ASP.NET 中的正则表达式 常用的匹配正则表达式和实例 ...

Global site tag (gtag.js) - Google Analytics