How to connect to a server using OpenFL?

Hello, I am currently making a multiplayer game using HaxeFlixel (built on OpenFL).

On the client side, I created a socket and sent an intial message upon connection. And then every one second another message would be sent through the socket.

The server will automatically send a message to any client that connects to it. Additionally, it will print any messages it receives.

The behaviour described above works well for android target. However, for the flash target only the initial connection message and server reply are delivered correctly. The periodic message sent by the client every 1 second is not printed on the server. I suspect the issue is related to flash socket policy. I did try setting up a seperate policy server, but I don’t know how to connect to it since openfl.system.Security.loadPolicyFile() is blank.

Client code:

package;
import openfl.net.Socket;
import openfl.events.*;
import openfl.system.Security;
import openfl.utils.Timer;

/**
 * ...
 * @author ...
 */
class ServerConnector
{
	private var socket:Socket = new Socket();
	private var socketData:String;

	public function new() 
	{
		Security.allowDomain("*");
		
		socket.addEventListener(Event.CONNECT, onConnect);
		socket.addEventListener(Event.CLOSE, onClose);
		socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
		socket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
		socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
		trace("Connecting to server.");
		socket.connect("52.36.8.250", 443);
		
		var timer:Timer = new Timer(1000);
		timer.addEventListener(TimerEvent.TIMER, timer_onTimer);
		timer.start();
		
	}
	
	private function timer_onTimer(event:TimerEvent):Void {

		trace("sending info");
		socket.writeUTFBytes("info");
	}
	
		function onConnect(e:Event):Void
		{
			trace("connected!");
			socket.writeUTFBytes("Initial connection.");
			socket.readUTFBytes(socket.bytesAvailable); // flush initial data
		}
		
		function onClose(e:Event):Void
		{
			// Security error is thrown if this line is excluded
			trace("on close");
			socket.close();
		}
		
		function onError(e:IOErrorEvent):Void
		{
			trace("IO Error: " + e);
		}
		
		function onSecError(e:SecurityErrorEvent):Void
		{
			trace("Security Error: " + e);
		}
		
		function onResponse(e:ProgressEvent):Void
		{
			trace("got response");
			
			if (socket.bytesAvailable > 0)
			{
				socketData = socket.readUTFBytes(socket.bytesAvailable);
				trace(socketData);
			}
			
			
		}
	
}

Server code (nodejs code):

var port = 443;
var net = require('net');
 
var server = net.createServer( function(socket)
{
	socket.on('data', function(data)
	{
		console.log("data received: " + data);
		socket.write('hellohello! Contact has been made!');

	});
 
	socket.on('close', function(socket)
	{
		console.log("close received");
	});
 
	socket.on('timeout', function(data)
	{
		console.log("timeout received");
	});
 
	socket.on('error', function(data)
	{
		console.log("error received");
	});
});
 
server.listen(port);

I am using this policy server https://www.npmjs.com/package/socket-policy-server but I’m not sure how to utilize it from client side.

Thanks for reading,
Don

Did you tried socket.flush() after socket.write() ?
I think Security.allowDomain() inside client is not always needed. For policy-delivery i use this: https://github.com/maitag/peote-socket/blob/master/html5-test/flashpolicyd.pl together with xml: https://github.com/maitag/peote-socket/blob/master/html5-test/flash_policy.xml
Its also possible to send policy-xml directly from your server-code (flashplayer ask on first connection for that)
Here is what i did for flash-target client: https://github.com/maitag/peote-socket/blob/master/Source/de/peote/socket/flash/PeoteSocket.hx

socket.flush() fixed my problem, thanks!

hxnet is cool to … i try to help myself with own peote-net lib … wanna be p2p “again” 8) ( NAT?)

for clientside js i would use simple http://peerjs.com/