我们在我们的一个应用程序中使用 websocket-sharp,它与服务器上的 SignalR Hub 建立了 websocket 连接,我们能够发送文本消息并接收相同的响应但无法发布 byte[].
向服务器发送文本的代码是这样的:
public void TestGroupData(string groupname)
{
DataCarrier dataCarrier = new DataCarrier()
{
H = "BILHub",
M = "GetAllGroupsFor",
A = new string[] { groupname }
};
string wsPacket = JsonConvert.SerializeObject(payLoad);
this._ws.Send(wsPacket);
//this.MakeServerCall(dataCarrier);
}
当我们尝试使用下面的代码发送 byte[] 时,它没有通过:
public void TestFileData()
{
try
{
// Read the file data
Console.WriteLine("Started reading file");
string fileName = @"C:\Saurabh\Data\Song.mp3";
byte[] file = File.ReadAllBytes(fileName);
DataCarrier dataCarrier = new DataCarrier()
{
H = "BILHub",
M = "SendFile",
A = new object[] { file }
};
string wsPacket = JsonConvert.SerializeObject(dataCarrier);
this._ws.SendAsync(file , OnSendComplete);
}
catch (Exception ex)
{
Console.WriteLine(ex);
//throw;
}
}
有什么帮助吗?如何在 _ws.sendAsync() 中设置我的集线器名称?
请您参考如下方法:
Sending big files并不是 SignalR 的真正用途。
SignalR 适用于服务器和客户端之间的实时消息传递,适用于相当小的消息(因为消息大小为 real impact on performance )。
对于这种需要,我会研究 ASP.NET Web API,尤其是通过使用 chunked upload (将文件分成多个部分以避免连接中断)
