在开发如股票客户端的软件,客户端的数据在实时刷新,是由于服务端在不停的推送信息到客户端,客户端只是显示信息。
1,webservice 的开发
2.配置
3.客户端调用
1,webservice 的开发
/// <summary>
/// 构建推送数据的服务
/// </summary>
[RemotingService]
public class DateFeedService
{
private static Thread _thread;
private static bool _isRunning = false;
static object _objLock = new object();
public DateFeedService()
{
}
public void toggle()
{
lock(_objLock)
{
if (!_isRunning)
{
_isRunning = true;
_thread = new Thread(new ThreadStart(Push));
_thread.Start();
}
else
{
_isRunning = false;
_thread.Join();
_thread = null;
}
}
}
public void Push()
{
while (_isRunning)
{
MessageBroker msbroker = MessageBroker.GetMessageBroker(null);
DateTime now = DateTime.Now;
AsyncMessage msg = new AsyncMessage();
msg.destination = "datafeed";
msg.clientId = Guid.NewGuid().ToString("D");
msg.messageId = Guid.NewGuid().ToString("D");
msg.timestamp = Environment.TickCount;
Hashtable body = new Hashtable();
body.Add("userId", "deamon");
body.Add("msg", now.ToString());
msg.body = body;
msbroker.RouteMessage(msg);
Thread.Sleep(1000);
}
}
}
/// 构建推送数据的服务
/// </summary>
[RemotingService]
public class DateFeedService
{
private static Thread _thread;
private static bool _isRunning = false;
static object _objLock = new object();
public DateFeedService()
{
}
public void toggle()
{
lock(_objLock)
{
if (!_isRunning)
{
_isRunning = true;
_thread = new Thread(new ThreadStart(Push));
_thread.Start();
}
else
{
_isRunning = false;
_thread.Join();
_thread = null;
}
}
}
public void Push()
{
while (_isRunning)
{
MessageBroker msbroker = MessageBroker.GetMessageBroker(null);
DateTime now = DateTime.Now;
AsyncMessage msg = new AsyncMessage();
msg.destination = "datafeed";
msg.clientId = Guid.NewGuid().ToString("D");
msg.messageId = Guid.NewGuid().ToString("D");
msg.timestamp = Environment.TickCount;
Hashtable body = new Hashtable();
body.Add("userId", "deamon");
body.Add("msg", now.ToString());
msg.body = body;
msbroker.RouteMessage(msg);
Thread.Sleep(1000);
}
}
}
2.配置
WEB-INF/flex/messaging-config.xml
<destination id="datafeed">
<adapter ref="messagingAdapter"/>
<channels>
<channel ref="my-rtmp"/>
</channels>
<properties>
<source>*</source>
<network>
<!-- The session-timeout element specifies the idle time in minutes before a subscriber is unsubscribed. When you set the value to 0 (zero), subscribers are not forced to unsubscribe automatically. The default value is 20. -->
<session-timeout>20</session-timeout>
</network>
</properties>
</destination>
<destination id="datafeed">
<adapter ref="messagingAdapter"/>
<channels>
<channel ref="my-rtmp"/>
</channels>
<properties>
<source>*</source>
<network>
<!-- The session-timeout element specifies the idle time in minutes before a subscriber is unsubscribed. When you set the value to 0 (zero), subscribers are not forced to unsubscribe automatically. The default value is 20. -->
<session-timeout>20</session-timeout>
</network>
</properties>
</destination>
3.客户端调用
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF" layout="absolute">
<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.messaging.events.MessageEvent;
import mx.managers.CursorManager;
private function logIn():void
{
CursorManager.setBusyCursor();
loginRO.logout();
loginRO.setCredentials(username.text, password.text);
loginRO.SecureOperation();
}
/*Call logout*/
private function logOut():void
{
loginRO.logout();
appViews.selectedChild = loginView;
}
private function loginResult(event:ResultEvent):void {
var result:Boolean = event.result as Boolean;
appViews.selectedChild = secureView;
consumer.setCredentials(username.text, password.text);
consumer.subscribe();
CursorManager.removeAllCursors();
}
private function loginFault(event:FaultEvent):void{
Alert.show( ObjectUtil.toString(event.fault), "Authentication failed" );
}
public function messageHandler(event:MessageEvent):void
{
var body:Object = event.message.body;
output.text = body.userId + ": " + body.msg;
}
private function toggle():void
{
feedService.toggle();
}
private function onFeedResult(event:ResultEvent):void
{
}
private function onfault(event:Event):void
{
trace(event.toString());
}
]]>
</mx:Script>
<mx:RemoteObject id="loginRO" destination="fluorine" source="FluServiceLibrary.MyLoginService" >
<mx:method name="SecureOperation" result="loginResult(event)" fault="loginFault(event)" />
</mx:RemoteObject>
<mx:RemoteObject id="feedService" destination="fluorine" source="FluServiceLibrary.DateFeedService">
<mx:method name="toggle" result="onFeedResult(event)" fault="onfault(event)"/>
</mx:RemoteObject>
<mx:Consumer id="consumer" destination="datafeed" message="messageHandler(event)"/>
<mx:ViewStack id="appViews" width="100%" height="100%">
<!--login view-->
<mx:HBox horizontalAlign="center" verticalAlign="middle" id="loginView" width="100%" height="100%">
<mx:Panel title="Login" id="loginPanel" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Form id="loginForm">
<mx:FormItem label="Username:">
<mx:TextInput id="username"/>
</mx:FormItem>
<mx:FormItem label="Password:">
<mx:TextInput id="password" displayAsPassword="true"/>
</mx:FormItem>
</mx:Form>
<mx:ControlBar>
<mx:Spacer width="100%" id="spacer1"/>
<mx:Button label="Login" id="loginButton"
enabled="{(username.text.length == 0 || password.text.length == 0) ? false : true}"
toolTip="{loginButton.enabled == true ? 'Click to submit' : 'Enter username and password'}"
click="logIn()" />
</mx:ControlBar>
</mx:Panel>
</mx:HBox>
<!--end loginView-->
<!--secure area view, made visible after a successful login-->
<mx:HBox id="secureView" width="100%" height="100%">
<mx:Box x="20" y="20">
<mx:VBox>
<mx:HBox>
<mx:Button label="Toggle Date Feed" click="toggle()"/>
<mx:Label text="DateFeed" />
</mx:HBox>
</mx:VBox>
</mx:Box>
<mx:Label id="output" text="..." />
</mx:HBox>
<!--end secure area view-->
</mx:ViewStack>
</mx:Application>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" backgroundColor="#FFFFFF" layout="absolute">
<mx:Script>
<![CDATA[
import mx.utils.ObjectUtil;
import mx.rpc.events.FaultEvent;
import mx.controls.Alert;
import mx.rpc.events.ResultEvent;
import mx.messaging.events.MessageEvent;
import mx.managers.CursorManager;
private function logIn():void
{
CursorManager.setBusyCursor();
loginRO.logout();
loginRO.setCredentials(username.text, password.text);
loginRO.SecureOperation();
}
/*Call logout*/
private function logOut():void
{
loginRO.logout();
appViews.selectedChild = loginView;
}
private function loginResult(event:ResultEvent):void {
var result:Boolean = event.result as Boolean;
appViews.selectedChild = secureView;
consumer.setCredentials(username.text, password.text);
consumer.subscribe();
CursorManager.removeAllCursors();
}
private function loginFault(event:FaultEvent):void{
Alert.show( ObjectUtil.toString(event.fault), "Authentication failed" );
}
public function messageHandler(event:MessageEvent):void
{
var body:Object = event.message.body;
output.text = body.userId + ": " + body.msg;
}
private function toggle():void
{
feedService.toggle();
}
private function onFeedResult(event:ResultEvent):void
{
}
private function onfault(event:Event):void
{
trace(event.toString());
}
]]>
</mx:Script>
<mx:RemoteObject id="loginRO" destination="fluorine" source="FluServiceLibrary.MyLoginService" >
<mx:method name="SecureOperation" result="loginResult(event)" fault="loginFault(event)" />
</mx:RemoteObject>
<mx:RemoteObject id="feedService" destination="fluorine" source="FluServiceLibrary.DateFeedService">
<mx:method name="toggle" result="onFeedResult(event)" fault="onfault(event)"/>
</mx:RemoteObject>
<mx:Consumer id="consumer" destination="datafeed" message="messageHandler(event)"/>
<mx:ViewStack id="appViews" width="100%" height="100%">
<!--login view-->
<mx:HBox horizontalAlign="center" verticalAlign="middle" id="loginView" width="100%" height="100%">
<mx:Panel title="Login" id="loginPanel" horizontalScrollPolicy="off" verticalScrollPolicy="off">
<mx:Form id="loginForm">
<mx:FormItem label="Username:">
<mx:TextInput id="username"/>
</mx:FormItem>
<mx:FormItem label="Password:">
<mx:TextInput id="password" displayAsPassword="true"/>
</mx:FormItem>
</mx:Form>
<mx:ControlBar>
<mx:Spacer width="100%" id="spacer1"/>
<mx:Button label="Login" id="loginButton"
enabled="{(username.text.length == 0 || password.text.length == 0) ? false : true}"
toolTip="{loginButton.enabled == true ? 'Click to submit' : 'Enter username and password'}"
click="logIn()" />
</mx:ControlBar>
</mx:Panel>
</mx:HBox>
<!--end loginView-->
<!--secure area view, made visible after a successful login-->
<mx:HBox id="secureView" width="100%" height="100%">
<mx:Box x="20" y="20">
<mx:VBox>
<mx:HBox>
<mx:Button label="Toggle Date Feed" click="toggle()"/>
<mx:Label text="DateFeed" />
</mx:HBox>
</mx:VBox>
</mx:Box>
<mx:Label id="output" text="..." />
</mx:HBox>
<!--end secure area view-->
</mx:ViewStack>
</mx:Application>