Listen inbound connections to AS3 (AIR) Client without using Server Socket

I was able to create a C # server that sends files to AS3 (AIR) clients using sockets . On the AS3 side, I use the flash.net.Socket library to receive data over TCP .

Here's how it works:
-> I turn on my server and it listens to clients (plus I can create a list of connected devices),
-> I turn on my client and automatically receives data;

The trigger event for receiving data is executed on the client, that is, I just turn on the server and when the client turns on, it receives data, causing these events:

 socket.addEventListener(Event.CONNECT, onConnect); -> to connect to the server socket.addEventListener(Event.SOCKET_DATA, onDataArrival); -> to receive the data 

Now I want to do something else. I do not want to run it on the client, I want the server to do this, i.e. I want to enable my client and on the server put in which client will receive the data.

So why am I trying to make a client a client / server? Good, because my server is one machine, and my clients are XXX mobile devices that connect the server, and this is my approach to this.

So, considering what I just said, I managed to create my AS3 client / server application, which works the same way as I do, using the flash.net.ServerSocket library.

First, I set the client to listen:

 serverSocket.bind(portNumber, "10.1.1.212"); serverSocket.addEventListener(ServerSocketConnectEvent.CONNECT, onConnectServer); serverSocket.listen(); 

And then I get the data using flash.net.Socket Event.SOCKET_DATA

And that is pretty much the case. It works the way I want. However, flash.net.ServerSocket not compatible with mobile devices, but ...

So, here is my problem: I need to send files from a C # server (which must listen on clients so that I can create a list of connected devices) for AS3 clients (AIR), but I have to determine which client receives data on the server, and the clients should be ready to accept this data at any time, so listen, but there are a lot of them, so I consider them customers.

And my question is: is there a way to get the client to listen for incoming connections and fire an event when this happens without using Server Socket in AS3?

In addition, if you have a different approach to achieving my goal without using C # server โ†” AS3 client / server logic, please feel free to, in your opinion.

+6
source share
1 answer

Yes. Your client must connect to the server using flash.net.Socket ( doc ) or flash.net.XMLSocket ( doc ). In most cases, you only need one server to which many clients can connect. Itโ€™s not entirely clear why you have servers at both ends?

Edit:

The following is a quick example of how a client continuously listens for messages from a server using Socket . This connects to the server and then waits for (indefinitely) data. When a piece of data is received (in this case, I expect the lines to end with a new line character), the data is sent along with the event.

If you tried this approach without success, you should see if there are any errors or if the connection is closing.

Document Class:

 package { import flash.display.Sprite; public class Main extends Sprite { protected var socket:SocketTest; public function Main() { this.socket = new SocketTest(); this.socket.addEventListener(SocketMessageEvent.MESSAGE_RECEIVED, onSocketMessage); this.socket.connect("127.0.0.1", 7001); } protected function onSocketMessage(e:SocketMessageEvent):void { var date:Date = new Date(); trace(date.hoursUTC + ":" + date.minutesUTC + ":" + date.secondsUTC + " Incoming message: " + e.message); } } } 

SocketTest Class:

 package { import flash.net.Socket; import flash.events.Event; import flash.events.IOErrorEvent; import flash.events.SecurityErrorEvent; import flash.events.ProgressEvent; import flash.errors.IOError; public class SocketTest extends Socket { protected var _message:String; public function SocketTest() { super(); this._message = ""; this.addEventListener(Event.CONNECT, socketConnected); this.addEventListener(Event.CLOSE, socketClosed); this.addEventListener(ProgressEvent.SOCKET_DATA, socketData); this.addEventListener(IOErrorEvent.IO_ERROR, socketError); this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, socketError); } protected function socketData(event:ProgressEvent):void { var str:String = readUTFBytes(bytesAvailable); //For this example, look for \n as a message terminator var messageParts:Array = str.split("\n"); //There could be multiple messages or a partial message, //pass on complete messages and buffer any partial for (var i = 0; i < messageParts.length; i++) { this._message += messageParts[i]; if (i < messageParts.length - 1) { this.notifyMessage(this._message); this._message = ""; } } } protected function notifyMessage(value:String):void { this.dispatchEvent(new SocketMessageEvent(SocketMessageEvent.MESSAGE_RECEIVED, value)); } protected function socketConnected(event:Event):void { trace("Socket connected"); } protected function socketClosed(event:Event):void { trace("Connection was closed"); //TODO: Reconnect if needed } protected function socketError(event:Event):void { trace("An error occurred:", event); } } } 

SocketMessageEvent Class:

 package { import flash.events.Event; public class SocketMessageEvent extends Event { public static const MESSAGE_RECEIVED:String = "messageReceived"; protected var _message:String; public function SocketMessageEvent(type:String, message:String = "", bubbles:Boolean = false, cancelable:Boolean = false) { super(type, bubbles, cancelable); this._message = message; } public function get message():String { return this._message; } } } 

As a test, I installed a server to send a message every 5 seconds, this is the output in the console:

 Socket connected 21:36:24 Incoming message: Message from server: 0 21:36:29 Incoming message: Message from server: 1 21:36:34 Incoming message: Message from server: 2 21:36:39 Incoming message: Message from server: 3 21:36:44 Incoming message: Message from server: 4 21:36:49 Incoming message: Message from server: 5 
+6
source

All Articles