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.




猜你喜欢

转载自oncer-l.iteye.com/blog/1897972
今日推荐