Corrected NullReferenceException due to WsReconnection event being called before the reference to the instance was created

This commit is contained in:
barelyprofessional
2024-06-17 19:22:04 +08:00
parent ec6c050e23
commit 8d28733309

View File

@@ -43,7 +43,7 @@ public class ChatClient
public async Task StartWsClient() public async Task StartWsClient()
{ {
_wsClient = await CreateWsClient(); await CreateWsClient();
} }
public void Disconnect() public void Disconnect()
@@ -51,7 +51,7 @@ public class ChatClient
_wsClient.Stop(WebSocketCloseStatus.NormalClosure, "Closing websocket").Wait(); _wsClient.Stop(WebSocketCloseStatus.NormalClosure, "Closing websocket").Wait();
} }
private async Task<WebsocketClient> CreateWsClient() private async Task CreateWsClient()
{ {
var factory = new Func<ClientWebSocket>(() => var factory = new Func<ClientWebSocket>(() =>
{ {
@@ -77,6 +77,7 @@ public class ChatClient
{ {
ReconnectTimeout = TimeSpan.FromSeconds(_config.ReconnectTimeout) ReconnectTimeout = TimeSpan.FromSeconds(_config.ReconnectTimeout)
}; };
_wsClient = client;
client.ReconnectionHappened.Subscribe(WsReconnection); client.ReconnectionHappened.Subscribe(WsReconnection);
client.MessageReceived.Subscribe(WsMessageReceived); client.MessageReceived.Subscribe(WsMessageReceived);
@@ -85,7 +86,6 @@ public class ChatClient
_logger.Debug("Websocket client has been built, about to start"); _logger.Debug("Websocket client has been built, about to start");
await client.Start(); await client.Start();
_logger.Debug("Websocket client started!"); _logger.Debug("Websocket client started!");
return client;
} }
public bool IsConnected() public bool IsConnected()