From d3336dda47d53dd8607d84ddedf3748ecfcfc4b2 Mon Sep 17 00:00:00 2001 From: CrazyFlasher Date: Fri, 17 Sep 2021 11:37:58 +0300 Subject: [PATCH 1/2] - added optional callbacks to start and close - fixed null pointer if immediateOpen if false in WebSocket constructor - changed MainLoop to haxe.Timer in WebSocketServer for nodejs target --- hx/ws/WebSocket.hx | 2 +- hx/ws/WebSocketServer.hx | 27 +++++++++++++++++++++++---- hx/ws/java/NioSocket.hx | 2 +- hx/ws/nodejs/NodeSocket.hx | 12 ++++++++---- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/hx/ws/WebSocket.hx b/hx/ws/WebSocket.hx index 92f6389..218180c 100644 --- a/hx/ws/WebSocket.hx +++ b/hx/ws/WebSocket.hx @@ -22,7 +22,6 @@ class WebSocket { // lets use composition so we can intercept send / onmessage a if (immediateOpen) { open(); } - set_binaryType(Types.BinaryType.ARRAYBUFFER); } private function createSocket() @@ -35,6 +34,7 @@ class WebSocket { // lets use composition so we can intercept send / onmessage a throw "Socket already connected"; } _ws = createSocket(); + set_binaryType(Types.BinaryType.ARRAYBUFFER); } public var onopen(get, set):Function; diff --git a/hx/ws/WebSocketServer.hx b/hx/ws/WebSocketServer.hx index ead6de8..4c61b88 100644 --- a/hx/ws/WebSocketServer.hx +++ b/hx/ws/WebSocketServer.hx @@ -1,5 +1,8 @@ + + package hx.ws; +import haxe.Timer; import haxe.Constraints; import haxe.MainLoop; import haxe.io.Error; @@ -26,6 +29,8 @@ class WebSocketServer public var onClientAdded:T->Void = null; public var onClientRemoved:T->Void = null; + private var _stopCallBack:Void -> Void; + public function new(host:String, port:Int, maxConnections:Int = 1) { _host = host; _port = port; @@ -36,13 +41,13 @@ class WebSocketServer return new SocketImpl(); } - public function start() { + public function start(?callBack:Void -> Void) { _stopServer = false; _serverSocket = createSocket(); _serverSocket.setBlocking(false); _serverSocket.bind(new sys.net.Host(_host), _port); - _serverSocket.listen(_maxConnections); + _serverSocket.listen(_maxConnections, callBack); Log.info('Starting server - ${_host}:${_port} (maxConnections: ${_maxConnections})'); #if cs @@ -56,6 +61,10 @@ class WebSocketServer Sys.sleep(sleepAmount); } + #elseif nodejs + + startTick(); + #else MainLoop.add(function() { @@ -66,6 +75,15 @@ class WebSocketServer #end } + #if nodejs + private function startTick():Void + { + tick(); + + Timer.delay(startTick, 100); + } + #end + private function handleNewSocket(socket) { var handler = new T(socket); handlers.push(handler); @@ -83,7 +101,7 @@ class WebSocketServer } handlers = []; try { - _serverSocket.close(); + _serverSocket.close(_stopCallBack); } catch (e:Dynamic) { } return false; } @@ -121,7 +139,8 @@ class WebSocketServer return true; } - public function stop() { + public function stop(?callBack:Void -> Void) { + _stopCallBack = callBack; _stopServer = true; } diff --git a/hx/ws/java/NioSocket.hx b/hx/ws/java/NioSocket.hx index d05786d..89bf4a2 100644 --- a/hx/ws/java/NioSocket.hx +++ b/hx/ws/java/NioSocket.hx @@ -40,7 +40,7 @@ class NioSocket { } } - public function close():Void { + public function close(?callBack:Void -> Void):Void { if (serverChannel != null) { server.close(); } diff --git a/hx/ws/nodejs/NodeSocket.hx b/hx/ws/nodejs/NodeSocket.hx index fb5bb88..1bb034d 100644 --- a/hx/ws/nodejs/NodeSocket.hx +++ b/hx/ws/nodejs/NodeSocket.hx @@ -45,7 +45,7 @@ class NodeSocket { } - public function listen(connections:Int):Void { + public function listen(connections:Int, ?callBack:Void -> Void):Void { if (_host == null) { throw "You must bind the Socket to an address!"; } @@ -55,7 +55,7 @@ class NodeSocket { host: _host.host, port: _port, backlog: connections - }); + }, callBack); } private var _host:Host = null; @@ -68,10 +68,14 @@ class NodeSocket { public function setBlocking(blocking:Bool) { } + + public function setTimeout(value:Int) { + _socket.setTimeout(value); + } - public function close() { + public function close(?callBack:Void -> Void) { if (_server != null) { - _server.close(); + _server.close(callBack); } if (_socket != null) { _socket.destroy(); From 35602305083c34cb8bd4fe78a3d4cced9169eba2 Mon Sep 17 00:00:00 2001 From: CrazyFlasher Date: Fri, 17 Sep 2021 12:13:19 +0300 Subject: [PATCH 2/2] - stop ticking, tick() returned false --- hx/ws/WebSocketServer.hx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hx/ws/WebSocketServer.hx b/hx/ws/WebSocketServer.hx index 4c61b88..8bef2cf 100644 --- a/hx/ws/WebSocketServer.hx +++ b/hx/ws/WebSocketServer.hx @@ -78,9 +78,12 @@ class WebSocketServer #if nodejs private function startTick():Void { - tick(); + var continueTick:Bool = tick(); - Timer.delay(startTick, 100); + if (continueTick) + { + Timer.delay(startTick, 100); + } } #end