Binary Messages
XSockets has supported binary messages for a long time, but in 4.0 we have made it even easier than before.
How to handle binary data
Lets say that we have a file c:\temp\xfile.txt
with the text This file was sent with XSockets.NET
and we want to send that file to the server.
Client
Client - C#
var blob = File.ReadAllBytes(@"c:\temp\xfile.txt");
await conn.Controller("chat").Invoke("myfile", blob);
Server
//using XSockets.Core.Common.Socket.Event.Interface;
//using XSockets.Core.XSocket;
public void MyFile(IMessage message)
{
var filecontent = Encoding.UTF8.GetString(message.Blob.ToArray());
}
How to pass meta-data together with binary data
If we want to attach metadata about the binary data that is easy to do. Just pass along the object representing the metadata and XSockets will let you extract that data on the server.
Client
Client - C#
var blob = File.ReadAllBytes(@"c:\temp\xfile.txt");
await conn.Controller("chat").Invoke("myfile", blob, new {Name="xfile.txt"});
Server
//using XSockets.Core.Common.Socket.Event.Interface;
//using XSockets.Core.XSocket;
//simple class for holding metadata about a file
public class FileInfo
{
public string Name {get;set;}
}
public void MyFile(IMessage message)
{
var filecontent = Encoding.UTF8.GetString(message.Blob.ToArray());
var metadata = message.Extract<FileInfo>();
}
Just use Extract<T>
to get back to metadata attached to the binary data.