aboutsummaryrefslogtreecommitdiffstats
path: root/src/server/Utils.hx
diff options
context:
space:
mode:
authorRblSb <msrblsb@gmail.com>2020-12-18 15:07:16 +0300
committerRblSb <msrblsb@gmail.com>2020-12-18 15:07:16 +0300
commitc32677a0b21d66c0b9b191cdffeb8281324373c3 (patch)
treeeaea4ca17605e9e9d46912f06e39265f8474520f /src/server/Utils.hx
parent1d97ceeae19befdb68bc249dab017d79b8a9d06e (diff)
Don't crash with busy port
Diffstat (limited to 'src/server/Utils.hx')
-rw-r--r--src/server/Utils.hx24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/server/Utils.hx b/src/server/Utils.hx
index 4e2fc9d..bab6300 100644
--- a/src/server/Utils.hx
+++ b/src/server/Utils.hx
@@ -2,6 +2,7 @@ package server;
import js.node.url.URL;
import js.node.Https;
+import js.node.Http;
import js.node.Os;
import sys.FileSystem;
@@ -11,6 +12,29 @@ class Utils {
if (!FileSystem.exists(path)) FileSystem.createDirectory(path);
}
+ public static function isPortFree(port:Int, callback:(free:Bool)->Void):Void {
+ final server = Http.createServer();
+ final timeout = 1000;
+ var status = false;
+
+ server.setTimeout(timeout);
+ server.once("error", function(err) {
+ status = false;
+ server.close();
+ });
+ server.once("timeout", function () {
+ status = false;
+ trace('Timeout (${timeout}ms) occurred waiting for port $port to be available');
+ server.close();
+ });
+ server.once("listening", function() {
+ status = true;
+ server.close();
+ });
+ server.once("close", () -> callback(status));
+ server.listen(port);
+ }
+
public static function getGlobalIp(callback:(ip:String)->Void):Void {
function onError(e):Void {
trace("Warning: connection error, server is local.");
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage