powered by Jive Software

Working Flash Chat code as3 (Not Flex)


#1

Hi, I tried to find a working code for Flash AS3 that can send and receive a chat message from a server (Ex. talk.google.com) but I couldn’t find any

I am a beginner so even with full XIFF 3.0.0 Flash XMPP client library I am not able to implement a working environment.

Right now I have implemented this Chat with PHP, but our client is not supporting the PHP now so have to come up with a better solution.

I did find some information regarding Flex but None for Flash as3 . I have to finish this work by next week at most…

Please some one guide me to the correct resource.

Thanks in advance

By the way really appreciate the efforts you people are putting in this development… Great work keepit up Cheers


#2

Hi there and welcome!

XIFF 3 is an AS3-only library, no Flex references are included.

Now as far as AS3-only examples of using XIFF, we haven’t yet included any in the source.

However, XIFFGUI (a Flex project that is included) is a great place to look to begin using the library. The mxml files are mainly there for the view, you can look at the ActionScript classes for most of the logic.

Make sure you read the Getting Started Guide here: http://community.igniterealtime.org/docs/DOC-2101.

That will show you how to download the source code and examples and get up and running.

Hope that helps!


#3

Thanks for the quick response

By the way I am working on the same. Please add a Flash project also, which can help some one like me with very less or no exp at all

Thanks again


#4

Hi @Mark Walters,

I was able to convert the Flex project to a Flash one but not completely tho

here it is located.(Original) http://paazio.wippiespace.com/xiff-gui-v2.1/

I need a small help in that. I am able to connect to an XMPP server, recieve the Chat messages that some one has sent me but I am not able to send one back

I need a simple code wich can accomplish this task.

If you see the last but one line under code secton you will come to know what I am looking for.

Here is the complete code:

//****************************************************************************** ******

import flash.events.Event;

import com.yourpalmark.chat.ChatManager;

import com.yourpalmark.chat.data.LoginCredentials;

import com.pixelbreaker.ui.osx.MacMouseWheel;

import mx.collections.ArrayCollection;

import mx.events.CloseEvent;

import mx.events.FlexEvent;

import mx.events.ListEvent;

import mx.managers.PopUpManager;

import mx.events.ResizeEvent;

import org.igniterealtime.xiff.conference.Room;

import org.igniterealtime.xiff.core.EscapedJID;

import org.igniterealtime.xiff.core.UnescapedJID;

import org.igniterealtime.xiff.data.Message;

import org.igniterealtime.xiff.data.Presence;

import org.igniterealtime.xiff.data.im.RosterGroup;

import org.igniterealtime.xiff.data.im.RosterItemVO;

import org.igniterealtime.xiff.events.DisconnectionEvent;

import org.igniterealtime.xiff.events.IncomingDataEvent;

import org.igniterealtime.xiff.events.InviteEvent;

import org.igniterealtime.xiff.events.LoginEvent;

import org.igniterealtime.xiff.events.MessageEvent;

import org.igniterealtime.xiff.events.OutgoingDataEvent;

import org.igniterealtime.xiff.events.PresenceEvent;

import org.igniterealtime.xiff.events.RegistrationSuccessEvent;

import org.igniterealtime.xiff.events.RosterEvent;

import org.igniterealtime.xiff.events.XIFFErrorEvent;

import mx.controls.ComboBox;

import org.igniterealtime.xiff.core.XMPPConnection;

import flash.net.XMLSocket;

var chatManager:ChatManager;

var messageDict:Dictionary;

//var roomPopup:RoomPopup;

var errorDataProvider:ArrayCollection = new ArrayCollection();

var logDataProvider:ArrayCollection = new ArrayCollection();

var presenceDataProvider:Array = [

{ label:“Online”, data:Presence.SHOW_CHAT },

{ label:“Free To Chat”, data:Presence.SHOW_CHAT },

{ label:“Away”, data:Presence.SHOW_AWAY },

{ label:“Do Not Disturb”, data:Presence.SHOW_DND },

{ label:“Extended Away”, data:Presence.SHOW_XA }

];

