我想使用WebSockets在UWP下创建与ASP.NET 5.0的通信。
问题在线" var response = await socket.ReceiveAsync .." 出现以下错误:
System.Exception: Exception from HRESULT: 0x80072EFE
at Windows.Networking.Sockets.MessageWebSocketMessageReceivedEventArgs.GetDataReader()
at System.Net.WebSockets.WinRTWebSocket.OnMessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
代码:
async void Initialize(Uri serverAddress)
{
tokenSource = new CancellationTokenSource();
token = tokenSource.Token;
socket = new ClientWebSocket();
try
{
await socket.ConnectAsync(serverAddress, token);
await Task.Delay(1000);
byte[] data = new byte[1024];
var response = await socket.ReceiveAsync(new ArraySegment<byte>(data), token); // crashes with error HRESULT
await Task.Delay(1000);
if (response.EndOfMessage)
{
if (response.Count >= data.Length) // error
return;
string json = Encoding.UTF8.GetString(data, 0, response.Count);
PacketData packet = JsonConvert.DeserializeObject<PacketData>(json);
}
}
catch (Exception ex)
{
if (socket != null)
{
socket.Dispose();
socket = null;
}
}
}
我做错了吗? 我尝试运行它几次..一旦我获得数据,但它崩溃了&#34;对象obj = ...&#34;我检查了json值并且它是正确的,我认为它运行正常,但有些东西我不知道是什么造成了崩溃。
由于
修改<!/强> 使用MessageWebSocket
的新代码使用以下代码时出错:&#34;操作尝试访问有效范围之外的数据&#34;
private void MSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
byte[] data = new byte[1024];
try
{
int length = 0;
using (DataReader reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
length = (int)reader.UnconsumedBufferLength; // If I create byte[] with length after then it throw same error like in original post.
reader.ReadBytes(data); // Exception jump
}
string json = Encoding.UTF8.GetString(data, 0, length);
PacketData packet = JsonConvert.DeserializeObject<PacketData>(json);
}
catch(Exception ex)
{
string a = ex.Message;
}
}
初始化:
mSocket = new MessageWebSocket();
mSocket.Control.MessageType = SocketMessageType.Binary;
mSocket.Control.MaxMessageSize = 1024;
mSocket.MessageReceived += MSocket_MessageReceived;
编辑2! 找到只有工作方式,它创建静态大小的数组。我不知道如何动态地制作它。请帮帮我!
private void MSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
byte[] data = new byte[27];
try
{
int length = 0;
using (DataReader reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
length = (int)reader.UnconsumedBufferLength;
reader.ReadBytes(data);
}
string json = Encoding.UTF8.GetString(data, 0, length);
PacketData packet = JsonConvert.DeserializeObject<PacketData>(json);
}
catch(Exception ex)
{
string a = ex.Message;
}
}
EDIT3: 添加了Array.Resize(参考数据,长度),看起来很有效,但我认为不合适。
答案 0 :(得分:2)
DataReader
类有一个ReadString
方法,我们可以使用此方法获取收到的消息,而不使用byte[]
,如下所示:
private void MSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
try
{
using (DataReader reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
string json = reader.ReadString(reader.UnconsumedBufferLength);
PacketData packet = JsonConvert.DeserializeObject<PacketData>(json);
}
}
catch (Exception ex)
{
string a = ex.Message;
}
}
如果您仍想使用byte[]
,可以尝试以下代码:
private void MSocket_MessageReceived(MessageWebSocket sender, MessageWebSocketMessageReceivedEventArgs args)
{
byte[] data;
try
{
int length = 0;
using (DataReader reader = args.GetDataReader())
{
reader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
length = (int)reader.UnconsumedBufferLength;
data = new byte[length];
reader.ReadBytes(data);
}
string json = System.Text.Encoding.UTF8.GetString(data, 0, length);
PacketData packet = JsonConvert.DeserializeObject<PacketData>(json);
}
catch (Exception ex)
{
string a = ex.Message;
}
}