A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: AS3 XMLSocket not receiving data continuously?

  1. #1
    Junior Member
    Join Date
    Jun 2007
    Posts
    3

    AS3 XMLSocket not receiving data continuously?

    Hi guys. I need some help with XML sockets in ActionScript 3 / Flex. I've previously built a simple XML parser in ActionScript 2 that receives data from a Java server through XML sockets. The server is connected to an input device, and sends out control messages at about 30 fps to Flash.

    I just started learning Flex and AS3 and ported the old, working software to AS3, but it's gotten really weird. Most of the time when I debug the app in Flex, I don't get a connection at all, sometimes the event handler receives a Connect event, and very rarely a data event is received (but even so only one event, when the server pushes them out continuously). To clarify, I mean that each time I click on Debug, I get 0, 1 or 2 events, and then nothing. I have to start the app over again.

    The AS2 version works perfectly in the exact same setup.

    It's not a security problem because connections do happen... so maybe I'm missing something in the way Flex is set up, or some basics of AS3 applications. Below is my test program in full.

    Code:
    package {
    	import flash.display.Sprite;
    
    	import flash.net.XMLSocket;
    	import flash.events.Event;
    	import flash.events.DataEvent;
    	import flash.events.IOErrorEvent;
    
    	public class SocketTest extends Sprite
    	{
    		public function SocketTest()
    		{
    			var socket:XMLSocket = new XMLSocket();
    			socket.connect("127.0.0.1", 10001);
    
    			socket.addEventListener(DataEvent.DATA, onData);
    
    			socket.addEventListener(Event.CONNECT, onEvent);
    			socket.addEventListener(Event.CLOSE, onEvent);
    			socket.addEventListener(IOErrorEvent.IO_ERROR, onEvent);
    		}
    		
    		private function onEvent(event:Event):void {
    			trace(event);
    		}
    
    		private function onData(event:DataEvent):void {
    			trace(event);
    		}
    	}
    }
    Any ideas?

  2. #2
    Junior Member
    Join Date
    Jun 2007
    Posts
    3
    I did some more testing. I removed the class wrapper and made the application into a simple file in Flash CS2. Everything works just fine.

    From this, I must conclude that it's an issue with Flex 2. Any ideas on where I should start looking for the solution?

  3. #3
    Junior Member
    Join Date
    Feb 2008
    Posts
    1
    I know this is a kind of an old thread, but since I ran into the same problem (and think I found the solution), I thought I'd post it here.

    The problem is that the instance of XMLSocket has no more references within the javascript world, so it's being cleaned up.

    When I changed "socket" from a local variable to a member variable, it worked as expected in AS3.



    Code:
    	public class SocketTest extends Sprite
    	{
    		var socket:XMLSocket; // <-- Now a member variable!
    
    		public function SocketTest()
    		{
    			socket = new XMLSocket();
    			socket.connect("127.0.0.1", 10001);
    
    			socket.addEventListener(DataEvent.DATA, onData);
    
    			socket.addEventListener(Event.CONNECT, onEvent);
    			socket.addEventListener(Event.CLOSE, onEvent);
    			socket.addEventListener(IOErrorEvent.IO_ERROR, onEvent);
    		}
    		
    		private function onEvent(event:Event):void {
    			trace(event);
    		}
    
    		private function onData(event:DataEvent):void {
    			trace(event);
    		}
    	}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center