var groupDataProvider:Array = [

{ label:“Buddies”, data:“Buddies” },

{ label:“Work”, data:“Work” },

{ label:“Family”, data:“Family” }

];

var instructionText:String = “Enter your username and password and press connect to log in.\n”

  • “Username should be a full JID (username@domain).\n”

  • “Login server and port are used if you fill them in. For example Google Talk uses talk.google.com.\n”

  • “If you are not registered, enter a username and password and press register.\n”

  • “Check the error log to determine if there was a conflict (ie. someone already has your ID).\n”

  • “Once you have successfully registered you may log in.”;

function clearLog():void

{

logDataProvider.removeAll();

}

setupChatManager();

function setupChatManager():void

{

chatManager = new ChatManager();

chatManager.addEventListener( DisconnectionEvent.DISCONNECT, onDisconnect );

chatManager.addEventListener( LoginEvent.LOGIN, onLogin );

chatManager.addEventListener( XIFFErrorEvent.XIFF_ERROR, onXIFFError );

chatManager.addEventListener( OutgoingDataEvent.OUTGOING_DATA, onOutgoingData );

chatManager.addEventListener( IncomingDataEvent.INCOMING_DATA, onIncomingData );

chatManager.addEventListener( RegistrationSuccessEvent.REGISTRATION_SUCCESS, onRegistrationSuccess );

chatManager.addEventListener( PresenceEvent.PRESENCE, onPresence );

chatManager.addEventListener( MessageEvent.MESSAGE, onMessage );

chatManager.addEventListener( InviteEvent.INVITED, onInvited );

chatManager.addEventListener( RosterEvent.ROSTER_LOADED, onRosterLoaded );

chatManager.addEventListener( RosterEvent.SUBSCRIPTION_DENIAL, onSubscriptionDenial );

chatManager.addEventListener( RosterEvent.SUBSCRIPTION_REQUEST, onSubscriptionRequest );

chatManager.addEventListener( RosterEvent.SUBSCRIPTION_REVOCATION, onSubscriptionRevocation );

chatManager.addEventListener( RosterEvent.USER_AVAILABLE, onUserAvailable );

chatManager.addEventListener( RosterEvent.USER_UNAVAILABLE, onUserUnavailable );

}

function setupDataProviders():void

{

//errorLogGrid.dataProvider = errorDataProvider;

//logList.dataProvider = logDataProvider;

//updateGroupBuddyComboBox.dataProvider = chatManager.roster;

//removeBuddyComboBox.dataProvider = chatManager.roster;

//groupComboBox.dataProvider = groupDataProvider;

}

connectButton.addEventListener(MouseEvent.CLICK,LoginNow);

function LoginNow(e){

connect();

}

function connect():void

{

var credentials:LoginCredentials = new LoginCredentials();

credentials.username = usernameInput.text;

credentials.password = passwordInput.text;

msgbox.text+=String(“vinod”);

chatManager.connect(credentials);

}

function addPersonalPopupWindow( userJID:EscapedJID, userPresence:String, userStatus:String, message:Message=null ):void

{

//var popup:PersonalMessagePopup = new PersonalMessagePopup();

//popup.addEventListener( MessageEvent.MESSAGE, onPopupMessage );

//popup.addEventListener( CloseEvent.CLOSE, onPopupClose );

//popup.userPresence = userPresence;

//popup.userStatus = userStatus;

//popup.fromJID = chatManager.currentUserJID;

//popup.toJID = userJID;

//if( message )

//{

// popup.updateMessage( message );

//}

//PopUpManager.addPopUp( popup, this, false );

//PopUpManager.centerPopUp( popup );

//messageDict[ userJID.bareJID ] = popup;

}

function createMUC():void

{

//roomPopup = new RoomPopup();

//roomPopup.addEventListener( XIFFErrorEvent.XIFF_ERROR, onXIFFError );

//roomPopup.chatManager = chatManager;

//PopUpManager.addPopUp( roomPopup, this );

//PopUpManager.centerPopUp( roomPopup );

}

function updateLogScrollPosition():void

{

logList.verticalScrollPosition = logList.maxVerticalScrollPosition;

}

function onInit( event:FlexEvent ):void

