F
F
FBR2017-06-25 22:17:05
Android
FBR, 2017-06-25 22:17:05

How to implement a WebSocket client using standard Xamarin tools?

Hello.
Began to deal with the Xamarin Studio environment. I set myself the goal of writing a simple WebSocket client for Android . It's good to see the documentation .
The information is there, so you can google it. It seems to have googled examples for pure c#, for example:

  1. A WebSocket Primer
  2. Websocket - Server Using HttpListener and Client W...
But I don't know if these 2 links will fit under Xamarin or not.
And actually on StackOverFlow , a person did something similar.
But honestly, how to combine all this information, I have no idea yet. That's why I decided to write to this forum for the first time.
Can you please direct me on the right path how to file a client in order to connect to a remote server in the future? I have not written it yet, I want to at least deal with the client. Thank you!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Silin, 2017-06-26
@byme

I don't have Xamarin installed right now, so this is a console example, but I'm 99.9% sure it will work there too. The example is purely for demonstration, there is both a server and a client at once.

using System;
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServerLaunchAsync();
            ClientLaunchAsync();
            Console.ReadKey();
        }

        private static async void ServerLaunchAsync()
        {
            var httpListener = new HttpListener();
            httpListener.Prefixes.Add("http://localhost:5000/");
            httpListener.Start();
            HttpListenerContext listenerContext = await httpListener.GetContextAsync();
            if (listenerContext.Request.IsWebSocketRequest)
            {
                WebSocketContext webSocketContext = await listenerContext.AcceptWebSocketAsync(null);
                WebSocket webSocket = webSocketContext.WebSocket;

                // Do something with WebSocket
                var buffer = new byte[1000];
                var segment = new ArraySegment<byte>(buffer);
                var result = await webSocket.ReceiveAsync(segment, CancellationToken.None);
                Console.WriteLine(Encoding.UTF8.GetString(segment.Array));
            }
            else
            {
                listenerContext.Response.StatusCode = 426;
                listenerContext.Response.Close();
            }
        }

        private static async void ClientLaunchAsync()
        {
            ClientWebSocket webSocket = null;
            webSocket = new ClientWebSocket();
            await webSocket.ConnectAsync(new Uri("ws://localhost:5000"), CancellationToken.None);
            
            // Do something with WebSocket

            var arraySegment = new ArraySegment<byte>(Encoding.UTF8.GetBytes("Hello"));
            await webSocket.SendAsync(arraySegment, WebSocketMessageType.Text, true, CancellationToken.None);
        }
    }
}

W
wcoder, 2017-11-04
@wcoder

Blog article Xamarin: Developing Real-Time Communication Apps with WebSocket
PCL websocket library: https://github.com/NVentimiglia/WebSockets.Pcl

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question