2

I am working on WebSocket server implementation on Xamarin for an Android project, here I have Monoframework MDK 3.10.0.23 and Xamarin.android 4.18 which are latest ones.

For server implementation I'm using HttpListener and HttpListenerContext and evaluating the context.Request.IsWebSocketrequest, which is always getting false, when I run it on a Google Nexus 10.

        {
            Console.WriteLine("WS Server Started Waiting for connection");
            HttpListenerContext httpListenerContext = await httpListener.GetContextAsync();

            if (!httpListenerContext.Request.IsWebSocketRequest)
            {
                WebSocketContext webSocketContext;
                Console.WriteLine("Got a Valid WebSocket connection request");
                try
                {
                    webSocketContext = await httpListenerContext.AcceptWebSocketAsync(subProtocol:null);
                }
                catch (Exception e)
                {
                    httpListenerContext.Response.StatusCode = 500;
                    httpListenerContext.Response.Close();
                    Console.WriteLine("Exception: {0}", e);
                    return;
                }
                WebSocket webSocket = webSocketContext.WebSocket;
                if (webSocket == null)
                    Console.WriteLine ("WebSocket obj is null");
                //ProcessRequest(httpListenerContext);
            }
            else
            {
                Console.WriteLine("Got a Bad WebSocket connection request");
                httpListenerContext.Response.StatusCode = 400;
                httpListenerContext.Response.Close();
            }
        }

Please help and suggest the problem here creating the HttpListener object for http:// local host at 9090

0

1 Answer 1

0

websockets initiates connection via http with the request to upgrade. if the server do not reply with the 101 response code, it is a sign that there is a problem in connection. the request to connect via websockets uses ws://yourdomain.com/nameOfApplicationEndPoint or wss in cases of ssl connection. i used this for my android project org.java-websocket:Java-WebSocket:1.3.0 and you can get a simple example here http://www.elabs.se/blog/66-using-websockets-in-native-ios-and-android-apps

1
  • Hi , my main objective is to implement a WebSocket server using c# for a Android application at latest Xamarin Studio and Xamarin.Android framework updated. Is it possible/Supported ? Another question If possible then how to implement it. Can we use System.Net.WebSockets classes to implement it. Please give some examples. NOTE I am using these : MonoFramework MDk 3.10.0.23 , Xamarin Studio 5.5.3 and Xamarin.Android 4.18.1.3 Nov 13, 2014 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.