[connect] web-socket connection fixes
This commit is contained in:
@@ -21,6 +21,7 @@ class Animate implements IAnimate {
|
||||
public function new(view:IView<Dynamic>, duration:Int = -1) {
|
||||
this.view = view;
|
||||
this.duration = duration > -1 ? duration : defaultDuraion;
|
||||
this.startTime = 0;
|
||||
}
|
||||
|
||||
private inline function get_object():DisplayObject {
|
||||
@@ -28,8 +29,10 @@ class Animate implements IAnimate {
|
||||
}
|
||||
|
||||
public function start():Promise<IAnimate> {
|
||||
startTime = runner.run(this);
|
||||
startTime = Date.now().getTime();
|
||||
deferred = new Deferred();
|
||||
runner.push(this);
|
||||
this.update(startTime);
|
||||
return deferred.promise();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,10 +19,7 @@ import flash.Lib;
|
||||
}
|
||||
}
|
||||
|
||||
public function run(animate:IAnimate):Float {
|
||||
public function push(animate:IAnimate):Void {
|
||||
animates.push(animate);
|
||||
var time = Date.now().getTime();
|
||||
animate.update(time);
|
||||
return time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,4 +17,13 @@ class PacketUtil {
|
||||
packet.writeTo(out);
|
||||
return out.getBytes();
|
||||
}
|
||||
|
||||
public static function toBytesWithSize<P:Message>(packet:P):Bytes {
|
||||
var out = new BytesOutput();
|
||||
out.writeUInt16(0);
|
||||
packet.writeTo(out);
|
||||
var bytes = out.getBytes();
|
||||
bytes.setUInt16(0, bytes.length - 2);
|
||||
return bytes;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,22 +71,14 @@ class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
}
|
||||
|
||||
private function onSocketData(event:Dynamic):Void {
|
||||
var packet:I = null;
|
||||
try {
|
||||
var bytes = Bytes.ofData(event.data);
|
||||
packet = PacketUtil.fromBytes(bytes, queue.packetClass);
|
||||
} catch (error:Dynamic) {
|
||||
handler.emit(ConnectionEvent.ERROR(error));
|
||||
}
|
||||
if (packet != null) {
|
||||
receive(packet);
|
||||
}
|
||||
pushData(bytes);
|
||||
}
|
||||
|
||||
override public function send(packet:O):Void {
|
||||
if (connected) {
|
||||
super.send(packet);
|
||||
var bytes = PacketUtil.toBytes(packet);
|
||||
var bytes = PacketUtil.toBytesWithSize(packet);
|
||||
socket.send(bytes.getData());
|
||||
} else {
|
||||
L.w("Connection", "closed");
|
||||
|
||||
@@ -10,6 +10,7 @@ import sys.net.Socket;
|
||||
class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
|
||||
private var opened:Bool;
|
||||
private var buffer:Bytes;
|
||||
|
||||
public function new(socket:Socket, i:Class<I>) {
|
||||
super(socket, i);
|
||||
@@ -17,8 +18,8 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
}
|
||||
|
||||
override private function sendPacket(packet:O):Void {
|
||||
var data = PacketUtil.toBytes(packet);
|
||||
writeData(data, socket);
|
||||
var bytes = PacketUtil.toBytesWithSize(packet);
|
||||
writeData(bytes, socket);
|
||||
}
|
||||
|
||||
override public function pushData(bytes:Bytes):Void {
|
||||
@@ -33,8 +34,7 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
} else {
|
||||
var data = parseData(bytes);
|
||||
if (data != null) {
|
||||
var packet:I = PacketUtil.fromBytes(data, queue.packetClass);
|
||||
receive(packet);
|
||||
super.pushData(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -126,23 +126,17 @@ class NekoWSConnection<O:Message, I:Message> extends NekoConnection<O, I> {
|
||||
len = (b2 << 24) + (b3 << 16) + (b4 << 8) + b5;
|
||||
}
|
||||
|
||||
//Lib.println("len = " + len);
|
||||
|
||||
// direct array init not work corectly!
|
||||
var mask = [];
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
mask.push(bytes.get(p++));
|
||||
|
||||
//Lib.println("mask = " + mask);
|
||||
|
||||
var data = new BytesBuffer();
|
||||
for (i in 0...len) {
|
||||
data.addByte(bytes.get(p++) ^ mask[i % 4]);
|
||||
}
|
||||
|
||||
//Lib.println("readed = " + data.toString());
|
||||
return data.getBytes();
|
||||
} else {
|
||||
throw "Expected masked data.";
|
||||
|
||||
Reference in New Issue
Block a user