package { import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.*; import flash.ui.Keyboard; import flash.system.ApplicationDomain; import mx.core.Singleton; import org.jivesoftware.xiff.core.*; import org.jivesoftware.xiff.data.*; import org.jivesoftware.xiff.events.*; [SWF( backgroundColor = '0x140E12', frameRate = '33', width = '300', height = '400')] public class TestChatBOSH extends Sprite { private var connection:XMPPBOSHConnection; private var presence:Presence; private var keepAlive:Timer; //private var port:Number = 5222; private var port:Number = 8001; private var resourceName:String = "flashPlayer"; private var server:EscapedJID = new EscapedJID("localhost"); private var userJID:EscapedJID = new EscapedJID("user1@" + server.toString()); private var recipient:EscapedJID = new EscapedJID("user2@" + server.toString()); private var msgOutput:TextField; private var msgInput:TextField; public function TestChatBOSH() { // http://www.igniterealtime.org/community/thread/34519 // Do what FlexModuleFactory does, only by hand. var resourceManagerImpl:Object = flash.system.ApplicationDomain.currentDomain.getDefinition("mx.resources::ResourceManagerImpl"); Singleton.registerClass("mx.resources::IResourceManager", Class(resourceManagerImpl)); stage.align = StageAlign.TOP_LEFT; stage.scaleMode = StageScaleMode.NO_SCALE; createElements(); initChat(); } private function createElements():void { msgOutput = new TextField(); msgOutput.background = true; msgOutput.backgroundColor = 0xF4F4F4; msgOutput.textColor = 0x042444; msgOutput.mouseWheelEnabled = true; msgOutput.multiline = true; msgOutput.width = stage.stageWidth - 2; msgOutput.height = stage.stageHeight - 32; msgOutput.x = 1; msgOutput.y = 1; addChild(msgOutput); msgInput = new TextField(); msgInput.background = true; msgInput.backgroundColor = 0xF4F4F4; msgInput.textColor = 0x042444; msgInput.type = "input"; msgInput.width = stage.stageWidth - 2; msgInput.height = 29; msgInput.x = 1; msgInput.y = stage.stageHeight - 30; msgInput.maxChars = 40; msgInput.addEventListener(KeyboardEvent.KEY_UP, onKeyUp); addChild(msgInput); } private function addMessage(from:EscapedJID, msg:String):void { var date:Date = new Date(); msgOutput.appendText(from.node + " [" + date.getHours() + ":" + date.getMinutes() + "] : " + msg + "\n"); } private function onKeyUp(evt:KeyboardEvent):void { switch(evt.keyCode) { case Keyboard.ENTER : messageSend(); break; } } private function messageSend():void { var txt:String = msgInput.text; var msg:Message = new Message(recipient, null, txt, null, Message.CHAT_TYPE); if (connection.isLoggedIn()) { connection.send(msg); msgInput.text = ""; addMessage(userJID, txt); } } private function initChat():void { connection = new XMPPBOSHConnection(); connection.addEventListener(ConnectionSuccessEvent.CONNECT_SUCCESS, onConnectSuccess); connection.addEventListener(DisconnectionEvent.DISCONNECT, onDisconnect); connection.addEventListener(XIFFErrorEvent.XIFF_ERROR, onXiffError); connection.addEventListener(LoginEvent.LOGIN, onLogin); connection.addEventListener(MessageEvent.MESSAGE, onMessage); connection.addEventListener(PresenceEvent.PRESENCE, onPresence); connection.useAnonymousLogin = false; connection.server = server.toString(); connection.username = "user1"; connection.password = "user1"; connection.port = port; connection.boshPath = "xmpp-httpbind/"; connection.resource = resourceName; connection.connect("flash"); keepAlive = new Timer(100000); keepAlive.addEventListener(TimerEvent.TIMER, onKeepAliveLoop); keepAlive.start(); //presence = new Presence(null, userJID, Presence.AVAILABLE_TYPE, Presence.SHOW_NORMAL, null, 1); presence = new Presence(null, userJID); } private function onConnectSuccess(evt:ConnectionSuccessEvent):void { trace("onConnectSuccess " + getTimer()); } private function onDisconnect(evt:DisconnectionEvent):void { trace("onDisconnect " + getTimer()); } private function onXiffError(evt:XIFFErrorEvent):void { trace("onXiffError " + getTimer()); trace("onXiffError: " + evt.toString()); } private function onLogin(evt:LoginEvent):void{ trace("onLogin " + getTimer()); connection.send(presence); } private function onMessage(evt:MessageEvent):void { trace("onMessage " + getTimer()); trace(evt.data.time); addMessage(evt.data.from, evt.data.body); } private function onPresence(evt:PresenceEvent):void { trace("onPresence " + getTimer()); } private function onKeepAliveLoop(evt:TimerEvent):void { connection.sendKeepAlive(); } } }