diff options
| author | RblSb <msrblsb@gmail.com> | 2020-12-18 15:07:16 +0300 |
|---|---|---|
| committer | RblSb <msrblsb@gmail.com> | 2020-12-18 15:07:16 +0300 |
| commit | c32677a0b21d66c0b9b191cdffeb8281324373c3 (patch) | |
| tree | eaea4ca17605e9e9d46912f06e39265f8474520f /src/server/Main.hx | |
| parent | 1d97ceeae19befdb68bc249dab017d79b8a9d06e (diff) | |
Don't crash with busy port
Diffstat (limited to 'src/server/Main.hx')
| -rw-r--r-- | src/server/Main.hx | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/server/Main.hx b/src/server/Main.hx index 8a5f381..07f782a 100644 --- a/src/server/Main.hx +++ b/src/server/Main.hx @@ -31,10 +31,10 @@ class Main { public final logsDir:String; final verbose:Bool; final statePath:String; - final wss:WSServer; + var wss:WSServer; final localIp:String; var globalIp:String; - final port:Int; + var port:Int; public final config:Config; final userList:UserList; final clients:Array<Client> = []; @@ -85,6 +85,23 @@ class Main { final envPort = (process.env : Dynamic).PORT; if (envPort != null) port = envPort; + var attempts = 5; + function preparePort():Void { + Utils.isPortFree(port, free -> { + if (!free && attempts > 0) { + trace('Warning: port $port is already in use. Changed to ${port + 1}'); + attempts--; + port++; + preparePort(); + return; + } + runServer(); + }); + } + preparePort(); + } + + function runServer():Void { trace('Local: http://$localIp:$port'); Utils.getGlobalIp(ip -> { globalIp = ip; |
