Skip to content

Instantly share code, notes, and snippets.

@MagnusThor
Created October 29, 2013 20:53
Show Gist options
  • Save MagnusThor/7222405 to your computer and use it in GitHub Desktop.
Save MagnusThor/7222405 to your computer and use it in GitHub Desktop.
This is an example how to "wrap" binary objects using JavaScript and pass them to the XSocketsController.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using XSockets.Core.Common.Socket.Event.Arguments;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
namespace XSockets.Example.BinaryWrapper
{
public class BinaryMessageWrapper
{
public Guid MessageId { get; set; }
public int ByteLength { get { return this.Buffer.Count(); } }
public IList<byte> Buffer { get; set; }
public BinaryMessageWrapper(byte[] arrayBuffer)
{
this.MessageId = Guid.Parse((Encoding.UTF8.GetString(arrayBuffer, 0, 36)));
this.Buffer = arrayBuffer.Skip(36).Take(arrayBuffer.Length - 36).ToList();
}
}
public class MyBinaryController : XSocketController
{
private static readonly List<BinaryMessageWrapper> MyBinaries;
static MyBinaryController()
{
MyBinaries = new List<BinaryMessageWrapper>();
}
public override void OnMessage(Core.Common.Socket.Event.Interface.IBinaryArgs binaryArgs)
{
var myBinary = new BinaryMessageWrapper(binaryArgs.data.ToArray());
// Just tell everyone that a blob is recieved
this.SendToAll(new { messageId = myBinary.MessageId, bytesReceived = myBinary.ByteLength }, "binaryRecieved");
// Store this in a static list , hmm , yes, its prolly better to reflect tge stuff into some other storage..
MyBinaries.Add(myBinary);
//base.OnMessage(binaryArgs); We are not sending the blob now...
}
/// <summary>
/// When a user ask's (triggers) for a message, pass back that buffer..
/// </summary>
/// <param name="messageId"></param>
public void GetBinary(Guid messageId)
{
var myBinary = MyBinaries.FirstOrDefault(b => b.MessageId.Equals(messageId));
if (myBinary == null) return;
this.Send(new BinaryArgs(myBinary.Buffer));
}
/// <summary>
/// Okey, lets remove the binary....
/// </summary>
/// <param name="messageId"></param>
public void RemoveBinary(Guid messageId)
{
var removeThis = MyBinaries.FirstOrDefault(b => b.MessageId.Equals(messageId));
MyBinaries.Remove(removeThis);
this.Send(new { messageId = messageId }, "binaryRemoved");
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using XSockets.Core.Common.Socket.Event.Arguments;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
namespace XSockets.Example.BinaryWrapper
{
public class BinaryMessageWrapper
{
public Guid MessageId { get; set; }
public int ByteLength { get { return this.Buffer.Count(); } }
public IList<byte> Buffer { get; set; }
public BinaryMessageWrapper(byte[] arrayBuffer)
{
this.MessageId = Guid.Parse((Encoding.UTF8.GetString(arrayBuffer, 0, 36)));
this.Buffer = arrayBuffer.Skip(36).Take(arrayBuffer.Length - 36).ToList();
}
}
public class MyBinaryController : XSocketController
{
private static readonly List<BinaryMessageWrapper> MyBinaries;
static MyBinaryController()
{
MyBinaries = new List<BinaryMessageWrapper>();
}
public override void OnMessage(Core.Common.Socket.Event.Interface.IBinaryArgs binaryArgs)
{
var myBinary = new BinaryMessageWrapper(binaryArgs.data.ToArray());
// Just tell everyone that a blob is recieved
this.SendToAll(new { messageId = myBinary.MessageId, bytesReceived = myBinary.ByteLength }, "binaryRecieved");
// Store this in a static list , hmm , yes, its prolly better to reflect tge stuff into some other storage..
MyBinaries.Add(myBinary);
//base.OnMessage(binaryArgs); We are not sending the blob now...
}
/// <summary>
/// When a user ask's (triggers) for a message, pass back that buffer..
/// </summary>
/// <param name="messageId"></param>
public void GetBinary(Guid messageId)
{
var myBinary = MyBinaries.FirstOrDefault(b => b.MessageId.Equals(messageId));
if (myBinary == null) return;
this.Send(new BinaryArgs(myBinary.Buffer));
}
/// <summary>
/// Okey, lets remove the binary....
/// </summary>
/// <param name="messageId"></param>
public void RemoveBinary(Guid messageId)
{
var removeThis = MyBinaries.FirstOrDefault(b => b.MessageId.Equals(messageId));
MyBinaries.Remove(removeThis);
this.Send(new { messageId = messageId }, "binaryRemoved");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment