0

The following is the piece of code that i am using to communicate with a websocket.

The webscoket uses a XML type of protocol for communication.

I am trying to send a series of request and waiting to receive a reponse.

Here is the following code:

static void Main(string[] args)
        {
        Thread.Sleep(1000);

        Uri myUri = new Uri("ws://127.0.0.1:4848", UriKind.Absolute);
        Connect(myUri).Wait();
        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    public static async Task Connect(Uri uri)
    {
        ClientWebSocket webSocket = null;

        try
        {
            webSocket = new ClientWebSocket();
            await webSocket.ConnectAsync(uri, CancellationToken.None);

            byte[] buffer;
            byte[] receiveBuffer = new byte[receiveChunkSize];

            string[] requestStrings =
            {
                "request1",
                "request2",
                "request3"
            };


            foreach (var request in requestStrings)
            {
                buffer = encoder.GetBytes(request);

                Task sendRequest = Send(webSocket, buffer);
                await sendRequest;

                Task receiveResponse = Receive(webSocket);
                await receiveResponse;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex);
        }
    }
    static UTF8Encoding encoder = new UTF8Encoding();

    private static async Task Send(ClientWebSocket webSocket, byte[] buffer)
    {
        await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
        LogStatus(false, buffer, buffer.Length);
    }

    private static async Task Receive(ClientWebSocket webSocket)
    {
        byte[] buffer = new byte[receiveChunkSize];
        var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
            LogStatus(true, buffer, result.Count);            
    }

    private static void LogStatus(bool receiving, byte[] buffer, int length)
    {
        lock (consoleLock)
        {
            Console.ForegroundColor = receiving ? ConsoleColor.Green : ConsoleColor.Gray;

            if (verbose)
                Console.WriteLine(encoder.GetString(buffer));
            Console.ResetColor();
        }
    }
}

I am not getting any reponses from the websocket. I am not sure of what i am doing wrrong here. The output looks like the following:

Output:
request1
request2
request3
Liam
  • 27,717
  • 28
  • 128
  • 190
sankar
  • 23
  • 1
  • 6
  • the only thing that's jumping out at me is `Connect(myUri).Wait();`. See [Can't specify the 'async' modifier on the 'Main' method of a console app](https://stackoverflow.com/questions/9208921/cant-specify-the-async-modifier-on-the-main-method-of-a-console-app) – Liam Feb 18 '19 at 10:47
  • Thanks for your reply.. But i am not sure what are you trying to imply here. My problem is with me not able to get any reponses – sankar Feb 18 '19 at 11:54
  • are you also coding the other side of this? if so, can you include that code so we can work on it? otherwise provide details about what it is connecting to. – Maslow Feb 18 '19 at 12:44
  • I am not coding the other side of this. I am just trying to send a request and get the correspoding response... As the websocket is custom, i cannot share those information – sankar Feb 19 '19 at 12:07

0 Answers0