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

7 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
  3. Purchasing YouTube subscribers can provide an immediate and impactful boost to your channel's visibility and credibility. By increasing your subscriber count, your channel becomes more attractive to new viewers, encouraging them to engage with your content. This strategy is particularly advantageous for new channels looking to establish a strong presence on the platform. With more subscribers, your videos may rank higher in search results, leading to increased organic traffic. Reputable providers ensure that the subscribers you purchase are real and active, thereby maintaining the authenticity of your audience. Moreover, a higher subscriber base can help you reach monetization thresholds more quickly, opening up revenue opportunities and potential brand collaborations. This initial surge in subscribers can set a solid foundation for sustained growth and long-term success on YouTube.
    https://www.buyyoutubesubscribers.in/

    ReplyDelete
  4. Choosing the best hosting plan in India can significantly impact your website's performance and user experience. Leading providers such as Hostinger, Bluehost, and SiteGround offer a range of options to accommodate varying needs. Hostinger is known for its cost-effective plans and excellent speed, making it an ideal choice for beginners and small businesses. Bluehost, recommended by WordPress, provides robust security features and 24/7 customer support, ensuring reliable service. SiteGround excels with its impressive uptime and superior customer service, making it suitable for larger websites. Each of these hosts provides scalable solutions, ample storage, and easy-to-use control panels, helping Indian users to maintain a seamless online presence.
    https://onohosting.com/

    ReplyDelete
  5. Dynamic Health Staff is a premier Nursing Recruitment Agency in India, renowned for its unparalleled excellence in linking skilled nursing professionals with top global healthcare institutions. They provide a comprehensive array of services, including permanent, temporary, and travel nurse placements, tailored to meet the diverse requirements of medical facilities. Dynamic Health Staff's stringent selection process guarantees the recruitment of highly competent nurses dedicated to delivering superior patient care. Emphasizing continuous professional development, they ensure their nurses remain abreast of the latest medical practices and innovations. By fostering empathy and professionalism, Dynamic Health Staff creates a nurturing environment for both staff and patients. Their efficient recruitment process and unwavering support solidify their reputation as a dependable partner in the healthcare staffing industry.
    https://dynamichealthstaff.com/

    ReplyDelete