I want to add a simple chat application functionality in my app which is made in action script 3.0. I did some research but didn't understand how data will flow and application will be triggered. I also don't know what type of resources is required to implement chat app. Any good latest tutorial in this regard will be really helpful for me.
Asked
Active
Viewed 2,431 times
2 Answers
1
You should check SmartFoxServer. I did similar applications and their solution (there is a free version as well) is very good, also, there is a lot of examples and a very good and participative support.
- Examples (including a chat application);
- Documentation;
- Release Notes.
You can try your own solution as well, check the flash.net.Socket class. I also found a good example here
-
is this only way to implement? Can i use socket program to do this task? – kaif Sep 11 '14 at 11:39
-
There is several ways to implement it, it's just one suggestion. SmartFoxServer is a Java socket server. – gabriel Sep 11 '14 at 11:45
-
ok gPearl.I have php server so that's why i want to implement chat using socket.Can you describe me the data flow to implement using socket. – kaif Sep 11 '14 at 12:08
-
I have edited my answer and added more details. I believe will be possible to give you more grounding – gabriel Sep 11 '14 at 12:14
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;
}
}
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();
}

Karthik
- 284
- 2
- 4
- 12