[Question] AS3 Socket Programming

Joined
Oct 26, 2012
Messages
2,355
Reaction score
1,087
Hello guys,

I have a question. I wanted to create an own SWF with sockets and packets. This is my MessageHandler.as code:

PHP:
package com {
	import flash.utils.Dictionary;
	import flash.utils.ByteArray;
	import flash.net.Socket;
	
	public class MessageHandler {
		
		private const Incoming:Dictionary = new Dictionary();
		private var mSocket:Socket;

		public function MessageHandler(mSocket:Socket) {
			this.mSocket = mSocket;
			
			Object o = new Object();
			o.test = new StartupCrypto(mSocket);
			
			Incoming[1] = o;
		}

		public function Handle(arg1:ByteArray) : void
		{
			var Reader:MessageEventReader = new MessageEventReader(arg1);
			
			var Header:int = Reader.readShort();
			
			if (Incoming[Header] == null)
			{
				this.mSocket.writeUTFBytes("hi");
			}
			
			if (Incoming[Header] != null)
			{
				try
				{
					this.Incoming[Header].
				}
				catch (e:Error)
				{
					throw new Error(e.message, e.errorID);
				}
			}
			else
			{
				this.mSocket.writeUTF("Cannot handle incoming packet " + Header + "!");
			}
		}
	}
	
}

And my TcpConnection.vb code:

PHP:
Imports System.Net.Sockets
Public Class TcpConnection
    Dim mSocket As Socket
    Dim mBuffer As Byte() = New Byte(1024) {}
    Public Sub New(ByRef mSocket As Socket)
        Me.mSocket = mSocket

        Console.WriteLine("Accepted connection from {0}", mSocket.RemoteEndPoint)
    End Sub
    Public Sub Start()
        Me.mSocket.BeginReceive(Me.mBuffer, 0, Me.mBuffer.Length, SocketFlags.None, AddressOf OnReceive, Nothing)
        Me.mSocket.Send(New Byte() {0, 1})
    End Sub
    Private Sub OnReceive(ByVal iAr As IAsyncResult)
        Dim Bits As Integer = Me.mSocket.EndReceive(iAr)

        If (Bits > 0) Then
            Dim Data As String = System.Text.Encoding.Default.GetString(Me.mBuffer, 0, Bits)

            Console.WriteLine("Received " & Data)
        Else
            ' Disconnect user.
        End If
    End Sub
End Class

However, if I send [0][1] (short 1) and read it in flash, it says Incoming[1] is null. How can I make it, Incoming[ID] = a class, and to progress it, it will invoke the constructor of the class?
 
Are you trying to send a vB class to flash and receive it as a AS3 class? If so, aint gonna happen, really.. You can send properties of a vB class to flash, and use those properties to instantiate an object in flash, however.

The point is, I send bytes to the flash socket. The socket receives short 1 but it says Incoming dictionary with short 1 is null, but it aint.
 
For one your casting up to an integer var k:int = blah.readShort()

Secondly you don't appear to be calling any functions from the class just accessing it via the array.
 
Back