Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
180 lines (154 sloc) 4.23 KB
<?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"
backgroundColor="#FFFFFF"
backgroundAlpha="0"
creationComplete="init();">
<fx:Script>
<![CDATA[
import flash.events.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;
import WebSocket;
import com.adobe.serialization.json.*;
import mx.collections.ArrayCollection;
[Bindable]
private var msgBuffer: ArrayCollection;
private var websock:WebSocket;
private var sessionId:String;
private var frame:String = '~m~';
private function init():void {
msgBuffer = new ArrayCollection([]);
sessionId = null;
loadPolicyFile("xmlsocket://192.168.1.16:843");
websock = new WebSocket(this, "ws://192.168.1.16:8080/socket.io/flashsocket","");
websock.addEventListener("open", onOpen);
websock.addEventListener("close", onClose);
websock.addEventListener("message", onMessage);
}
public function onMessage(e:Event):void
{
var msgs:Array = websock.readSocketData();
for each (var msg:String in msgs) {
msg = decodeURIComponent(msg);
log("received string: "+msg);
if (msg.substr(0, 3) == frame)
{
var m:Array = msg.split(frame);
if (sessionId == null) {
sessionId = m[2];
log("session id= " + sessionId);
}
else
{
log(" m[2] = " +m[2]);
if (m[2].substr(0, 3) == '~h~')
{
log("heartbeat: " + msg);
sendHeartBeat(m[2].substr(3));
}
else
if (m[2].substr(0, 3) == '~j~')
{
log("json: " + msg);
parseMsg(m[2].substr(3));
}
else
{
parseMsg(m[2]);
log ("message: " + m[2]);
}
}
}
}
}
private function parseMsg(s:String):void
{
var msg:Object = JSON.decode(s);
if (msg.announcement != null) {
trace("announcement :" + msg.announcement);
addToList(msg.announcement);
}
else
if (msg.message != null)
{
addToList(msg.message[0]+" :"+msg.message[1]);
}
else
if (msg.buffer != null) {
for each (var o:Object in msg.buffer) {
addToList(o.message[0]+" : "+o.message[1]);
}
}
}
private function addToList(s:String):void {
msgBuffer.addItem(s);
messages.verticalScrollPosition = messages.maxVerticalScrollPosition;
}
public function sendMsg(m:String):void
{
websock.send(frame + m.length + frame + m);
addToList("me : "+m);
}
public function sendHeartBeat(num:String):void
{
var strnum:int = 3 + num.length;
websock.send(frame + strnum.toString() + frame+ '~h~' + num);
}
public function onClose(e:Event):void
{
log("websocket disconnected");
}
public function onOpen(e:Event):void
{
log("websocket connected");
//websock.send(frame+'17'+frame+"USERNAME:username");
}
private function sendMessage():void {
sendMsg(msgbox.text);
msgbox.text="";
}
public function getCallerHost():String {
return ("http://192.168.1.16");
}
public function getOrigin():String {
return ("http://192.168.1.16");
}
public function fatal(str:String):void
{
trace(str);
}
public function error(str:String):void
{
trace(str);
}
public function log(str:String):void
{
trace(str);
}
public function loadPolicyFile(url:String):void
{
log("policy file: " + url);
Security.loadPolicyFile(url);
}
public function getCookie():String
{
return sessionId;
}
private function msgboxKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.ENTER)
sendMessage();
}
]]>
</fx:Script>
<mx:VBox width="100%" height="500">
<mx:List id="messages" width="100%" height="400" dataProvider="{msgBuffer}" />
<mx:HBox>
<mx:TextInput id="msgbox" height="50" width="650" keyDown="msgboxKeyDown(event)" />
<mx:Button id="btn" click="sendMessage()" height="50" width="50" />
</mx:HBox>
</mx:VBox>
</s:WindowedApplication>