|
@@ -43,7 +43,7 @@ namespace EVCB_OCPP.WSServer.Service.WsService
|
|
|
|
|
|
//public event OCPPClientDataEventHandler<WsSession, String> m_ReceiveData;
|
|
|
|
|
|
- public event EventHandler<CloseReason> SessionClosed;
|
|
|
+ public event EventHandler<string> SessionClosed;
|
|
|
|
|
|
private CancellationTokenSource disconnectCancellationTokenSource = new CancellationTokenSource();
|
|
|
private Task ReceiveLoopTask;
|
|
@@ -62,7 +62,7 @@ namespace EVCB_OCPP.WSServer.Service.WsService
|
|
|
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), token);
|
|
|
if (result.CloseStatus.HasValue)
|
|
|
{
|
|
|
- _ = Close(CloseReason.ClientClosing);
|
|
|
+ _ = InternalClose(result.CloseStatus.Value);
|
|
|
break;
|
|
|
}
|
|
|
string received = Encoding.UTF8.GetString(buffer, 0, result.Count);
|
|
@@ -88,18 +88,35 @@ namespace EVCB_OCPP.WSServer.Service.WsService
|
|
|
ClientWebSocket.SendAsync(data, WebSocketMessageType.Text, endOfMessage: true, cancellationToken: disconnectCancellationTokenSource.Token);
|
|
|
}
|
|
|
|
|
|
- internal async Task Close(CloseReason closeReason)
|
|
|
+ internal async Task Close()
|
|
|
{
|
|
|
+ string closeReason = "ServerShutdown";
|
|
|
try
|
|
|
{
|
|
|
- await _WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, closeReason.ToString(), default);
|
|
|
+ await _WebSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, closeReason, default);
|
|
|
}
|
|
|
finally
|
|
|
{
|
|
|
_WebSocket.Dispose();
|
|
|
}
|
|
|
disconnectCancellationTokenSource.Cancel();
|
|
|
- SessionClosed?.Invoke(this, closeReason);
|
|
|
+ SessionClosed?.Invoke(this, closeReason.ToString());
|
|
|
+ EndConnSemaphore.Release();
|
|
|
+ }
|
|
|
+
|
|
|
+ private async Task InternalClose(WebSocketCloseStatus closeStatus)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ await _WebSocket.CloseAsync(closeStatus, null , default);
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ _WebSocket.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ disconnectCancellationTokenSource.Cancel();
|
|
|
+ SessionClosed?.Invoke(this, closeStatus.ToString());
|
|
|
EndConnSemaphore.Release();
|
|
|
}
|
|
|
}
|