2

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()?

Saurabh Sashank
  • 280
  • 1
  • 5
  • 18
  • As suggested by @bsoulier , Use signalR to send real time message example *your mp3 is ready for download and once you receive the message download mp3 from the api* – Eldho Sep 19 '16 at 11:43
  • I´m curious. Why would you use websocket-sharp rather than SignalR client? – xleon Sep 19 '16 at 14:23
  • Our client apps are developed using xamarin and the signalr client doesn't supports websocket o the mono platfrom, that made us to use websocket-sharb. @xleon – Saurabh Sashank Feb 20 '17 at 12:24
  • Xamarin supports SSE as transport protocol and it works really well. Unless you need websockets for an specific reason. I´m also curious about how you connect to signalr with WS. I thought that wasn´t possible. Any link to docs? – xleon Feb 20 '17 at 12:28
  • 1
    SSE with AJAX for bi-directional communication will have much higher round-trip latency and higher client->server bandwidth than using a WebSocket connection. I based my logic based on the details provided here: https://github.com/sta/websocket-sharp @xleon – Saurabh Sashank Feb 23 '17 at 16:19
  • you can use [SignalGo][1] project, that is open source project and have more than features: [1]: https://github.com/SignalGo/server-net – Ali Yousefi Apr 28 '17 at 10:36
  • @SaurabhSashank Can I ask whether you solved this and how? Did you manage to set it up so your SignalR server receives `bytes[]` or a stream? – eja May 02 '19 at 20:54

1 Answers1

8

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)

Community
  • 1
  • 1
Benjamin Soulier
  • 2,223
  • 1
  • 18
  • 30
  • It will great if you add more details from the external resource – Eldho Sep 19 '16 at 11:47
  • I thought the same, but there's too much code on that one, and examples on how to chunk/merge files using ASPNET are easy to Google. – Benjamin Soulier Sep 19 '16 at 12:49
  • we already have a solution using webapi and multipart data upload for the big files, SignalR Client doesn't support websockets on xamarin (mono) that is where we are using [websocket-sharp](https://github.com/sta/websocket-sharp) on our mono clients, there is a method in websocket-sharp for sending file, `ws.SendAsync (data, completed)` , how can i make use of this method with my hub is what we are looking for. – Saurabh Sashank Sep 19 '16 at 13:09
  • Any "Hello Word" kind example for this implementation? – Adnand Jul 26 '18 at 08:10
  • Here's one article from CodeProject [here](https://www.codeproject.com/Articles/1034347/Upload-large-files-to-MVC-WebAPI-using-partitionin), but you can find other examples by looking around. – Benjamin Soulier Jul 26 '18 at 17:15