User Count.
Hi All.
First of all, I'm sorry for my English. But, I'll do my best to explain.
I'm developing Text Chat App with FMS 4, Flex 4.6.
I want to count how many users in.
I've searched enough. And I made some codes.
Server Side Code(main.asc)
-------------------------------------------------------------------
application.onAppStart = function()
{
trace("hihihi");
}
application.onConnect = function(client)
{
application.acceptConnection(client);
userCount = application.clients.length;
// Define new client function for a nc.call().
client.callFromClient = function() {
return userCount;
}
}
application.onDisconnect = function(client){
trace("Connection is disconnected!!!");
userCount = application.clients.length;
trace("length is " + userCount);
for(var i=0; i<application.clients.length; i++){
application.clients.call("setUserCount", null, userCount);
}
}
SimpleChatTest.mxml
-------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="497" height="518" minWidth="955" minHeight="600" creationComplete="initApp()">
<fx:Script>
<![CDATA[
private var nc:NetConnection;
private var so:SharedObject;
private var soUserCount:SharedObject;
private var myResponder:Responder = new Responder(onReply);
private function initApp():void{
nc = new NetConnection();
nc.client = this;
nc.connect("rtmp://127.0.0.1/SimpleChatTest/");
nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
nc.call("callFromClient", myResponder, "Server");
}
private function netStatusHandler(event:NetStatusEvent):void{
switch(event.info.code){
case "NetConnection.Connect.Success":
trace("Success Connection!!");
// share chat contents
so = SharedObject.getRemote("simpleChat", nc.uri, false);
so.addEventListener(SyncEvent.SYNC, syncHandler);
so.connect(nc);
// share user count
soUserCount = SharedObject.getRemote("userCount", nc.uri, false);
soUserCount.addEventListener(SyncEvent.SYNC, syncHandler_UserCount);
soUserCount.connect(nc);
break;
}
}
private function syncHandler(event:SyncEvent):void{
if(so.data["simpleChat"] != undefined){
view.text += so.data["simpleChat"] + "\n";
}
}
private function syncHandler_UserCount(event:SyncEvent):void{
trace("soUserCount.data[userCount] in syncHandlerMethod : ", soUserCount.data["userCount"]);
viewCount.text = soUserCount.data["userCount"];
}
/* ServerSide에서 Client length가져오기*/
public function setUserCount(userCount:Number):void{
soUserCount.setProperty("userCount", userCount.toString());
}
// Responder function for nc.call()
private function onReply(result:Object):void
{
trace("result : " + result);
soUserCount.setProperty("userCount", result.toString());
trace("soUserCount.data[userCount] : ", soUserCount.data["userCount"]);
}
private function sendMessage():void{
so.setProperty("simpleChat", textInput.text);
textInput.text = "";
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:TextArea x="10" y="10" width="329" height="468" id="view"/>
<s:TextInput x="10" y="486" width="246" enter="sendMessage()" id="textInput"/>
<s:Button x="264" y="487" label="Send" click="sendMessage()"/>
<s:Label x="347" y="10" text="Total Users : "/>
<s:Label x="455" y="10" id="viewCount"/>
</s:Application>
I traced result of SharedObject.
Result
Success Connection!!
result : 1
soUserCount.data[userCount] 1
soUserCount.data[userCount] in syncHandlerMethod undefined --> why?
I can't understand why sharedobject's data is dissipated??
Thanks in advanced.
Kevin.
ps. I also wanna use SharedObject in Server Side. Unfortunately I tried tried tried tried many times... But I don't know how to do it. I'm newbie to flex&FMS. Is there some example about that?