{

setupChatManager();

messageDict = new Dictionary();

instructions.text = instructionText;

setupDataProviders();

}

function onAddedToStage( event:Event ):void

{

MacMouseWheel.setup( stage );

}

function onResize( event:ResizeEvent ):void

{

//if( roomPopup ) roomPopup.invalidateSize();

}

function onDisconnect( event:DisconnectionEvent ):void

{

createMUCButton.enabled = false;

groupComboBox.enabled = false;

//rosterGrid.dataProvider = null;

updateGroupBuddyComboBox.enabled = false;

removeBuddyComboBox.enabled = false;

presenceStateComboBox.enabled = false;

disconnectButton.enabled = false;

connectButton.enabled = true;

registerButton.enabled = true;

}

function onLogin( event:LoginEvent ):void

{

msgbox.text+=“LoginEvent:”+String(chatManager.currentUserJID);

//createMUCButton.enabled = true;

//groupComboBox.enabled = true;

//updateGroupBuddyComboBox.enabled = true;

//removeBuddyComboBox.enabled = true;

//presenceStateComboBox.enabled = true;

//presenceStateComboBox.dataProvider = presenceDataProvider;

//disconnectButton.enabled = true;

//connectButton.enabled = false;

//registerButton.enabled = false;

}

function onXIFFError( event:XIFFErrorEvent ):void

{

msgbox.text+=“Error:”+String(event.errorMessage);

//var error:Object = { errorCode:event.errorCode, errorCondition:event.errorCondition, errorMessage:event.errorMessage, errorType:event.errorType };

//errorDataProvider.addItem( error );

//logDataProvider.addItem( "XIFFErrorEvent " + “onXIFFError: " + “type:” + event.errorType + " message:” + event.errorMessage );

//callLater( updateLogScrollPosition );

}

function onOutgoingData( event:OutgoingDataEvent ):void

{

logDataProvider.addItem( ">>outgoing: " + event.data.toString() );

//callLater( updateLogScrollPosition );

}

function onIncomingData( event:IncomingDataEvent ):void

{

logDataProvider.addItem( ">>incoming: " + event.data.toString() );

//callLater( updateLogScrollPosition );

}

function onRegistrationSuccess( event:RegistrationSuccessEvent ):void

{

connect();

//var generalPopup:GeneralPopup = new GeneralPopup();

//generalPopup.message = “Registration was successful. You will now be logged in. You may want to add some contacts at this point.”;

//PopUpManager.addPopUp( generalPopup, this );

//PopUpManager.centerPopUp( generalPopup );

}

function onPresence( event:PresenceEvent ):void

{

var presence:Presence = event.data[ 0 ] as Presence;

var presenceType:String;

switch( presence.type )

{

case Presence.SHOW_CHAT :

case Presence.SHOW_AWAY :

case Presence.SHOW_DND :

case Presence.SHOW_XA :

presenceType = “Presence.AVAILABLE_TYPE”;

break;

case Presence.UNAVAILABLE_TYPE :

presenceType = “Presence.UNAVAILABLE_TYPE”;

break;

case Presence.UNSUBSCRIBE_TYPE :

presenceType = “Presence.UNSUBSCRIBE_TYPE”;

break;

case Presence.PROBE_TYPE :

presenceType = “Presence.PROBE_TYPE”;

break;

default :

break;

}

logDataProvider.addItem( "PresenceEvent " + presenceType + ": " + presence.from );

//callLater( updateLogScrollPosition );

}

function onMessage( event:MessageEvent ):void

{

var message:Message = event.data as Message;

msgbox.text+=“Message:”+String(event.data.body)+" $ "+String(event.data.from.bareJID)+ " # "+String(event.type);

/*

if( message.type == Message.CHAT_TYPE )

{

if( messageDict[ message.from.bareJID ] )

{

//var popup:PersonalMessagePopup = messageDict[ message.from.bareJID ] as PersonalMessagePopup;

//popup.updateMessage( message );

}

else

{

try

{

var ri:RosterItemVO = RosterItemVO.get( message.from.unescaped, false );

//addPersonalPopupWindow( message.from, ri.show, ri.status, message );

}

catch( event:Error )

{

logDataProvider.addItem( “Message from a user not in roster.” );

}

}

}

*/

//callLater( updateLogScrollPosition );

}

