question

mjrzvgn-7012 avatar image
0 Votes"
mjrzvgn-7012 asked RobCaplan edited

WebSocket in Background transfer and NSURLSession in Xamarin.iOS

I am looking into creating a Xamarin.iOS app that can connect to a WebSocket server when the app is backgrounded or suspended. The app is expected to close the channel once it receives a certain 'close' message from the server.

As per Apple's documentation, NSUrlSession allows to create a WebSocket task for the provided URL.

Xamarin has a short guide about using NSUrlSession, but the API reference documentation does not have any WebSocket-related methods.

A search engine search for 'xamarin nsurlsession webSocketTaskWithURL' returns zero results.

Is it possible to use WebSocket in NSUrlSession in Xamarin.iOS?


dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered

Hello,
Welcome to our Microsoft Q&A platform!

If you want to use WebSocket in Xamarin.iOS, declare a namespace(using Foundation) to access NSUrlSessionWebSocketTask,then use C# to implement it. The following code shows a sample:

 NSUrlSession session = NSUrlSession.SharedSession;
 NSUrlSessionWebSocketTask task = session.CreateWebSocketTask(new NSUrl(""));


Best Regards,
Wenyan Zhang



If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.
[1]: https://docs.microsoft.com/en-us/answers/articles/67444/email-notifications.html

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.