1- using System ;
1+ using Buttplug . Components . Controls ;
2+ using Buttplug . Components . WebsocketServer ;
3+ using Buttplug . Core ;
4+ using Buttplug . Server ;
5+ using JetBrains . Annotations ;
6+ using Microsoft . Win32 ;
7+ using System ;
28using System . Collections . ObjectModel ;
39using System . Net ;
410using System . Net . NetworkInformation ;
511using System . Net . Sockets ;
12+ using System . Timers ;
613using System . Windows ;
714using System . Windows . Controls ;
8- using Buttplug . Components . WebsocketServer ;
9- using Buttplug . Core ;
10- using Buttplug . Server ;
11- using JetBrains . Annotations ;
12- using Microsoft . Win32 ;
1315using Windows . UI . Notifications ;
14- using Buttplug . Components . Controls ;
1516
1617namespace Buttplug . Apps . WebsocketServerGUI
1718{
@@ -30,6 +31,8 @@ public partial class WebsocketServerControl
3031 private bool _loopback ;
3132 private string _hostname ;
3233 private ConnUrlList _connUrls ;
34+ private Timer _toastTimer ;
35+ private string _currentExceptionMessage ;
3336
3437 public WebsocketServerControl ( IButtplugServerFactory bpFactory )
3538 {
@@ -41,6 +44,17 @@ public WebsocketServerControl(IButtplugServerFactory bpFactory)
4144 _config = new ButtplugConfig ( "Buttplug" ) ;
4245 _connUrls = new ConnUrlList ( ) ;
4346 _port = 12345 ;
47+
48+ // Usually, if we throw errors then connect, it's not actually an error.
49+ // If we don't connect after half a second of throwing an exception, pop the toaster, but not before then.
50+ _toastTimer = new Timer
51+ {
52+ Interval = 500 ,
53+ AutoReset = false ,
54+ Enabled = false ,
55+ } ;
56+ _toastTimer . Elapsed += PopToaster ;
57+
4458 if ( uint . TryParse ( _config . GetValue ( "buttplug.server.port" , "12345" ) , out uint pres ) )
4559 {
4660 _port = pres ;
@@ -76,6 +90,7 @@ public WebsocketServerControl(IButtplugServerFactory bpFactory)
7690
7791 private void WebSocketExceptionHandler ( object aObj , [ NotNull ] UnhandledExceptionEventArgs aEx )
7892 {
93+ _toastTimer . Enabled = true ;
7994 var errorMessage = ( aEx . ExceptionObject as Exception ) ? . Message ?? "Unknown" ;
8095
8196 if ( _secure && errorMessage . Contains ( "Not GET request" ) && _ws != null && ! aEx . IsTerminating )
@@ -90,14 +105,15 @@ private void WebSocketExceptionHandler(object aObj, [NotNull] UnhandledException
90105 }
91106 else if ( _secure )
92107 {
93- errorMessage += "\n \n This could mean that the client/browser has not accepted our SSL certificate. Try hitting the test button on the \" Websocket Server\" tab." ;
108+ errorMessage += "\n \n If your connection is working, you can ignore this message. Otherwise, this could mean that the client/browser has not accepted our SSL certificate. Try hitting the test button on the \" Websocket Server\" tab." ;
94109 }
95-
110+ _currentExceptionMessage = errorMessage ;
96111 _log . LogException ( aEx . ExceptionObject as Exception , true , errorMessage ) ;
97112 }
98113
99114 private void WebSocketConnectionAccepted ( object aObj , [ NotNull ] ConnectionEventArgs aEvent )
100115 {
116+ _toastTimer . Enabled = false ;
101117 Dispatcher . InvokeAsync ( ( ) =>
102118 {
103119 ConnStatus . Content = "(Connected) " + aEvent . ClientName ;
@@ -107,13 +123,36 @@ private void WebSocketConnectionAccepted(object aObj, [NotNull] ConnectionEventA
107123
108124 private void WebSocketConnectionClosed ( object aObj , [ NotNull ] ConnectionEventArgs aEvent )
109125 {
126+ _toastTimer . Enabled = false ;
110127 Dispatcher . InvokeAsync ( ( ) =>
111128 {
112129 ConnStatus . Content = "(Not Connected)" ;
113130 DisconnectButton . IsEnabled = false ;
114131 } ) ;
115132 }
116133
134+ private void PopToaster ( object aObj , ElapsedEventArgs aArgs )
135+ {
136+ _toastTimer . Enabled = false ;
137+ Dispatcher . InvokeAsync ( ( ) =>
138+ {
139+ // Use the toast system to notify the user
140+ var toastXml = ToastNotificationManager . GetTemplateContent ( ToastTemplateType . ToastText02 ) ;
141+ var tmp = toastXml . GetXml ( ) ;
142+ toastXml . SelectSingleNode ( "//*[@id='1']" ) . InnerText = "Buttplug Error" ;
143+ toastXml . SelectSingleNode ( "//*[@id='2']" ) . InnerText = _currentExceptionMessage ;
144+ var toast = new ToastNotification ( toastXml ) ;
145+ toast . Activated += OnActivatedToast ;
146+ var appId = ( string ) Registry . GetValue (
147+ @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\" + AppDomain . CurrentDomain . FriendlyName , "AppId" ,
148+ string . Empty ) ;
149+ if ( appId != null && appId . Length > 0 )
150+ {
151+ ToastNotificationManager . CreateToastNotifier ( appId ) . Show ( toast ) ;
152+ }
153+ } ) ;
154+ }
155+
117156 private void ExceptionLogged ( object aObj , [ NotNull ] LogExceptionEventArgs aEvent )
118157 {
119158 if ( aEvent . ErrorMessage != null )
@@ -122,20 +161,8 @@ private void ExceptionLogged(object aObj, [NotNull] LogExceptionEventArgs aEvent
122161 {
123162 // Show the error message in the app
124163 LastError . Text = aEvent . ErrorMessage ;
125-
126- // Use the toast system to notify the user
127- var toastXml = ToastNotificationManager . GetTemplateContent ( ToastTemplateType . ToastText02 ) ;
128- var tmp = toastXml . GetXml ( ) ;
129- toastXml . SelectSingleNode ( "//*[@id='1']" ) . InnerText = "Buttplug Error" ;
130- toastXml . SelectSingleNode ( "//*[@id='2']" ) . InnerText = aEvent . ErrorMessage ;
131- var toast = new ToastNotification ( toastXml ) ;
132- toast . Activated += OnActivatedToast ;
133- var appId = ( string ) Registry . GetValue ( @"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\AppID\" + AppDomain . CurrentDomain . FriendlyName , "AppId" , string . Empty ) ;
134- if ( appId != null && appId . Length > 0 )
135- {
136- ToastNotificationManager . CreateToastNotifier ( appId ) . Show ( toast ) ;
137- }
138164 } ) ;
165+ _toastTimer . Enabled = true ;
139166 }
140167 }
141168
@@ -296,4 +323,4 @@ public override string ToString()
296323 return WsUrl ;
297324 }
298325 }
299- }
326+ }
0 commit comments