function onInvited( event:InviteEvent ):void

{

//var acceptDeclinePopup:AcceptDeclinePopup = new AcceptDeclinePopup();

//acceptDeclinePopup.additionalData = { room:event.room, reason:event.reason, from:event.from };

//acceptDeclinePopup.message = event.from + " requests you to join a room. Reason: " + event.reason;

//acceptDeclinePopup.addEventListener( AcceptDeclinePopup.ACCEPT, onInviteAccept );

//acceptDeclinePopup.addEventListener( AcceptDeclinePopup.DECLINE, onInviteDecline );

//PopUpManager.addPopUp( acceptDeclinePopup, this );

//PopUpManager.centerPopUp( acceptDeclinePopup );

//logDataProvider.addItem( ">>handle Invitation to room: " + ( Room( event.room ).roomJID as UnescapedJID ).bareJID );

//callLater( updateLogScrollPosition );

}

function onInviteAccept( event:Event ):void

{

//var acceptDeclinePopup:AcceptDeclinePopup = event.target as AcceptDeclinePopup;

//var room:Room = acceptDeclinePopup.additionalData.room as Room;

//PopUpManager.removePopUp( acceptDeclinePopup );

//roomPopup = new RoomPopup();

//roomPopup.moderator = false;

//roomPopup.chatManager = chatManager;

//roomPopup.room = room;

//PopUpManager.addPopUp( roomPopup, this );

//PopUpManager.centerPopUp( roomPopup );

}

function onInviteDecline( event:Event ):void

{

//var acceptDeclinePopup:AcceptDeclinePopup = event.target as AcceptDeclinePopup;

//var room:Room = acceptDeclinePopup.additionalData.room as Room;

//room.decline( room.userJID, “I do not want to join” );

//PopUpManager.removePopUp( acceptDeclinePopup );

}

function onRosterLoaded( event:RosterEvent ):void

{

//rosterGrid.dataProvider = chatManager.roster;

}

function onSubscriptionDenial( event:RosterEvent ):void

{

//var generalPopup:GeneralPopup = new GeneralPopup();

//generalPopup.message = event.jid + " denied you request to subscribe to their presence."

//PopUpManager.addPopUp( generalPopup, this );

//PopUpManager.centerPopUp( generalPopup );

}

function onSubscriptionRequest( event:RosterEvent ):void

{

if( !chatManager.roster.contains( RosterItemVO.get( event.jid, false ) ) )

{

//var acceptDeclinePopup:AcceptDeclinePopup = new AcceptDeclinePopup();

//acceptDeclinePopup.addEventListener( AcceptDeclinePopup.ACCEPT, onSubscriptionAccept );

//acceptDeclinePopup.addEventListener( AcceptDeclinePopup.DECLINE, onSubscriptionDecline );

//acceptDeclinePopup.jid = event.jid.escaped;

//acceptDeclinePopup.message = “The user " + event.jid + " wishes to add you to their roster and see your presence online.”;

//PopUpManager.addPopUp( acceptDeclinePopup, this, false );

//PopUpManager.centerPopUp( acceptDeclinePopup );

}

}

function onSubscriptionAccept( event:Event ):void

{

//var acceptDeclinePopup:AcceptDeclinePopup = event.target as AcceptDeclinePopup;

//chatManager.roster.grantSubscription( acceptDeclinePopup.jid.unescaped, true );

//PopUpManager.removePopUp( acceptDeclinePopup );

}

function onSubscriptionDecline( event:Event ):void

{

//var acceptDeclinePopup:AcceptDeclinePopup = event.target as AcceptDeclinePopup;

//chatManager.roster.denySubscription( acceptDeclinePopup.jid.unescaped );

//PopUpManager.removePopUp( acceptDeclinePopup );

}

function onSubscriptionRevocation( event:RosterEvent ):void

