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

8 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. 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
  4. 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
  5. The cost of breast surgery in Delhi varies significantly, typically ranging from INR 1,50,000 to INR 3,00,000. Key factors influencing this price include the type and complexity of the surgery, along with whether it’s performed at a private or public hospital. Surgeons' fees also play a crucial role, with more experienced professionals often charging higher rates. Additional costs stem from pre-operative diagnostics, post-operative care, and the length of the hospital stay. Follow-up consultations and medications further add to the financial burden. Opting for comprehensive treatment packages and considering insurance options can help mitigate these expenses, ensuring patients receive quality care within their budget.
    https://www.breastoncosurgery.com/services/breast-cancer-treatment-cost-in-delhi/

    ReplyDelete
  6. Dr. Vishal Shah is a highly regarded colorectal doctor in Ahmedabad, known for his expertise in managing a wide range of colorectal conditions. With over 15 years of experience, Dr. Shah specializes in minimally invasive surgical techniques, including advanced laparoscopic and robotic-assisted procedures that ensure quicker recoveries and reduced discomfort. He practices at a premier medical facility equipped with state-of-the-art technology, guaranteeing comprehensive and advanced care for his patients. Dr. Shah's personalized treatment plans are tailored to meet the individual needs of each patient, reflecting his dedication to superior patient care. His commitment to continuous learning and embracing the latest medical advancements underscores his reputation as a leading specialist. Patient testimonials often highlight his compassionate approach and meticulous attention to detail, making him a trusted name in colorectal care in Ahmedabad.
    https://drvirajlavingia.com/colorectal-cancer-specialist-in-ahmedabad

    ReplyDelete