How to Send File to SignalR hub From websocket-sharp Client?

Saurabh Sashank picture Saurabh Sashank · Sep 19, 2016 · Viewed 8.7k times · Source

We are using the websocket-sharp in one of our applications which establishes a websocket connection with our SignalR Hub on the server, we are able to send the text messages and receive the response for the same but unable to post a byte[].

Code for sending text to server is this:

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);
}

When we try to send byte[] using the below code, its not going through:

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;
    }
}

Any help with this? How can I set my hub name in _ws.sendAsync()?

Answer

Benjamin Soulier picture Benjamin Soulier · Sep 19, 2016

Sending big files is not really what SignalR is meant for.

SignalR is good for real-time messaging purposes between server & clients, for a rather small sized messages (as messahe size has a real impact on performance).

For such a need I would look into ASP.NET Web API, especially by using chunked upload (splitting file in multiple pieces to avoid connection interruptions)