我在.net核心中使用websocket,我的问题是,当客户端不再连接到Internet时,他的websocket连接关闭,但是根据服务器,客户端仍处于连接状态,我没有收到任何断开连接数据包,另外,我正在Azure上发布Websocket,并且我已经设置了属性WebSocket = ON和AlwaysOn = On。请您能帮帮我!
我有以下服务器:
public async Task Invoke(HttpContext context)
{
if (context.WebSockets.IsWebSocketRequest)
{
SocketUserObject socketUser = new SocketUserObject();
if (_webSocketLogic.IsAuthorizedUser(context, ref socketUser, _operation))
{
var connection = await _webSocketHandler.OnConnected(context, socketUser, _operation);
if (connection != null)
{
await _webSocketHandler.ListenConnection(connection);
}
else
{
context.Response.StatusCode = 404;
}
}
else
{
context.Response.StatusCode = 404;
}
}
}
public async Task ListenConnection(WebSocketConnection connection)
{
var buffer = new byte[BufferSize];
try
{
while (connection.WebSocket.State == WebSocketState.Open)
{
var result = await connection.WebSocket.ReceiveAsync(
buffer: new ArraySegment<byte>(buffer),
cancellationToken: CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Text)
{
var message = Encoding.UTF8.GetString(buffer, 0, result.Count);
await ReceiveAsync(connection, message, _operation2);
}
else if (result.MessageType == WebSocketMessageType.Close)
{
await OnDisconnected(connection, _operation2);
}
}
}
catch (Exception ex)
{
_logging.TraceError(ex, _operation);
}
_logging.TraceLog(System.Reflection.MethodInfo.GetCurrentMethod(), _operation, "after State ", connection.WebSocket.State.ToString());
}
我在启动中添加了以下内容:
var webSocketOptions = new WebSocketOptions()
{
KeepAliveInterval = TimeSpan.FromSeconds(1),
ReceiveBufferSize = 4 * 1024
};
app.UseWebSockets(webSocketOptions);
我还尝试将其添加到从Kudu控制台在Azure上创建的web.config文件中:
<system.webServer>
<webSocket enabled="true" receiveBufferLimit="4194304" pingInterval="00:01:00">
</webSocket>
</system.webServer>
这是我的客户:
public void StartReceiving(int modulo)
{
RunWebSockets().GetAwaiter().GetResult();
}
private async Task RunWebSockets()
{
var client = new ClientWebSocket();
try
{
client.Options.SetRequestHeader("id_handset", "1");
await client.ConnectAsync(new Uri(url), CancellationToken.None);
Console.WriteLine("Connected!");
var sending = Task.Run(async () =>
{
string line;
if (client.State == WebSocketState.Open)
{
SocketOperation.Start(_socketOperation, client);
}
});
var receiving = Receiving(client);
await Task.WhenAll(sending, receiving);
}
catch (Exception ex)
{
if (client.State == WebSocketState.Open)
{
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
}
SocketOperation.StopConsole();
RunWebSockets().GetAwaiter().GetResult();
}
}
private async Task Receiving(ClientWebSocket client)
{
var buffer = new byte[1024 * 4];
while (true)
{
var result = await client.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Text)
Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, result.Count));
else if (result.MessageType == WebSocketMessageType.Close)
{
await client.CloseAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
SocketOperation.StopConsole();
RunWebSockets().GetAwaiter().GetResult();
//await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
break;
}
}
}