Your First Union Application with MXML
forked from colin challenge for professionals (diff: 185)
ActionScript3 source code
/**
* Copyright paq ( http://wonderfl.net/user/paq )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3j1j
*/
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="465" height="465" applicationComplete="init()" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#444444, #1B1B1B]" color="#000000">
<mx:Script>
<![CDATA[
/*
* 特に機能のないチャットです。
* Union Platformの参考にどうぞ。
*
* 名前の表示がおかしかったので修正しました。
*/
import mx.collections.ArrayCollection;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.ui.Keyboard;
import net.user1.reactor.IClient;
import net.user1.reactor.Room;
import net.user1.reactor.Reactor;
import net.user1.reactor.ReactorEvent;
protected var _reactor:Reactor;
protected var _chatRoom:Room;
private var _message:ArrayCollection = new ArrayCollection();
private var _number:uint;
public function init():void
{
_reactor = new Reactor();
_reactor.addEventListener(ReactorEvent.READY,readyListener);
_reactor.connect("tryunion.com", 9100);
outgoingMessages.addEventListener(KeyboardEvent.KEY_UP,keyUpListener);
}
protected function readyListener(e:ReactorEvent):void
{
_message.addItem({No:_number, Name:"System", Comment:"Connected to Union!"});
_chatRoom = _reactor.getRoomManager().createRoom("chatRoom");
_chatRoom.addMessageListener("CHAT_MESSAGE",chatMessageListener);
_chatRoom.join();
datagrid.dataProvider=_message;
_number++;
}
protected function keyUpListener(e:KeyboardEvent):void
{
if (e.keyCode == Keyboard.ENTER) {
_chatRoom.sendMessage("CHAT_MESSAGE",true,null,outgoingMessages.text);
outgoingMessages.text = "";
}
}
protected function nameChangeListener():void {
var self:IClient;
self = _reactor.getClientManager().self();
self.setAttribute("username", nameInput.text);
}
protected function chatMessageListener(fromClient:IClient, messageText:String):void
{
var username:String = getUserName(fromClient);
_message.addItem({No:_number, Name:username, Comment:messageText});
_number++;
}
protected function getUserName(client:IClient):String
{
var username:String = client.getAttribute(null, "username");
if (username == null)
{
return "Guest" + client.getClientID();
}
else
{
return username;
}
}
]]>
</mx:Script>
<mx:DataGrid x="10" y="46" width="445" height="349" id="datagrid">
<mx:columns>
<mx:DataGridColumn headerText="No." dataField="No" width="30"/>
<mx:DataGridColumn headerText="Name" dataField="Name" width="100"/>
<mx:DataGridColumn headerText="Comment" dataField="Comment"/>
</mx:columns>
</mx:DataGrid>
<mx:TextInput x="119" y="428" width="336" id="outgoingMessages" tabIndex="2"/>
<mx:TextInput x="10" y="428" width="101" id="nameInput" tabIndex="1" change="nameChangeListener()"/>
<mx:Label x="10" y="406" text="Name:" color="#FFFFFF"/>
<mx:Label x="119" y="406" text="Comment:" color="#FFFFFF"/>
<mx:Label x="10" y="10" text="Union Platfrom Sample" width="270.55" height="28" fontSize="20" color="#FFFFFF" fontFamily="Arial" textDecoration="normal"/>
</mx:Application>