Sunday, October 26, 2014

Game-development Log (10. Real-time with SignalR)


Work-in-progress: https://site-win.azurewebsites.net/ 

On this iteration I'm adding real-time notifications to the unit movement using the (awesome) SignalR library.

The idea for this will be simple: if a player moves a unit on his browser window all the others players will see that unit move in realtime on their browsers.

I've created an ultra-simple video demoing it. I've opened three separate browser windows: one with Chrome, Firefox and Safari. The movement of the units should be synchronised in real-time across the various browsers, regardless of the one that triggered the movement.


So, how was this setup? Easy as pie.

1. Add SignalR NuGet package
install-package Microsoft.AspNet.SignalR

2. Initialise SignalR on the Startup routine of ASP.NET MVC 5 (on previous versions it's slightly different)
public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.MapSignalR();
    }
}
3. Create a Hub
My hub will be ultra simple. It will simply receive a movement notification and share it with all the other SignalR clients as is.
public class UnitHub : Hub
{
    public void SetPosition(dynamic unitdata)
    {
        Clients.Others.unitUpdated(unitdata);
    }
}

4. Create Javascript code both to send a SignalR message when the player moves a unit or when a notification of another player moving a unit has been received.
//code to receive notification from server
unitUpdated = function (unit) {
 // (...)
};

// Code to submit to server
server.setPosition(unit);
5. (Optional) If you're using Azure, you should enable WebSockets on your website.
And that's it.

Please note that I haven't yet added persistence nor support for different users. Thus, everyone will be moving the same units. Anyway, I don't have that many page views on my blog for it to be a problem :)

So, what's next?
- Adding a database to persist the units position
- Adding users, each with his own units.
- Authentication with Facebook, Twitter, Google, Windows, Email

4 comments:

  1. Great Blog! HASHCRON Technologies A game is a part of a match, for example in tennis or bridge, consisting of a fixed number of points.
    She won six games to love in the second set.

    ReplyDelete
  2. Wow, this article really dives deep into the technical aspects of game development! I'm impressed by your dedication to creating a real-time weather system from scratch. It must have been challenging, but the results speak for themselves. For beginners who want to tackle similar projects, I would recommend seeking out resources and communities where you can learn and collaborate with fellow developers. And if you're looking for a platform to connect with potential partners and suppliers for your game development projects, I highly recommend checking out TradersFind, a leading B2B portal. It's a great resource for finding reliable suppliers, networking with other professionals in the industry, and accessing valuable resources to support your projects.
    For more information, visit us :- https://www.tradersfind.com/category/tile-works

    ReplyDelete