简体   繁体   中英

How to handle WCF Duplex in Xamarin.Forms?

A little background: I'm building a simple turn-based game. There is a server hosting WCF services and client built using Xamarin.Forms (with PCL libraries). So I wanted to built very simple "request-response" server with duplex channel - make a move and wait for response (catch the response/callback in the event). BUT...The problem is, I can't see netTcp binding or any duplex/dual bindings there. Only BasicHttpBinding .

My proxy PCL library targets:

  • .NET Framework 4.5
  • ASP.NET Core 1.0
  • Windows 8
  • Windows Phone Silverlight 8
  • Xamarin.Android
  • Xamarin.iOS
  • Xamarin.iOS (Classic)

My question is: How can I make an event based service architecture (a WCF Duplex communication with Callback) - so I could consume it in Xamarin.Forms?

At this point tones of people probably want to downvote this question (or already done it), so I show what've done already:

I found ticket here and a lot of complains (there and in other places) that Xamarin is doing nothing about wcf duplex communication. This doesn't look promising. But maybe you have some good workaround this problem?

As for now, I have done it like this:

public async Task<MyResponse> Start(int id)
{
    MyResponse response;
    // doing stuff...
    return await Task.Factory.StartNew(() => response);
}

so, I'm calling asynchronously a service and basically wait till it returns. Because it's async, the client's screen is not frozen. I'm not sure it's the best solution...

The best solution would be an event based architecture (using callbacks, since you have to wait for your opponent). But I have no idea how to do it? Is it even possible?

Thanks,

WCF development has been suspended and resumed many times. I do not believe that you will see work done in this area given that REST services(Or frameworks built upon it like Service Stack) are the modern approach rather than WCF.

You can read a list of items that have no plan for support here: http://www.mono-project.com/docs/web/wcf/#components-with-no-plan-to-support (The whole article gives more history into this as well)

As for your question, there are many ways to approach a turn-based game with modern services. A quick example might be using a framework built for this. Xamarin.Android has a turn-based multiplayer package they ship with Google Play Services to do this:

https://developers.google.com/games/services/common/concepts/turnbasedMultiplayer (You can scroll down to the Client Implementations to support more than just Android)

There are also things like SignalR https://github.com/SignalR/SignalR in which you can create turn-based mechanisms with.

Finally you can also do the same with a REST service. However with a REST service, you won't be able to really capture the session state but you could model a turn based game in the sense of having enpoints such as:

/game/<gameID>/turn/<turnID>

Thus you can model a game and what the state of a player's turn is based on the turnID . It really depends on the complexity of your turn based game, but for simple things like Chess/Tic Tac Toe/Rock Paper Scissors, this is definitely possible.

In the example of waiting for turns, you can grab the latest turn and return which player's turn it is or something on those lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM