简体   繁体   中英

Sending custom objects through signalR to cross-platform clients

Let's say I have an Instant Messenger server using SignalR. I want to broadcast a ContactList custom object that contains a collection of User custom objects to all connected clients which may be on a variety of platforms.

How do I package these custom objects on the server side to allow java, objective c and .net clients to map back to copies of these classes on their side? I assume some sort of serialization, but don't want to write a "parser" for every client language that reconstructs every different custom object I may want to use. Do I have any better options?

如果您使用SignalR,则格式为JSON。

SignalR is technically tied to JSON right now in its architecture (ie IJsonSerializer and IJsonValue ) and uses JSON.NET by default for serialization. So as long as your objects can be serialized by JSON.NET and as long as those other platforms support parsing JSON in some way (even if it's simple text parsing) then you should be good to go.

Exposing your objects in a RESTful manner will allow other languages to consume them. As long as all objects you expose are serializable, it doesn't matter what the calling language is as long as they can themselves do REST requests. WCF / Web API handle this nicely.

I'd like to add that if you want the JSON that comes in over the wire to be automatically deserialized into objects in the various platforms, you could create a PCL (portable class library) using Xamarin to run C# everywhere. Have a look at http://xamarin.com/csharp .

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