Skip to content

Instantly share code, notes, and snippets.

@codeplanner
Created October 23, 2012 16:26
Show Gist options
  • Save codeplanner/3939883 to your computer and use it in GitHub Desktop.
Save codeplanner/3939883 to your computer and use it in GitHub Desktop.
XSockets Read/Write Storage example
using XSockets.Core.Common.Socket;
using XSockets.Core.XSocket;
using XSockets.Core.XSocket.Helpers;
namespace XSocketHandler
{
/// <summary>
/// RealTime Controller showing howto use Storage to persist data between connections.
///
/// This is only to show session-like persistence between connections.
/// We did not provide any public actionmethods for communicating with the storage here.
///
///
/// HTML for connecting to this controller...
/// <!DOCTYPE html>
///<html>
///<head>
/// <title></title>
/// <script src="Scripts/jquery-1.7.2.js" type="text/javascript"></script>
/// <script src="Scripts/jXSockets.2.0.3.js" type="text/javascript"></script>
///
///<script>
/// var ws = null;
///
/// $(function () {
///
/// ws = new XSockets.WebSocket("ws://127.0.0.1:4502/StorageController");
///
/// ws.bind(XSockets.Events.open, function (client) {
/// console.log('open',client);
/// });
///
/// ws.bind(XSockets.Events.onError, function (err) {
/// console.log('error',err);
/// });
/// });
///
/// </script>
///</head>
///<body>
///
///</body>
///</html>
/// </summary>
public class StorageController : XSocketController
{
//Object that we want to keep state on in the controller.
private Person _person;
public StorageController()
{
this._person = new Person();
//Listen for new connection
this.OnClientConnect += StorageController_OnClientConnect;
}
/// <summary>
/// On connect the client will check storage for data... If there is no data the client will save new data.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void StorageController_OnClientConnect(object sender, XSockets.Core.Common.Socket.Event.Arguments.OnClientConnectArgs e)
{
this._person = this.GetFromStorage<Person>("Person");
if(this._person == null)
{
var storageInfo = new Person {Age = 35, Name = "Uffe"};
//Add new PersonInformation to the storage...
this.WriteToStorage("Person", storageInfo);
}
}
}
/// <summary>
/// Extensions for IXBaseSocket
/// </summary>
public static class Helpers
{
/// <summary>
/// Lock object for read write to storage
/// </summary>
private static readonly object Locker = new object();
/// <summary>
/// Write to server storage (memory)
/// </summary>
/// <param name="socket"> </param>
/// <param name="key"></param>
/// <param name="obj"></param>
public static void WriteToStorage<T>(this T socket, string key, object obj) where T : IXBaseSocket
{
lock(Locker)
{
socket.StorageSet(key, obj);
}
}
/// <summary>
/// Read from server storage (memory)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="socket"> </param>
/// <param name="key"></param>
/// <returns></returns>
public static T GetFromStorage<T>(this IXBaseSocket socket, string key)
where T : class
{
lock (Locker)
{
return socket.StorageGet(key) as T;
}
}
}
/// <summary>
/// Simple object to store in serverside storage, but this could be anything.
/// </summary>
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment