1

I am very much new to XSockets in .Net.

Following is my Server Code.

public class MyChatController : XSocketController
{

    public void Foo(ITextArgs textArgs)
    {
        this.SendToAll(textArgs);
    }

}

Following is Client side Code

   static void Main(string[] args)
    {
        var client = new XSocketClient("ws://127.0.0.1:4502/MyChat","*");

        client.OnOpen += (sender, eventArgs) => Console.WriteLine("OPEN");
        client.Bind("foo", message=>
        {
            dynamic data = Newtonsoft.Json.JsonConvert.DeserializeObject(message.data);

            var array = data;

            byte[] bytes = Convert.FromBase64String(array);

            Stream readStream = new MemoryStream(bytes);//videovm.video
            var fileName = "C:\\Users\\NandaKishore\\Documents\\Visual Studio 2013\\Projects\\CSClientApp\\CSClientApp\\somefile" + ".mp4";

            string targetPath =  fileName;

            FileStream writeStream = new FileStream(targetPath, FileMode.Create, FileAccess.Write);

            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();

            Console.WriteLine("done dona done");
        });
        client.Open();





        ConsoleKeyInfo cki;            
        Console.WriteLine("Press the Escape (Esc) key to quit and any other key to send a message: \n");

        var _FileName = "C:\\Users\\NandaKishore\\Documents\\Visual Studio 2013\\Projects\\CSClientApp\\CSClientApp\\tt.mp4";

        byte[] _Buffer = null;
        try
        {
            System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream);
            long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
            _Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
            _FileStream.Close();
            _FileStream.Dispose();
            _BinaryReader.Close();
        }
        catch (Exception _Exception)
        {
            Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
        }

        do
        {
            cki = Console.ReadKey();
            if (cki.Key != ConsoleKey.Escape) {

                var dd = Convert.ToBase64String(_Buffer);
                client.Send(dd, "foo");
            }
        } while (cki.Key != ConsoleKey.Escape);
    }

It works fine for small images and videos but with large video it fails. Is there a better way to pass large files such as byte serving or anything.

Any guidance would be helping.

Tinku Chacko
  • 520
  • 1
  • 6
  • 20

1 Answers1

0

WebSockets is not suitable for streaming large files but can be achieved if you send the files in small chunks. When doing so, you should never use Base64 strings since the average size of Base64 will be ~36 % larger then the actual file.

So slice the file and send it as a array of bytes, and also use the binary methods of XSockets. In your example code you are using ITextArgs instead of IBinaryArgs, and that is also why you have been using Base64...

You can read more about binary messages here

Having both ITextArgs and IBinaryArgs is a bit strange, but since websockets has a pretty poor support for binary messages this is the way it is currently done in XSockets. As of version 4.0 (not yet released) IMessage will replace both ITextArgs and IBinaryArgs with much improved support for handling all types of messages.

Uffe
  • 2,275
  • 1
  • 13
  • 9