I have a java server running that checks for item respawns every set time interval then sends a packet to every client connected that the item has respawned.
My AS3 client will receive the packet and then spawn the item on the client side. However I also have movement packets the AS3 client is receiving as well.
I tested it and noticed that if I'm moving when the item respawn packet is sent that the AS3 client does not receive the packet and so the item doesn't respawn on the client side but the java server will have the item respawned. I tested it with multiple clients and it may respawn on one client but not the other which I think receiving movement packets at the same time is causing it.
The java server receives all packets 100% of the time but the AS3 client loses packets. I'm not sure why. I can come up with the explaination that if the AS3 client is receiving multiple packets at the same time then packets can get lost. I'm about to implement a way for the java server to flag if a packet has been received or not then resend if the AS3 client doesn't send a message back saying it has received the packet. However this would mean I would need to implement another TimerTask method on the server that goes through each connection and check if they received the packet. I did read TCP is very reliable in transmitting packets.
It could possibly also be the way I wrote my packet receiver code in AS3. I wrote it in such a way that I assume I may not receive all bytes of a packet.
This is the code I have for my client sided packet reader
Code:
// Read method assumes that packet received may be less or more than requirement
// Reads as a stream
// Reading 1 byte at a time is too slow as connection closes before finished reading so reading entire packet received at a time was implemented
public function read(e:ProgressEvent):void {
var packet:Packet;
try {
this.bandwidth_in += this.socket.bytesAvailable;
this.socket.readBytes(buffer, buffer.bytesAvailable, this.socket.bytesAvailable);
// Get packet length data after 4 bytes
if(buffer.length >= 4 && packetLength == 0) {
packetLength = buffer.readInt();
buffer.position = buffer.length;
}
// Once packet is complete, construct packet
if(buffer.length >= packetLength) {
packet = new Packet(this.socket);
var rawData:ByteArray = new ByteArray();
rawData.writeBytes(buffer, rawData.bytesAvailable, packetLength);
rawData.position = 0;
packet.construct(rawData);
// If buffer is > packetLength, take the difference and make it the new buffer
var newBuffer:ByteArray = new ByteArray();
if(buffer.bytesAvailable - packetLength > 0) {
newBuffer.writeBytes(buffer, packetLength, buffer.bytesAvailable - packetLength);
buffer = newBuffer;
} else {
// Reset buffer to 0 size
buffer.clear();
}
// Reset packetLength to 0
packetLength = 0;
// Dispatch event once packet is constructed
dispatchEvent(new PacketEvent(packet.command.toString(), packet));
}
} catch(e:Error) {
DebugConsole.instance.print("Packet read error: "+e);
}
}