S
S
Soo2018-10-19 01:42:08
C++ / C#
Soo, 2018-10-19 01:42:08

C#, why doesn't it hook a WebSocket connection?

Good!
There is a js client that accepts WebSocket connections:

var socket = new WebSocket("ws://127.0.0.1:8005");
// ... и так далее, обработка сообщений

I have a server in C#
namespace SocketTcpServer
{
    class Program

    {
        public static async Task SendTicksRequest()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
            var ws = new ClientWebSocket();
            var uri = new Uri("ws:127.0.0.1:8005");

            await ws.ConnectAsync(uri, CancellationToken.None);

            var reqAsBytes = Encoding.GetEncoding(1251).GetBytes("{\"ticks\":\"R_100\"}");
            Console.WriteLine(reqAsBytes);
            var ticksRequest = new ArraySegment<byte>(reqAsBytes);

            await ws.SendAsync(ticksRequest,
                WebSocketMessageType.Text,
                true,
                CancellationToken.None);

            var buffer = new ArraySegment<byte>(new byte[1024]);
            var result = await ws.ReceiveAsync(buffer, CancellationToken.None);

            string response = Encoding.GetEncoding(1251).GetString(buffer.Array, 0, result.Count);
            Console.WriteLine(response);
        }

        static void Main(string[] args)
        {
            SendTicksRequest();
            Console.ReadLine();
        }
    }
}

The connection is not established. What am I doing wrong?
PS Implementation through ASP.NET, please do not offer

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question