From 76f29d9178c4f0c7c4ddf11a8541de1eb6345818 Mon Sep 17 00:00:00 2001 From: Mike Olsen Date: Wed, 24 Aug 2016 11:31:53 -0700 Subject: [PATCH 1/3] added RestartAfterListenError --- src/Fleck/WebSocketServer.cs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Fleck/WebSocketServer.cs b/src/Fleck/WebSocketServer.cs index eb5cda3a..b359edbf 100644 --- a/src/Fleck/WebSocketServer.cs +++ b/src/Fleck/WebSocketServer.cs @@ -43,6 +43,7 @@ public WebSocketServer(int port, string location) public X509Certificate2 Certificate { get; set; } public SslProtocols EnabledSslProtocols { get; set; } public IEnumerable SupportedSubProtocols { get; set; } + public bool RestartAfterListenError { get; set; } public bool IsSecure { @@ -99,7 +100,24 @@ public void Start(Action config) private void ListenForClients() { - ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e)); + ListenerSocket.Accept(OnClientConnect, e => { + + FleckLog.Error("Listener socket is closed", e); + + if(RestartAfterListenError){ + + FleckLog.Info("Listener socket restarting"); + + try + + { + + ListenerSocket.Dispose(); + + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); + + ListenerSocket = new SocketWrapper(socket); + + Start(_config); + + FleckLog.Info("Listener socket restarted"); + + } + + catch (Exception ex) + + { + + FleckLog.Error("Listener could not be restarted", ex); + + } + + } + + }); } private void OnClientConnect(ISocket clientSocket) From ead5f7f3f13635ecf34d29a79a95d1b754d71a11 Mon Sep 17 00:00:00 2001 From: Mike Olsen Date: Wed, 24 Aug 2016 23:47:58 -0700 Subject: [PATCH 2/3] Revert "added RestartAfterListenError" --- src/Fleck/WebSocketServer.cs | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Fleck/WebSocketServer.cs b/src/Fleck/WebSocketServer.cs index b359edbf..eb5cda3a 100644 --- a/src/Fleck/WebSocketServer.cs +++ b/src/Fleck/WebSocketServer.cs @@ -43,7 +43,6 @@ public WebSocketServer(int port, string location) public X509Certificate2 Certificate { get; set; } public SslProtocols EnabledSslProtocols { get; set; } public IEnumerable SupportedSubProtocols { get; set; } - public bool RestartAfterListenError { get; set; } public bool IsSecure { @@ -100,24 +99,7 @@ public void Start(Action config) private void ListenForClients() { - ListenerSocket.Accept(OnClientConnect, e => { - + FleckLog.Error("Listener socket is closed", e); - + if(RestartAfterListenError){ - + FleckLog.Info("Listener socket restarting"); - + try - + { - + ListenerSocket.Dispose(); - + var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); - + ListenerSocket = new SocketWrapper(socket); - + Start(_config); - + FleckLog.Info("Listener socket restarted"); - + } - + catch (Exception ex) - + { - + FleckLog.Error("Listener could not be restarted", ex); - + } - + } - + }); + ListenerSocket.Accept(OnClientConnect, e => FleckLog.Error("Listener socket is closed", e)); } private void OnClientConnect(ISocket clientSocket) From 8766a15358ca39dd3932d49d776591cd23e63619 Mon Sep 17 00:00:00 2001 From: Mike Olsen Date: Wed, 24 Aug 2016 23:59:30 -0700 Subject: [PATCH 3/3] add RestartAfterListenError to readme --- readme.markdown | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/readme.markdown b/readme.markdown index c9cacc6d..4c6ba2cc 100644 --- a/readme.markdown +++ b/readme.markdown @@ -108,6 +108,20 @@ server.Start(socket => }); ``` +Auto Restart After Listen Error +--- + +Set `RestartAfterListenError` to `true` on the `WebSocketConnection` + +```cs +var server = new WebSocketServer("ws://0.0.0.0:8181"); +server.RestartAfterListenError = true; +server.Start(socket => +{ + //...use as normal +}); +``` + License ---