[client] fixes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tankz",
|
||||
"version": "0.7.1",
|
||||
"version": "0.7.2",
|
||||
"private": true,
|
||||
"devDependencies": {
|
||||
"ansi-colors": "^1.0.1",
|
||||
|
||||
@@ -38,9 +38,9 @@ class Init {
|
||||
#if flash
|
||||
Provider.set(IConnection, new ru.m.connect.flash.FlashConnection<Request, Response>('localhost', 5001, Response));
|
||||
#elseif html5
|
||||
Provider.set(IConnection, new ru.m.connect.js.JsConnection('localhost', 5001));
|
||||
Provider.set(IConnection, new ru.m.connect.js.JsConnection<Request, Response>('localhost', 5001, Response));
|
||||
#else
|
||||
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection());
|
||||
Provider.set(IConnection, new ru.m.connect.fake.FakeConnection<Request, Response>(Response));
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,10 @@ views:
|
||||
text: 2 VS
|
||||
$style: button
|
||||
# network
|
||||
- $type: haxework.gui.VGroupView
|
||||
contentSize: true
|
||||
visible: false
|
||||
views:
|
||||
- $type: haxework.gui.LabelView
|
||||
$style: label
|
||||
fontSize: 20
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
package ru.m.connect.fake;
|
||||
|
||||
import ru.m.connect.IConnection.ConnectionEvent;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
|
||||
class FakeConnection extends BaseConnection {
|
||||
override public function connect():Void {}
|
||||
override public function disconnect():Void {}
|
||||
|
||||
class FakeConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
var promise:Promise<IConnection<O, I>> = cast Promise.promise(this);
|
||||
return promise;
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package ru.m.connect.js;
|
||||
|
||||
import ru.m.Base64;
|
||||
import ru.m.connect.IConnection.IConnectionHandler;
|
||||
import promhx.Promise;
|
||||
import protohx.Message;
|
||||
import haxe.io.Bytes;
|
||||
import ru.m.Base64;
|
||||
import ru.m.connect.IConnection.ConnectionEvent;
|
||||
|
||||
|
||||
typedef WebSocket = {
|
||||
var onopen:Dynamic -> Void;
|
||||
@@ -15,14 +16,14 @@ typedef WebSocket = {
|
||||
function close():Void;
|
||||
}
|
||||
|
||||
class JsConnection extends BaseConnection {
|
||||
class JsConnection<O:Message, I:Message> extends BaseConnection<O, I> {
|
||||
|
||||
private var host:String;
|
||||
private var port:Int;
|
||||
private var socket:WebSocket;
|
||||
|
||||
public function new(host:String, port:Int) {
|
||||
super();
|
||||
public function new(host:String, port:Int, i:Class<I>) {
|
||||
super(i);
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
connected = false;
|
||||
@@ -32,7 +33,7 @@ class JsConnection extends BaseConnection {
|
||||
return untyped __js__("new WebSocket('ws://'+host+':'+port);");
|
||||
}
|
||||
|
||||
override public function connect():Void {
|
||||
override public function connect():Promise<IConnection<O, I>> {
|
||||
var self = this;
|
||||
var decodeBytes = Base64.decodeBase64;
|
||||
socket = buildSocket(host, port);
|
||||
@@ -40,43 +41,43 @@ class JsConnection extends BaseConnection {
|
||||
socket.onclose = this.onClose;
|
||||
socket.onerror = this.onError;
|
||||
socket.onmessage = this.onSocketData;
|
||||
return connectDeferred.promise();
|
||||
}
|
||||
|
||||
override public function disconnect():Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.dispatch(function(h) h.onDisconnected());
|
||||
}
|
||||
|
||||
private function onError(event:Dynamic):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.dispatch(function(h) h.onError(event));
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
}
|
||||
|
||||
private function onConnect(_):Void {
|
||||
connected = true;
|
||||
handler.dispatch(function(h) h.onConnected());
|
||||
handler.emit(ConnectionEvent.CONNECTED);
|
||||
}
|
||||
|
||||
private function onClose(_):Void {
|
||||
socket.close();
|
||||
connected = false;
|
||||
handler.dispatch(function(h) h.onDisconnected());
|
||||
handler.emit(ConnectionEvent.DISCONNECTED);
|
||||
}
|
||||
|
||||
private function onSocketData(event:Dynamic):Void {
|
||||
try {
|
||||
var data:String = event.data;
|
||||
var packet = WebSocketTools.string2packet(data, builder);
|
||||
var packet:I = WebSocketTools.string2packet(data, queue.packetClass);
|
||||
receive(packet);
|
||||
} catch (error:Dynamic) {
|
||||
handler.dispatch(function(h) h.onError(error));
|
||||
handler.emit(ConnectionEvent.ERROR(event));
|
||||
}
|
||||
}
|
||||
|
||||
override public function send(packet:Message):Void {
|
||||
override public function send(packet:O):Void {
|
||||
super.send(packet);
|
||||
socket.send(WebSocketTools.packet2string(packet, builder));
|
||||
socket.send(WebSocketTools.packet2string(packet));
|
||||
}
|
||||
}
|
||||
@@ -224,10 +224,9 @@ class Game {
|
||||
}
|
||||
|
||||
public function next():Option<GameState> {
|
||||
var level = state.level + 1;
|
||||
state.level++;
|
||||
if (level >= config.game.levels) level = 0;
|
||||
return Option.Some({type: state.type, presetId: preset.id, level: level});
|
||||
if (state.level >= config.game.levels) state.level = 0;
|
||||
return Option.Some({type: state.type, presetId: preset.id, level: state.level});
|
||||
}
|
||||
|
||||
public function dispose():Void {
|
||||
|
||||
Reference in New Issue
Block a user