- Joined
- Oct 26, 2012
- Messages
- 2,357
- Reaction score
- 1,086
Hello guys,
I have a question. I wanted to create an own SWF with sockets and packets. This is my MessageHandler.as code:
And my TcpConnection.vb code:
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?
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?