Skip to content

Commit 28b12c4

Browse files
blackspherefollowerqdot
authored andcommitted
Suppressing websocket exceptions that don't close the connections
This should quite down the "Not GET" notices.
1 parent 3877a40 commit 28b12c4

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

Buttplug.Apps.WebsocketServerGUI/WebsocketServerControl.xaml.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,21 @@ public WebsocketServerControl(IButtplugServerFactory bpFactory)
7777
private void WebSocketExceptionHandler(object aObj, [NotNull] UnhandledExceptionEventArgs aEx)
7878
{
7979
var errorMessage = (aEx.ExceptionObject as Exception)?.Message ?? "Unknown";
80-
if (_secure &&
81-
(errorMessage.Contains("An established connection was aborted by the software in your host machine") ||
82-
errorMessage.Contains("Not GET request")))
80+
81+
if (_secure && errorMessage.Contains("Not GET request") && _ws != null && !aEx.IsTerminating)
8382
{
84-
errorMessage += "\n\nThis usually means that the client/browser has not accepted our SSL certificate. Try hitting the test button on the \"Websocket Server\" tab.";
83+
_log.LogException(aEx.ExceptionObject as Exception, true, errorMessage);
84+
return;
8585
}
86-
else if (_secure && errorMessage.Contains("The handshake failed due to an unexpected packet format"))
86+
87+
if (_secure && errorMessage.Contains("The handshake failed due to an unexpected packet format"))
8788
{
8889
errorMessage += "\n\nThis usually means that the client/browser tried to connect without SSL. Make sure the client is set use the wss:// URI scheme.";
8990
}
91+
else if (_secure)
92+
{
93+
errorMessage += "\n\nThis could mean that the client/browser has not accepted our SSL certificate. Try hitting the test button on the \"Websocket Server\" tab.";
94+
}
9095

9196
_log.LogException(aEx.ExceptionObject as Exception, true, errorMessage);
9297
}

Buttplug.Components.WebsocketServer/ButtplugWebsocketServer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ public class ButtplugWebsocketServer
4444
[NotNull]
4545
private CancellationTokenSource _cancellation;
4646

47+
public bool IsConnected => _server.IsStarted;
48+
4749
public void StartServer([NotNull] IButtplugServerFactory aFactory, int aPort = 12345, bool aLoopBack = true, bool aSecure = false, string aHostname = "localhost")
4850
{
4951
_cancellation = new CancellationTokenSource();
@@ -71,17 +73,18 @@ private async Task AcceptWebSocketClientsAsync(WebSocketListener aServer, Cancel
7173
{
7274
while (!aToken.IsCancellationRequested)
7375
{
76+
WebSocket ws = null;
7477
try
7578
{
76-
var ws = await aServer.AcceptWebSocketAsync(aToken).ConfigureAwait(false);
79+
ws = await aServer.AcceptWebSocketAsync(aToken).ConfigureAwait(false);
7780
if (ws != null)
7881
{
7982
Task.Run(() => HandleConnectionAsync(ws, aToken));
8083
}
8184
}
8285
catch (Exception aEx)
8386
{
84-
OnException?.Invoke(this, new UnhandledExceptionEventArgs(aEx, false));
87+
OnException?.Invoke(this, new UnhandledExceptionEventArgs(aEx, !(ws?.IsConnected ?? false)));
8588
}
8689
}
8790
}

0 commit comments

Comments
 (0)