Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MainLoop issue for NodeJS target #35

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion hx/ws/WebSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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;
Expand Down
30 changes: 26 additions & 4 deletions hx/ws/WebSocketServer.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@


package hx.ws;

import haxe.Timer;
import haxe.Constraints;
import haxe.MainLoop;
import haxe.io.Error;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -56,6 +61,10 @@ class WebSocketServer
Sys.sleep(sleepAmount);
}

#elseif nodejs

startTick();

#else

MainLoop.add(function() {
Expand All @@ -66,6 +75,18 @@ class WebSocketServer
#end
}

#if nodejs
private function startTick():Void
{
var continueTick:Bool = tick();

if (continueTick)
{
Timer.delay(startTick, 100);
}
}
#end

private function handleNewSocket(socket) {
var handler = new T(socket);
handlers.push(handler);
Expand All @@ -83,7 +104,7 @@ class WebSocketServer
}
handlers = [];
try {
_serverSocket.close();
_serverSocket.close(_stopCallBack);
} catch (e:Dynamic) { }
return false;
}
Expand Down Expand Up @@ -121,7 +142,8 @@ class WebSocketServer
return true;
}

public function stop() {
public function stop(?callBack:Void -> Void) {
_stopCallBack = callBack;
_stopServer = true;
}

Expand Down
2 changes: 1 addition & 1 deletion hx/ws/java/NioSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class NioSocket {
}
}

public function close():Void {
public function close(?callBack:Void -> Void):Void {
if (serverChannel != null) {
server.close();
}
Expand Down
33 changes: 17 additions & 16 deletions hx/ws/nodejs/NodeSocket.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class NodeSocket {
input = new NodeSocketInput(this);
output = new NodeSocketOutput(this);
}

private var _server:Server = null;
private function createServer():Void {
if (_server == null) {
_server = Net.createServer(acceptConnection);
}
}

private static var _connections:Array<NodeSocket> = [];
private var _newConnections:Array<NodeSocket> = [];
private function acceptConnection(socket:Socket) {
Expand All @@ -34,18 +34,18 @@ class NodeSocket {
_connections.push(nodeSocket);
_newConnections.push(nodeSocket);
}

public function accept() {
if (_newConnections.length == 0) {
throw "Blocking";
}

var socket = _newConnections.pop();
return socket;

}

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!";
}
Expand All @@ -55,32 +55,33 @@ class NodeSocket {
host: _host.host,
port: _port,
backlog: connections
});
}, callBack);
}

private var _host:Host = null;
private var _port:Int;
public function bind(host:Host, port:Int):Void {
_host = host;
_port = port;
}

public function setBlocking(blocking:Bool) {

}

public function setTimeout(timeout:Int) {
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();
}
}

public static function select(read : Array<SocketImpl>, write : Array<SocketImpl>, others : Array<SocketImpl>, ?timeout : Float) : { read: Array<SocketImpl>, write: Array<SocketImpl>, others: Array<SocketImpl> } {
if (write != null && write.length > 0) {
throw "Not implemented";
Expand All @@ -104,7 +105,7 @@ class NodeSocket {
}
}
}

return ret;
}
}