我是flex的新手,我不知道在as3 / flex中使用共享对象进行私聊。
任何人都可以分享他们对此的参考或想法..,
先谢谢
答案 0 :(得分:0)
http://www.giantflyingsaucer.com/blog/?p=289 这看起来像你在寻找。
答案 1 :(得分:0)
# This code is work as adding user to user list and allow the user's to private chat using #SharedObject.
Client side code :
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" height="500" width="700">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import flash.events.AsyncErrorEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.events.SyncEvent;
import flash.net.NetConnection;
import flash.net.SharedObject;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.events.ListEvent;
public var nc:NetConnection;
private var so:SharedObject=null;
public var tempUser:String;
public var listUsr:String;
private var soUser:String="users_so1";
[Bindable]
private var a:ArrayCollection;
[Bindable]
[Embed(source="src/online.png")]
private var online:Class;
[Bindable]
[Embed(source="src/offline.png")]
private var offline:Class;
private var _alert:Alert;
[Embed(source="src/online.png")]
private var IconCritical:Class;
private var uswerData:ArrayCollection = new ArrayCollection;
protected function btnSubmit_clickHandler(event:MouseEvent):void
{
nc=new NetConnection();
nc.client=this;
nc.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
nc.connect("rtmp://localhost/test",txtUsername.text);
lblCurrentUser.text=txtUsername.text;
}
protected function netStatusHandler(event:NetStatusEvent):void
{
if (event.info.code == "NetConnection.Connect.Failed")
{
trace(""+event.info.code);
Alert.show(""+event.info.code);
}
else if (event.info.code == "NetConnection.Connect.Rejected")
{
trace(""+event.info.code);
Alert.show(""+event.info.code);
}
else(event.info.code == "NetConnection.Connect.Success")
{
trace(""+event.info.code);
SharedObject.defaultObjectEncoding=flash.net.ObjectEncoding.AMF3;
so=SharedObject.getRemote(soUser,nc.uri,false);
so.addEventListener(SyncEvent.SYNC,syncEvent);
so.connect(nc);
so.client=this;
}
}
protected function syncEvent(event:SyncEvent):void{
a=new ArrayCollection();
for (var prop:String in so.data)
{
var obj:Object = new Object;
obj.name = prop;
obj.status=so.data[prop];
a.addItem(obj);
}
lstOnlineUsers.dataProvider=a;
lstOnlineUsers.addEventListener(MouseEvent.DOUBLE_CLICK,onDoubleClick);
}
protected function findIcon(item:Object):String {
if(item.status == "hold") {
//lstOnlineUsers.setStyle("icon",online);
item.icon = online;
//item.name=item.name;
return item.name;
}
else {
//lstOnlineUsers.setStyle("icon",offline);
item.icon = offline;
//item.name=item.name;
return item.name;
}
}
protected function onDoubleClick(e:MouseEvent):void{
listUsr=""+lstOnlineUsers.selectedItem.name;
lblReceiverName.text=listUsr;
}
private function eSendClick(event:Event):void
{
sendMessage();
}
public function sendMessage():void{
var obj:Object=new Object();
obj.sender=lblCurrentUser.text;
obj.msg=txtMessage.text;
obj.receiver=lblReceiverName.text;
so.send("showMessage",obj);
}
public function showMessage(msg:Object):void{
if(msg.sender==lblCurrentUser.text){
if(msg.sender==msg.receiver || lblReceiverName.text=="UserNotSelected")
{
Alert.show("Select A Receiver !");
return;
}
}
// you can comment this below condition statement this will work as public chat
if((msg.receiver==lblCurrentUser.text) || (msg.sender==lblCurrentUser.text) ){
txtDisplay.text += msg.sender+" : "+msg.msg+ "\n";
txtMessage.text = "";
if(msg.receiver==lblCurrentUser.text)
showAlert(msg.sender);
}
}
private function showAlert(sender:String):void{
var myMessage:String = "!!!";
var myTitle:String = "Messagesg Received From "+sender;
_alert = Alert.show(myMessage, myTitle, Alert.OK, null, null, IconCritical);
}
private function onErrorMsg(event:AsyncErrorEvent):void{
trace("========================= onErrorMsg"+"\r");
}
]]>
</fx:Script>
<!--design-->
<s:Panel x="97" y="78" width="239" height="337"
title="Welcome User" id="panelUsers">
<s:Label id="lblCurrentUser" x="103" y="-22" width="89" height="16"
text=""/>
<s:Label id="lblUser" x="30" y="47" width="78" height="30" text="Username"
verticalAlign="middle"/>
<s:TextInput id="txtUsername" width="86" height="29"
horizontalCenter="-44" verticalCenter="-68" />
<!--<s:TextInput id="txtPassword" width="88" height="21" horizontalCenter="-50" verticalCenter="-8" />-->
<s:Button id="btnSubmit" x="126" y="76" label="Submit" click="btnSubmit_clickHandler(event)"/>
<mx:List id="lstOnlineUsers" x="26" y="117" width="138" height="174"
labelFunction="findIcon" doubleClickEnabled="true" />
</s:Panel>
<s:Panel x="381" y="92" width="250" height="308"
id="panelChat" title="To: ">
<s:TextArea x="20" y="9" width="207" height="196" id="txtDisplay" editable="false"/>
<s:TextInput x="20" y="215" width="140" id="txtMessage"/>
<s:Button x="168" y="216" label="SEND" id="btnSend" click="sendMessage()"/>
<s:Label x="31" y="-22" id="lblReceiverName" text="UserNotSelected"/>
</s:Panel>
<!--design-->
</s:WindowedApplication>
------------------------
server side code :
------------------------
application.onAppStart = function()
{
trace("Begin....");
// Get the server shared object 'users_so'
application.users_so = SharedObject.get("users_so1", false);
}
application.onConnect = function(client, name)
{
//trace("clients::"+application.clients.length)
var temp = application.users_so.getProperty(name);
if (temp)
{
application.rejectConnection(client);
return false;
}
client.name = name;
application.users_so.setProperty(name,"hold");
application.acceptConnection(client);
}
application.onDisconnect = function(client)
{
trace("disconnect: " + client.name);
application.users_so.setProperty(client.name, null);
//users_so1.clear();
}