FMS端代码
application.onAppStart = function()
{
//获取用户列表
application.users_so = SharedObject.get("users_so", false);
}
application.onConnect = function(client,name,obj)
{
//接受改用户
application.acceptConnection( client );
//
client.name = name;
//添加用户
application.users_so.setProperty( name, name );
//欢迎新用户
client.serverHelloMsg = function( ) {
return "You are welcome, " + client.name + "!";
}
client.sendMsg = function( msg ){
//向所有用户发送某用户的发言
application.broadcastMsg( "sendText", client.name + ":" + msg );
}
}
application.onDisconnect = function( client ){
//从用户列表删除该用户
application.users_so.setProperty( client.userName, null );
}
Flex端代码
//连接服务器
nc = new NetConnection();
nc.addEventListener( NetStatusEvent.NET_STATUS, onStatus );
clientObj = new ClientObject();
nc.client = clientObj;
uo = event.credentials
nc.connect( uri, uo.username, uo );
private function onStatus( event:NetStatusEvent ):void
{
var info:Object = event.info;
trace( info.code );
switch( info.code ){
case "NetConnection.Connect.Success":
currentState = "Explore";
nc.call( "serverHelloMsg", new Responder( sendSucHandle ) );
//获取服务器用户列表
sharedObj = SharedObject.getRemote( "users_so", uri, false );
sharedObj.addEventListener( SyncEvent.SYNC, syncHandle );
//连接服务器
sharedObj.connect(nc);
userList.dataProvider = new ArrayCollection();
trace("the connection was successful");
break;
case "NetConnection.Connect.Rejected":
Alert.show( "连接被拒绝,请再尝试一次!", "失败!" );
break;
case "NetConnection.Connect.Failed":
Alert.show( "无法连接!", "失败!" );
break;
case "NetConnection.Connect.AppShutDown":
Alert.show( "网站已关闭!", "失败!" );
nc.close();
break;
case "NetConnection.Connect.Closed":
Alert.show( "连接已经成功关闭!", "成功!" );
currentState = "Login";
break;
}
}
ClientObject类,nc(NetConnection类)的client指向的对象
package com
{
public class ClientObject{
[Bindable]
public var chatText:String;
public function sendText( msg:String ):void{
if(chatText == null){
chatText = '';
}
chatText += msg + "<br />";
}
}
}
application.onAppStart = function()
{
//获取用户列表
application.users_so = SharedObject.get("users_so", false);
}
application.onConnect = function(client,name,obj)
{
//接受改用户
application.acceptConnection( client );
//
client.name = name;
//添加用户
application.users_so.setProperty( name, name );
//欢迎新用户
client.serverHelloMsg = function( ) {
return "You are welcome, " + client.name + "!";
}
client.sendMsg = function( msg ){
//向所有用户发送某用户的发言
application.broadcastMsg( "sendText", client.name + ":" + msg );
}
}
application.onDisconnect = function( client ){
//从用户列表删除该用户
application.users_so.setProperty( client.userName, null );
}
Flex端代码
//连接服务器
nc = new NetConnection();
nc.addEventListener( NetStatusEvent.NET_STATUS, onStatus );
clientObj = new ClientObject();
nc.client = clientObj;
uo = event.credentials
nc.connect( uri, uo.username, uo );
private function onStatus( event:NetStatusEvent ):void
{
var info:Object = event.info;
trace( info.code );
switch( info.code ){
case "NetConnection.Connect.Success":
currentState = "Explore";
nc.call( "serverHelloMsg", new Responder( sendSucHandle ) );
//获取服务器用户列表
sharedObj = SharedObject.getRemote( "users_so", uri, false );
sharedObj.addEventListener( SyncEvent.SYNC, syncHandle );
//连接服务器
sharedObj.connect(nc);
userList.dataProvider = new ArrayCollection();
trace("the connection was successful");
break;
case "NetConnection.Connect.Rejected":
Alert.show( "连接被拒绝,请再尝试一次!", "失败!" );
break;
case "NetConnection.Connect.Failed":
Alert.show( "无法连接!", "失败!" );
break;
case "NetConnection.Connect.AppShutDown":
Alert.show( "网站已关闭!", "失败!" );
nc.close();
break;
case "NetConnection.Connect.Closed":
Alert.show( "连接已经成功关闭!", "成功!" );
currentState = "Login";
break;
}
}
ClientObject类,nc(NetConnection类)的client指向的对象
package com
{
public class ClientObject{
[Bindable]
public var chatText:String;
public function sendText( msg:String ):void{
if(chatText == null){
chatText = '';
}
chatText += msg + "<br />";
}
}
}