Establishing a socket connection between a .swf file and a socket-test program (TCP/IP builder - Windows), in AS3.
I have an issue with a college project I'm working on.
Using Actionscript 3, I made a simple .swf program, an animated, interactive smiley, that 'reacts' to number inputs in a input-box.
For the sake of the project, I now need to make the framework for establishing a socket connection with the smiley .swf, and another program.
This is where I encounter issues. I have very little knowledge of AS3 programming, so I'm not certain how to establish the connection - what's required code-wise for it, that is.
To test the connection, I'm attempting to use the "TCP/IP builder" program from windows, which lets me set up a server socket. I need to program the .swf file into a client - to recognize it, connect to it, then be able to receive data (so that the data can then be used to have the smiley 'react' to it - like how it does now with the input-box, only 'automatically' as it gets the data rather than by manual input).
My attempts at coding it are as follows, using a tutorial (linked HERE😞
//SOCKET STUFF GOES HERE
//****************************************************************
var socket:XMLSocket;
stage.addEventListener(MouseEvent.CLICK, doConnect);
// This one connects to local, port 9001, and applies event listeners
function doConnect(evt:MouseEvent):void
{
stage.removeEventListener(MouseEvent.CLICK, doConnect);
socket = new XMLSocket("127.0.0.1", 9001);
socket.addEventListener(Event.CONNECT, onConnect);
socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
}
// This traces the connection (lets us see it happened, or failed)
function onConnect(evt:Event):void
{
trace("Connected");
socket.removeEventListener(Event.CONNECT, onConnect);
socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
socket.addEventListener(DataEvent.DATA, onDataReceived);
socket.addEventListener(Event.CLOSE, onSocketClose);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
}
function onError(evt:IOErrorEvent):void
{
trace("Connect failed");
socket.removeEventListener(Event.CONNECT, onConnect);
socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
stage.addEventListener(MouseEvent.CLICK, doConnect);
}
// Here, the flash tracks what keyboard button is pressed.
// If 'q' is pressed, the connection ends.
function keyUp(evt:KeyboardEvent):void
{
if (evt.keyCode == 81) // the key code for q is 81
{
socket.send("exit");
}
else
{
socket.send(evt.keyCode);
}
}
// This one should handle the data we get from the server.
function onDataReceived(evt:DataEvent):void
{
try {
trace("From Server:", evt.data );
}
catch (e:Error) {
trace('error');
}
}
function onSocketClose(evt:Event):void
{
trace("Connection Closed");
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
socket.removeEventListener(Event.CLOSE, onSocketClose);
socket.removeEventListener(DataEvent.DATA, onDataReceived);
Trying to connect to the socket gives me either no result (other than a 'connection failed' message when I click the .swf), or the following error:
at Smiley_TCP_IP_v4_fla::MainTimeline/doConnect()[Smiley_TCP_IP_v4_fla.MainTimeline::frame1:12]

