0

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?


Update 1:

Visual Studio does not know anything neither about NSUrlSessionWebSocketTask nor about .CreateWebSocketTask:

NSUrlSessionWebSocketTask in Visual Studio

(Microsoft Visual Studio Community 2017, version 15.9.25, Xamarin iOS 12.4.0.64)

1 Answers1

0

Like the link i provided in the comment, you could use NSURLSessionWebSocketTask to make a URL session task that communicates over the WebSockets protocol standard.

  NSUrlSessionConfiguration nSUrlSessionConfiguration=null;
        NSUrlSession session= NSUrlSession.FromConfiguration(nSUrlSessionConfiguration);
        NSUrl nSUrl =new NSUrl("");
        NSUrlSessionWebSocketTask task = session.CreateWebSocketTask(nSUrl);
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • I just tried to implement it as per your recommendation in a basic Xamarin Forms app. Visual Studio does not know anything neither about `NSUrlSessionWebSocketTask` nor about `.CreateWebSocketTask`, I am updating the original post with a screenshot. Could you please point to the Xamarin documentation where `NSUrlSessionWebSocketTask` and `.CreateWebSocketTask` are specified? I cannot seem to find it in the official documentation. – theTruthIsOutThere Jul 06 '21 at 17:58