7 Replies Latest reply on Feb 24, 2008 12:39 PM by mac_55

    Chat component problems

    mac_55 Level 1
      Hi Guys, I haven't got any sort of flash server installed, so I'm using a database to build a primative chat component.
      The app greates a timer on initialise, which calls a web service every 3 seconds to get all of the chat text since the date it passes. The idea is that when the app requests some text, it appends the time to an array and uses the second last time in the array (the time of the last request) as the last chat time.

      So, the problem is that sometimes when text is input it gets inserted into the db, but it doesn't get recalled. I guess because there's an issue with the time. Any ideas?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Panel xmlns="*" xmlns:mx=" http://www.adobe.com/2006/mxml" initialize="TimerCreate()" title="Network Chat" xmlns:reflector="com.rictus.reflector.*" verticalScrollPolicy="off" width="500" height="500" backgroundColor="#ffffff">
      <mx:Script>
      <![CDATA[
      import flash.utils.getTimer;
      import mx.collections.ArrayCollection;
      import mx.formatters.DateFormatter;
      import mx.rpc.events.ResultEvent;
      import mx.controls.Alert;
      import tinag.GlobalStuff;
      import mx.controls.Button;

      [Bindable]
      private var myarray:ArrayCollection;

      [Bindable]
      private var timearray:Array;


      private function ReturnXml(e:ResultEvent):void
      {
      if (e.result.NewDataSet != "")
      {
      for (var i:int = 0; i<e.result.NewDataSet.Table.length; i++)
      {
      myarray.addItem(e.result.NewDataSet.Table );
      }
      }
      dgr1.validateNow();
      dgr1.verticalScrollPosition = dgr1.maxVerticalScrollPosition;

      }
      [Bindable]
      private var ChatRefreshTime:String;

      [Bindable]
      private var ChatRefreshNewTime:String;

      private function TimerCreate():void {
      var myTimer:Timer = new Timer(3000,0);
      myTimer.addEventListener("timer", timerHandler);
      myTimer.start();
      ChatRefreshTime = "2/18/08 14:30:29";
      ChatRefreshNewTime = "2/18/08 14:30:29";
      timearray = new Array();
      timearray.push(ChatRefreshTime);
      timearray.push(ChatRefreshTime);
      TestApp.getChat.send();
      }

      private function UpdateChatTime():void
      {
      var D:DateFormatter = new DateFormatter();
      D.formatString = "M/D/YY H:NN:SS";
      var UTCstring:String = D.format(new Date().toUTCString());
      var UTCD:Number = Date.parse(UTCstring);
      var newDate:Date = new Date(UTCD);
      var n:Number = 1000 * 60 * 60 *2;
      newDate.setTime(newDate.getTime() - n);
      ChatRefreshNewTime = D.format(newDate);
      timearray.push(ChatRefreshNewTime);
      }
      private function initDataGrid():void
      {
      myarray = new ArrayCollection();
      }

      private function timerHandler(event:TimerEvent):void {
      var t:Number = timearray.length - 2;
      ChatRefreshTime = timearray[t];
      TestApp.getChat.send();
      UpdateChatTime();
      }
      private function SendText():void
      {
      if (txtInputText.length>0)
      {
      TestApp.sendInput.send();
      }
      }
      private function SendInputResult(e:ResultEvent):void
      {
      if (e.result==true)
      {
      txtInputText.text = "";
      TestApp.getChat.send();
      }
      }
      ]]>
      </mx:Script>
      <mx:WebService id="TestApp"
      wsdl=" http://myURL/getChatText.asmx?wsdl"
      showBusyCursor="true"
      fault="{Alert.show(event.fault.getStackTrace())}">
      <mx:operation name="getChat" result="ReturnXml(event)">
      <mx:request>
      <UserId>{GlobalStuff.UserId}</UserId>
      <ChatTime>{ChatRefreshTime}</ChatTime>
      </mx:request>
      </mx:operation>
      <mx:operation name="sendInput" result="SendInputResult(event)">
      <mx:request>
      <UserId>{GlobalStuff.UserId}</UserId>
      <ChatText>{txtInputText.text}</ChatText>
      </mx:request>
      </mx:operation>
      </mx:WebService>
      <mx:VBox height="100%" verticalScrollPolicy="off" horizontalScrollPolicy="off" width="100%" horizontalAlign="center" verticalAlign="middle">
      <mx:VBox id="ChatVbox1" top="20" horizontalAlign="center" width="90%" height="80%">
      <mx:Label text="{ChatRefreshTime}"/>
      <mx:DataGrid x="34" y="116" id="dgr1" headerHeight="0" variableRowHeight="true" initialize="initDataGrid()" dataProvider="{myarray}" width="96%" height="80%">
      <mx:columns>
      <mx:DataGridColumn width="300">
      <mx:itemRenderer>
      <mx:Component>
      <mx:Text text="{data.Username.toString() +'> '+data.Message.toString()}"/>
      </mx:Component>
      </mx:itemRenderer>
      </mx:DataGridColumn>
      <mx:DataGridColumn width="50">
      <mx:itemRenderer>
      <mx:Component>
      <mx:Text text="{data.TimeOfChatt}"/>
      </mx:Component>
      </mx:itemRenderer>
      </mx:DataGridColumn>
      </mx:columns>
      </mx:DataGrid>
      <mx:HBox>
      <mx:TextInput x="34" y="275" width="267" id="txtInputText" maxChars="100" displayAsPassword="false"/>
      <mx:Button x="309" y="275" label="&gt;" width="27" click="SendText()"/>
      </mx:HBox>
      </mx:VBox>
      <!--<reflector:Reflector target="{ChatVbox1}" alpha="0.2" falloff="0.1"/>-->
      </mx:VBox>
      </mx:Panel>