Komak57

LoLChat.mxml

Oct 13th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.                xmlns:s="library://ns.adobe.com/flex/spark"
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="200" minHeight="100"
  5.                applicationComplete="OnLoad()">
  6.     <fx:Script>
  7.         <![CDATA[
  8.             import flash.events.*;
  9.             import flash.external.ExternalInterface;
  10.             import flash.media.Microphone;
  11.            
  12.             import mx.collections.XMLListCollection;
  13.             import mx.controls.*;
  14.             import mx.events.*;
  15.             import mx.managers.PopUpManager;
  16.             import mx.utils.Base64Decoder;
  17.             import mx.utils.Base64Encoder;
  18.            
  19.             private var DOUBLE_CLICK_SPEED:int = 10;
  20.            
  21.             private var mic:Microphone = Microphone.getMicrophone();
  22.             private var last:String = "";
  23.             private var selected:String = "";
  24.             private var lastTime:Date = new Date();
  25.             private var delay:int = 1000; // 1 second buffer
  26.             private var _delay:int = 0;
  27.            
  28.             private var server:String = "66.168.187.147";
  29.             private var TextSock:Socket;
  30.             private var VoiceSock:Socket;
  31.             private var Nick:String = "Guest";
  32.             private var MyRoom:String = null;
  33.             private var sound:Sound = null;
  34.             private var sound_Playing:Boolean = false;
  35.             private var _vbuf:ByteArray = new ByteArray();
  36.            
  37.            
  38.             [Bindable]
  39.             private var Rooms:XML =
  40.                 <list>
  41.                     <room title="Offline" code="100"></room><room title="Debug" code="100"></room>
  42.                 </list>;
  43.             [Bindable]
  44.             private var Rooms_Data:XMLListCollection =
  45.                 new XMLListCollection(Rooms.room);
  46.            
  47.             private function treeLabel(item:Object):String {
  48.                 var node:XML = item as XML;
  49.                 if( node.localName() == "room" )
  50.                     return node.@title;
  51.                 else if( node.localName() == "user" )
  52.                     return node.@nick;
  53.                 else
  54.                     return "NULL";
  55.             }
  56.                    
  57.             private function OnLoad():void
  58.             {
  59.                 //try {
  60.                     //Security.loadPolicyFile("xmlsocket://"+server+":843/");
  61.                     TextSock = new Socket();
  62.                     TextSock.addEventListener(Event.CONNECT, textConnect);
  63.                     TextSock.addEventListener(Event.CLOSE, textDisconnect);
  64.                     TextSock.addEventListener(ProgressEvent.SOCKET_DATA, textReceive);
  65.                    
  66.                     //Security.loadPolicyFile("xmlsocket://"+server+":"+portNumber+"/");
  67.                     TextSock.connect(server, 843);
  68.                    
  69.                     ChatTree.addEventListener( MouseEvent.DOUBLE_CLICK, dblClick );
  70.                     ChatTree.doubleClickEnabled = true;
  71.                     mic.addEventListener(StatusEvent.STATUS, this.onMicStatus);
  72.                     mic.addEventListener(ActivityEvent.ACTIVITY, this.onMicActivity);
  73.                     mic.addEventListener(SampleDataEvent.SAMPLE_DATA, this.micParse);
  74.                     mic.setUseEchoSuppression(true);
  75.                     mic.gain = 100;
  76.                     mic.rate = 44;
  77.                     mic.setSilenceLevel(voicelimit.value, 1);
  78.                     if (mic.muted) {
  79.                         btn_mute.label = "UnMute";
  80.                         Security.showSettings(SecurityPanel.PRIVACY);
  81.                         if (!mic.muted)
  82.                             btn_mute.label = "Mute";
  83.                     }
  84.                     // load post variables
  85.                     var tmp:String = LoaderInfo(this.root.loaderInfo).parameters.name as String;
  86.                     if (tmp != null) {
  87.                         Nick = tmp;
  88.                         Nickname.text = Nick;
  89.                     } else {
  90.                         mx.controls.Alert.show("Could not grab Nick");
  91.                     }
  92.                     //Rooms = new XML("<list><room title=\"Lobby\"></room><room title=\"Alt Lobby\"></room><room title=\"Afk\"><user nick=\"Dummy\"/></room></list>");
  93.                     //Rooms_Data = new XMLListCollection(Rooms.room);
  94.                 //} catch (error:Error) {
  95.                 //  Alert.show(error.toString(), "OnLoad Error");  
  96.                 //}
  97.                 //playSound(null);
  98.             }
  99.            
  100.             private function onMicStatus(event:StatusEvent):void
  101.             {
  102.                 try {
  103.                 if (event.code == "Microphone.Unmuted")
  104.                 {
  105.                     btn_mute.label = "Mute";
  106.                 }  
  107.                 else if (event.code == "Microphone.Muted")
  108.                 {
  109.                     miclevel.value = 0;
  110.                     btn_mute.label = "UnMute";
  111.                 }
  112.                 } catch (error:Error) {
  113.                     Alert.show(error.toString(), "onMicStatus Error"); 
  114.                 }
  115.             }
  116.             private function onMicActivity(event:ActivityEvent):void {
  117.                 if (event.activating && btn_mute.label == "Mute") {
  118.                     //btn2.label = "true";
  119.                 } else {
  120.                     //btn2.label = "false";
  121.                     miclevel.value = 0;
  122.                 }
  123.             }
  124.             private function micParse(event:SampleDataEvent):void {
  125.                 //try {
  126.                     if (mic.muted)
  127.                         return;
  128.                     if (btn_mute.label == "Mute") {
  129.                         miclevel.value = mic.activityLevel;
  130.                         // we are connected
  131.                         if (VoiceSock.connected) {
  132.                             var buf:ByteArray = new ByteArray();
  133.                             event.data.readBytes(buf, 0, event.data.bytesAvailable);
  134.                             VoiceSock.writeBytes(buf, 0, buf.length);
  135.                             VoiceSock.flush();
  136.                         }
  137.                     }
  138.                 //} catch (error:Error) {
  139.                 //  Alert.show(error.toString(), "micParse Error");
  140.                 //}
  141.             }
  142.            
  143.             private function Join():void {
  144.                 if (selected != MyRoom) {
  145.                     MyRoom = selected;
  146.                     RemoveName(Nick);
  147.                     AddName(Nick, MyRoom);
  148.                 }
  149.             }
  150.             private function dblClick(evt:MouseEvent):void {
  151.                 // if we selected a room, pick room
  152.                 var tmp:XML = ChatTree.selectedItem as XML;
  153.                 if (tmp.localName() == "room") {
  154.                     last = selected;
  155.                     selected = tmp.@title;
  156.                     //mx.controls.Alert.show("Selected: Room="+selected);
  157.                 } else if (tmp.localName() == "user") {
  158.                     // if we selected a name, pick parent node
  159.                     last = selected;
  160.                     selected = tmp.parent().@title;
  161.                     //mx.controls.Alert.show("Selected: User="+selected);
  162.                 }
  163.                 Join();
  164.             }
  165.            
  166.             private function AddName(name:String, dest:String):void {
  167.                 btn2.label = selected;
  168.                 var newNode:XML = <user/>;
  169.                 newNode.@nick = name;
  170.                 var room:XMLList = Rooms.room.(@title == dest);
  171.                 if (room.localName() == "room") {
  172.                     if( room.length() > 0 ) {
  173.                         room[0].appendChild(newNode);
  174.                     }
  175.                 }
  176.             }
  177.            
  178.             private var _foundNode:XML = null;
  179.             private function FindUser(name:String, target:XML):Boolean {
  180.                 if (target.localName() == "user") {
  181.                     // is a user listing
  182.                     if (target.@nick == name) {
  183.                         _foundNode = target;
  184.                         return true;
  185.                     }
  186.                 } else {
  187.                     // is a channel listing
  188.                     if (target.children().length() > 0) {
  189.                         for each (var _node:XML in target.children()) {
  190.                             if (FindUser(name, _node))
  191.                                 return true;
  192.                         }
  193.                     }
  194.                 }
  195.                 return false;
  196.             }
  197.             private function FindRoom(name:String, target:XML):Boolean {
  198.                 if (target.localName() == "user")
  199.                     return false;
  200.                 if (target.localName() != "user") {
  201.                     // did we find it?
  202.                     if (target.@title == name) {
  203.                         _foundNode = target;
  204.                         return true;
  205.                     }
  206.                     // does it have children?
  207.                     if (target.children().length() > 0) {
  208.                         // scan for child rooms
  209.                         for each (var _node:XML in target.children()) {
  210.                             if (FindRoom(name, _node))
  211.                                 return true;
  212.                         }
  213.                     }
  214.                 }
  215.                 return false;
  216.             }
  217.            
  218.             private function RemoveName(name:String):void {
  219.                 if (!FindUser(name, Rooms))
  220.                     return;
  221.                
  222.                 var children:XMLList = XMLList(_foundNode.parent()).children();
  223.                 for(var i:Number=0; i < children.length(); i++) {
  224.                     if( children[i].@nick == _foundNode.@nick ) {
  225.                         delete children[i];
  226.                         break;
  227.                     }
  228.                 }
  229.             }
  230.            
  231.             private function MicToggle():void {
  232.                 if (mic.muted) { // mic disabled
  233.                     Security.showSettings(SecurityPanel.PRIVACY);
  234.                     if (!mic.muted)
  235.                         btn_mute.label = "Mute";
  236.                 } else { // mic enabled
  237.                     if (btn_mute.label == "UnMute") {
  238.                         btn_mute.label = "Mute"; // unmuted
  239.                     } else {
  240.                         miclevel.value = 0;
  241.                         btn_mute.label = "UnMute"; // muted
  242.                     }
  243.                 }
  244.             }
  245.            
  246.             protected function textConnect(event:Event):void
  247.             {
  248.                 trace("Text connected.");
  249.                 // send nickname
  250.                
  251.                 // start voice comms
  252.                 VoiceSock = new Socket();
  253.                 VoiceSock.addEventListener(Event.CONNECT, voiceConnect);
  254.                 VoiceSock.addEventListener(Event.CLOSE, voiceDisconnect);
  255.                 VoiceSock.addEventListener(ProgressEvent.SOCKET_DATA, voiceReceive);
  256.                
  257.                 VoiceSock.connect(server, 3784);
  258.             }
  259.             protected function textDisconnect(event:Event):void
  260.             {
  261.                 trace("Text Disconnected.");
  262.                 if (VoiceSock.connected) {
  263.                     VoiceSock.close();
  264.                 }
  265.                 Alert.show("Text Connection Disconnect");
  266.             }
  267.             private var sbuf:String = "";
  268.             protected function textReceive(event:ProgressEvent):void
  269.             {
  270.                 //try {
  271.                     sbuf += TextSock.readUTFBytes(TextSock.bytesAvailable);
  272.                     trace("Text received "+sbuf.length+".");
  273.                     while (sbuf.indexOf("\r\n") > -1) {
  274.                         // strip next line out of buffer
  275.                         var line:String = sbuf.substr(0,sbuf.indexOf("\r\n"));
  276.                         sbuf = sbuf.substr(sbuf.indexOf("\r\n")+2);
  277.                        
  278.                         if (line.indexOf("PING") == 0) {
  279.                             TextSock.writeUTF("PONG\r\n");
  280.                             continue;
  281.                         }
  282.                         if (line.indexOf("PONG") == 0) {
  283.                             // ignore
  284.                             continue;
  285.                         }
  286.                         Alert.show(line, "Message:");  
  287.                     }
  288.                 //} catch (error:Error) {
  289.                 //  Alert.show(error.toString(), "textReceive Error"); 
  290.                 //}
  291.             }
  292.            
  293.             protected function voiceConnect(event:Event):void
  294.             {
  295.                 trace("voice connected.");
  296.                 //Alert.show("Voice Connected");
  297.             }
  298.             protected function voiceDisconnect(event:Event):void
  299.             {
  300.                 trace("voice Disconnected.");
  301.                 //Alert.show("Voice Connection Disconnect");
  302.             }
  303.            
  304.             private var player:Timer = null;
  305.             private var buf:ByteArray = new ByteArray();
  306.             protected function voiceReceive(event:ProgressEvent):void
  307.             {
  308.                 //try {
  309.                     //event.target.removeEventListener(ProgressEvent.SOCKET_DATA, SoundBuffer);
  310.                     //var bytes:ByteArray = new ByteArray();
  311.                     var avail:uint = VoiceSock.bytesAvailable;
  312.                     trace("voice received "+avail+".");
  313.                     if (avail > 0) {
  314.                         // get bytes from socket, write to buffer
  315.                         var _buf:ByteArray = new ByteArray();
  316.                         VoiceSock.readBytes(_buf);
  317.                         buf.writeBytes(_buf); // add to buf
  318.                                                
  319.                         if (player == null)
  320.                             player = new Timer(1000, 1); // 1 second delay
  321.                         /*if (!player.running) {
  322.                             if (!sound_Playing) {
  323.                                 // received data and we aren't cued to play
  324.                                 player.addEventListener(TimerEvent.TIMER, playSound);
  325.                                 player.start();
  326.                             }
  327.                         }*/
  328.                         playSound(null);
  329.                     }
  330.                 //} catch (error:Error) {
  331.                 //  Alert.show(error.toString(), "voiceReceive Error");
  332.                 //}
  333.             }
  334.            
  335.             private var _numSamples:int;
  336.             private var _lastname:String = "";
  337.             private var _lasttimer:String = "";
  338.             private function playSound(evt:TimerEvent):void
  339.             {
  340.                 // Sock -> buf -> _vbuf -> sound
  341.                 trace("playSound()");
  342.                 //try {
  343.                     if (_vbuf == null)
  344.                         _vbuf = new ByteArray();
  345.                     if (sound != null)
  346.                         sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer);
  347.                     var _buf:ByteArray = new ByteArray();
  348.                     _buf.writeBytes(_vbuf, _vbuf.position, _vbuf.length-_vbuf.position);
  349.                     _buf.writeBytes(buf);
  350.                     buf = new ByteArray();
  351.                     _vbuf = _buf;
  352.                     _vbuf.position = 0;
  353.                    
  354.                     if (_vbuf.length > 0) {
  355.                         trace("vbuf contains "+_vbuf.length+" bytes.");
  356.                     }
  357.                    
  358.                     sound = null;
  359.                     sound = new Sound();
  360.                     sound.addEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer, false, 0, true);
  361.                     //_numSamples = _vbuf.length/4;
  362.                    
  363.                     //sound.extract(_vbuf, int(_vbuf.length * 44.1));
  364.                     //_phase = 0;
  365.                     //channel.addEventListener( Event.SOUND_COMPLETE, playbackComplete );
  366.                     //channel = sound.play();
  367.                     sound_Playing = true;
  368.                     sound.play();
  369.                     //Alert.show("Playing Sound");
  370.                     trace("Playing Sound");
  371.                 //} catch (error:Error) {
  372.                 //  Alert.show(error.toString(), "playSound Error");   
  373.                 //}
  374.             }
  375.            
  376.             private var _phase:Number = 0;
  377.             private function SoundBuffer(event:SampleDataEvent):void {
  378.                 //try {
  379.                 trace("[SoundBuffer]");
  380.                     //sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer);
  381.                
  382.                 var l:Number = 0;
  383.                 var r:Number = 0;
  384.                 // buffer 4KB of data
  385.                 for (var i:int = 0; i < 4096; i++)
  386.                 {
  387.                     //if (_vbuf.length >= int(_phase)*4)
  388.                         //_vbuf.position = int(_phase)*4;
  389.                     if (_vbuf.position == _vbuf.length) {
  390.                         l = 0;
  391.                     } else {
  392.                         l = _vbuf.readFloat();
  393.                     }
  394.                     //_vbuf.position -= 1;
  395.                     if (_vbuf.position == _vbuf.length) {
  396.                         r = 0;
  397.                     } else {
  398.                         //r = _vbuf.readFloat();
  399.                         r=l;
  400.                     }
  401.                     // aiming for a 100mhz quality, or closest there of
  402.                     //for (var rate:int = 0; rate < 100 && rate+(mic.rate/2) < 100; rate += mic.rate) {
  403.                     event.data.writeFloat(l);
  404.                     event.data.writeFloat(r);
  405.                     /*_phase += 1.0;
  406.                     if (_phase >= _numSamples) {
  407.                         _phase -= _numSamples;
  408.                     }*/
  409.                     //}
  410.                 }
  411.                
  412.                 /*if (_vbuf.position == _vbuf.length) {
  413.                     sound_Playing = false;
  414.                     sound.removeEventListener(SampleDataEvent.SAMPLE_DATA, SoundBuffer);
  415.                 }*/
  416.                 /*var _buf:ByteArray = new ByteArray();
  417.                 _buf.writeBytes(_vbuf, _vbuf.position, _vbuf.length);
  418.                 _vbuf = _buf;*/
  419.                
  420.                 //player = new Timer(1000, 1);
  421.                 //player.addEventListener(TimerEvent.TIMER, playSound);
  422.                 //trace("Timer set durring playback.");
  423.                 System.gc();
  424.                 trace("[!SoundBuffer]");
  425.                
  426.                 //} catch (error:Error) {
  427.                 //  Alert.show(error.toString(), "SoundBuffer Error"); 
  428.                 //}
  429.             }
  430.             private function changevoicelimit(event:Event):void {
  431.                 mic.setSilenceLevel(voicelimit.value, 1);
  432.             }
  433.             protected function playbackComplete(event:Event):void
  434.             {
  435.                 /*try {
  436.                     Alert.show("playbackComplete");
  437.                     // TODO Auto-generated method stub
  438.                     // cycle off read data
  439.                     var buf:ByteArray = new ByteArray();
  440.                     buf.readBytes(_vbuf, _vbuf.position, _vbuf.length);
  441.                     if (buf.length < 4096) {
  442.                         for (var c:int = 0; c < 4096-buf.length; c++) {
  443.                             buf.writeFloat(0);// silence
  444.                         }
  445.                     }
  446.                     _vbuf = buf;
  447.                    
  448.                     _vbuf.position = 0;
  449.                     _numSamples = _vbuf.length / 4;
  450.                     _phase = 0;
  451.                     channel = sound.play();
  452.                 } catch (error:Error) {
  453.                     Alert.show(error.toString(), "playbackComplete Error");
  454.                 }*/
  455.             }
  456.            
  457.         ]]>
  458.     </fx:Script>
  459.     <fx:Declarations>
  460.         <!-- Place non-visual elements (e.g., services, value objects) here -->
  461.     </fx:Declarations>
  462.     <s:Panel x="0" y="0" width="100%" height="100%" backgroundColor="#909090" title="LoLChat">
  463.         <s:Label id="Nickname" right="10" top="-29" width="253" height="21" text="Guest"
  464.                  textAlign="right" verticalAlign="middle"/>
  465.        
  466.         <mx:Tree id="ChatTree" left="10" right="10" top="0" bottom="0" doubleClick="dblClick(event);"
  467.             dataProvider="{Rooms_Data}" labelFunction="treeLabel" />
  468.        
  469.         <s:controlBarContent>
  470.             <s:HGroup width="30%" height="44">
  471.                 <s:Button id="btn_join" width="30%" height="40" label="Join" click="Join();"/>
  472.                 <s:ToggleButton id="btn_mute" width="30%" height="40" label="Mute" click="MicToggle();"/>
  473.                 <s:Button id="btn2" width="30%" height="40" label=""/>
  474.             </s:HGroup>
  475.             <s:VGroup width="70%" height="44">
  476.                 <s:HSlider id="miclevel" width="100%" maximum="100" minimum="0" stepSize="1.0"/>
  477.                 <s:HSlider id="voicelimit" width="100%" change="changevoicelimit(event);" maximum="100"
  478.                            minimum="0" stepSize="0.1" value="10"/>
  479.             </s:VGroup>
  480.         </s:controlBarContent>
  481.     </s:Panel>
  482. </s:Application>
Advertisement
Add Comment
Please, Sign In to add comment