Trying to use System.Net.WebSockets.Websocket
with ASP.Net Core, but getting a problem when receiving Chinese text.
This is my code:
while (_socket.State == WebSocketState.Open)
{
var result = await _socket.ReceiveAsync(_reciveBufferSegment, _source.Token);
if (_source.Token.IsCancellationRequested) return;
if (result.MessageType != WebSocketMessageType.Close)
{
if (_source.Token.IsCancellationRequested) return;
await _recivedData.WriteAsync(_reciveBuffer, 0, result.Count);
if (result.EndOfMessage)
{
_recivedData.Position = 0;
var data = _recivedData.ToArray();
_recivedData.SetLength(0);
switch (result.MessageType)
{
case WebSocketMessageType.Binary:
if (OnBinaryMessage != null)
Task.Run(() => OnBinaryMessage(this, data));
break;
case WebSocketMessageType.Text:
string decode = null;
try
{
decode = Encoding.UTF8.GetString(data);
}
catch
{
decode = "UTF8 decode failure";
}
if (OnTextMessage != null)
Task.Run(() => OnTextMessage(this, decode));
break;
}
}
}
}
If client sends a 50 bytes text with multiple-byte characters and server receive with buffer smaller than 50 and will cut multiple-byte characters, an Invalid UTF-8
exception will occur.
I port the same code to .Net Framework 4.52 and host on IIS and it works fine, did I miss something? Or any config that can disable the utf-8 check?