{

logDataProvider.addItem( "RosterEvent.SUBSCRIPTION_REVOCATION: " + event.jid.toString() );

//callLater( updateLogScrollPosition );

}

function onUserAvailable( event:RosterEvent ):void

{

logDataProvider.addItem( "RosterEvent.USER_AVAILABLE: " + event.jid );

//callLater( updateLogScrollPosition );

}

function onUserUnavailable( event:RosterEvent ):void

{

logDataProvider.addItem( "RosterEvent.USER_UNAVAILABLE: " + event.jid );

//callLater( updateLogScrollPosition );

}

function onPopupMessage( event:MessageEvent ):void

{

msgbox.text+=“popUp:”+String(event.data);

var message:Message = event.data as Message;

chatManager.connection.send( message );

}

function onPopupClose( event:CloseEvent ):void

{

//var popup:PersonalMessagePopup = event.target as PersonalMessagePopup;

//delete messageDict[ popup.toJID.bareJID ];

//PopUpManager.removePopUp( popup );

}

function onServerInputChange():void

{

ChatManager.serverName = serverInput.text;

}

function onServerPortInputChange():void

{

ChatManager.serverPort = parseInt(serverPortInput.text);

}

function onUpdateGroupBuddyChange( event:ListEvent ):void

{

var rosterItem:RosterItemVO = RosterItemVO( updateGroupBuddyComboBox.selectedItem );

var groups:Array = chatManager.roster.getContainingGroups( rosterItem );

var group:RosterGroup = RosterGroup( groups[ 0 ] );

for( var i:int=0; i<groupDataProvider.length; i++ )

{

var groupObject:Object = groupDataProvider[ i ];

var groupLabel:String = groupObject.label;

if( groupLabel == group.label )

{

groupComboBox.selectedIndex = i;

break;

}

}

}

function onPresenceStateChange( event:ListEvent ):void

{

chatManager.updatePresence( presenceStateComboBox.value.toString(), presenceStateComboBox.text );

}

function onConnectClick( event:MouseEvent ):void

{

if (serverInput.text == “”)

{

serverInput.text = usernameInput.text.split("@").pop();

ChatManager.serverName = serverInput.text;

}

connect();

}

function onCreateMUCClick( event:MouseEvent ):void

{

createMUC();

}

function onClearLogClick( event:MouseEvent ):void

{

clearLog();

}

function onRegisterClick( event:MouseEvent ):void

{

var credentials:LoginCredentials = new LoginCredentials();

credentials.username = usernameInput.text;

credentials.password = passwordInput.text;

chatManager.register( credentials );

}

function onDisconnectClick( event:MouseEvent ):void

{

chatManager.disconnect();

}

function onRosterItemClick( event:ListEvent ):void

{

var rosterItem:RosterItemVO = chatManager.roster.getItemAt( event.rowIndex ) as RosterItemVO;

if( messageDict[ rosterItem.jid.bareJID ] )

{

//var win:PersonalMessagePopup = messageDict[ rosterItem.jid.bareJID ] as PersonalMessagePopup;

//PopUpManager.bringToFront( win );

}

else

{

addPersonalPopupWindow( rosterItem.jid.escaped, rosterItem.show, rosterItem.status );

}

}

function onUpdateGroupClick( event:MouseEvent ):void

{

if( updateGroupBuddyComboBox.selectedIndex != -1 )

{

var rosterItem:RosterItemVO = RosterItemVO( updateGroupBuddyComboBox.selectedItem );

chatManager.updateGroup( rosterItem, groupComboBox.selectedLabel );

updateGroupBuddyComboBox.selectedIndex = 0;

}

else

{

//var generalPopup:GeneralPopup = new GeneralPopup();

//generalPopup.message = “You must select a buddy to update their group.”;

//PopUpManager.addPopUp( generalPopup, this );

//PopUpManager.centerPopUp( generalPopup );

}

}

function onAddBuddyClick( event:MouseEvent ):void

