Posts

Showing posts from May, 2012

SignalR: send messages to single client

One of the problems I came across while using SignalR was to send messages to a single client. In the following example (taken from my game prototype) the static DispatchMessage() is used to send a message to a specific client. Note that the connection id's are generated via the ConnectionFactory explained in my last post (connectionId equals player.Id). public class ServerHub : Hub { private static readonly IHubContext ctx = GlobalHost.DependencyResolver.Resolve ().GetHubContext (); public static void DispatchMessage( int playerId, RpgMessage msg ) { var client = ctx.Clients[playerId.ToString()]; client.serverMessage( msg ); } // other instance method omitted ... } Here we cache the hub context in a static member. The context exposes a collection of all known clients that we use to get the desired client. Also note that the client.serverMessage() function is implemented on the client side and you should invoke it

SignalR: Wire-up the ConnectionId with a user instance

While working on a RPG game prototype based on ASP.NET I started experimenting with SignalR. This library was a perfect fit as it allows pushing data from server to clients in a very easy way (read: with just a few lines of code) and of course server method invocations from clients through JavaScript. Unfortunately SignalR is not providing a direct and intuitive way of gluing together application specific (server side) objects with the SignalR internal connections. This means that at any time you know how many clients are connected but you have absolutely no idea who they are. A client is basically a connection. A connection has a unique connectionId so you can distinguish clients but that's about it. Even worse, each time a client reconnects it gets a new connection id. In real world applications you have a bunch high level objects like: users, accounts, players etc. that are required to be in sync with client connections. So the problem is how to wire up a particular client w

Manipulating connection strings

Every once in a while I see devs struggling with connection strings.  As a rule they seem inclined to use string concatenation, string.Replace or even string.SubString In case you didn't know here is the correct way to do it:  SqlConnectionStringBuilder