{

var jid:UnescapedJID = new UnescapedJID( buddyJIDInput.text );

var message:String;

if( ChatManager.isValidJID( jid.escaped ) )

{

message = “A request has been sent to the user. You will see them online if they accept your request.”;

chatManager.addBuddy( jid.escaped );

}

else

{

message = “The JID of the user you tried to add did not validate. Try just adding nick.”;

}

//var generalPopup:GeneralPopup = new GeneralPopup();

//generalPopup.message = message;

//PopUpManager.addPopUp( generalPopup, this );

//PopUpManager.centerPopUp( generalPopup );

}

function onRemoveBuddyClick( event:MouseEvent ):void

{

if( removeBuddyComboBox.selectedIndex != -1 )

{

var rosterItem:RosterItemVO = RosterItemVO( removeBuddyComboBox.selectedItem );

chatManager.removeBuddy( rosterItem );

removeBuddyComboBox.selectedIndex = 0;

}

else

{

//var generalPopup:GeneralPopup = new GeneralPopup();

//generalPopup.message = “You must select a buddy to remove.”;

//PopUpManager.addPopUp( generalPopup, this );

//PopUpManager.centerPopUp( generalPopup );

}

}

//////////////////////////////////

sendButton.addEventListener(MouseEvent.CLICK,sendMessageNow)

function sendMessageNow(e){

msgbox.text+=“Send click”;

sendMessage(“v4@server”,“v5@server”,“Body message”);

}

// I will be calling this function from different popup windows to send the chat message//

function sendMessage(fromId,toId,str,){

"<<<<<<Here I want to put the code to send the message to the chat server>>>>>>";

}

/*****************************************

Thanks in advance


#5

Hi,

At last I got the solution

Incase if any one interested, here is the complete code for you to work with

This is a very simple basic (FLASH) Chat application, where you can login and send/recieve message. All of that is displayed in log.

Please see the code to build custom popup windows and more.

No group chat/add user/block user/register is included. (Which will be updated later)

Please include framework.swc in the project so that you can access some of the features that are available only to Flex but needed in this project.

You can find the steps to include framework.swc of Flex in Flash here.(Thanks to Tareq AlJaber). I have also included the framework.swc with my source.

My special thanks to “@Mark Walters” and “Igniterealtime” team for providing such a wonderful library and support.

Also cannot wait to mention “http://paazio.wippiespace.com/xiff-gui-v2.1/” where I got the original source code.

Thanks a ton for every one
Flash Chat XIFF.zip (3339998 Bytes)


#6

FYI, you are referencing an older XIFF GUI example at Juga’s website.

To work with the current library, you should reference the XIFF GUI included in the examples directory. It gets updated as the library gets updated.

The getting started doc that I referenced above explains how to work with the examples.

I wasn’t able to open up your Flash file to look at what code is in the actual .fla (I have CS4), but unfortunately you are kind of defeating the purpose of making a Flash project if you are including Flex’s framework.swc. That swc is most of the weight of the Flex framework, so you might as well be using Flex.

To make a true Flash (AS3-only) project, it should not have any Flex references. Which is why we worked so hard to pull out all of the Flex references from the library.

To write an AS3-only Flash project based on XIFF GUI, you really should just need to create a new view in Flash (not using any of the Flex components or classes, ie the mx classes), but still be able to use most of the logic classes like ChatManager, etc without much change.

Hope that helps.


#7

Hi Mark,

Yes I totally agree with you. But as there are no working examples of Flash yet and I am busy with my project here, I just wanted to post something for otheres who are in need of a quick solution in Flash.

Will work with the new Lib and upload the same soon

By the way that source is from CS5. Will try to upload one with CS4.

Hope its ok.

Cheers


#8

Hi Mark,

Any help on File transfer using the chat?

When I try to send a file from Spark to XIFF …

on Incoming Data I am getting:
Sendinghttp://jabber.org/protocol/bytestreamshttp://jabber.org/protocol/ibb

I dont know how to accept the request from XIFF. Any infor would really help

Thanks again


#9

A bit late in the discussion but XIFF does not support yet the feature that your incoming data contains, namely SI File Transfer.

It is in the roadmap, perhaps in 3.2.0 or later.

It has a feature request:

http://issues.igniterealtime.org/browse/XIFF-92

that depends of

http://issues.igniterealtime.org/browse/XIFF-90