none
How to get the Sec-WebSocket-Key for WebSocket connection request? RRS feed

  • Question

  • Hi,

    I am new to WebSocket. But i am in need to create a  WebSocket connection. I need to send a request header Sec-WebSocket-Key. I searched over google about this. but i got confused.

    where to calculate this from ? or where should i get it from ?

    Thanks.

    Tuesday, June 4, 2019 11:45 AM

Answers

  • Hi PM16,

    Thank you for posting here.

    Based on your description, you want to get the Sec-WebSocket-Key for WebSocket connection.

    I make a simple code, you could have a look.

    Client Code:

    static void Main(string[] args)
            {
                using (var ws = new WebSocket("ws://localhost:9998/service"))
                {
                    ws.OnMessage += (sender, e) =>
                      Console.WriteLine("Laputa says: " + e.Data);
    
                    ws.Connect();
                    Console.ReadKey(true);
                }
            }

    I used WebScoket from this link.

    Server code:

     static void Main(string[] args)
            {
                TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 9998);
                server.Start();
                var client = server.AcceptTcpClient();
                var stream = client.GetStream();
    
                while (true)
                {
                    var buffer = new byte[1024];
                    // wait for data to be received
                    var bytesRead = stream.Read(buffer, 0, buffer.Length);
                    var r = System.Text.Encoding.UTF8.GetString(buffer);
                    foreach (var item in r.Split('\n'))
                    {
                        if(item.Contains("Sec-WebSocket-Key"))
                        {
                            Console.WriteLine(item);
                            string m = item.Split(':')[1].Trim();
                            Console.WriteLine(m);
                        }
                    }
                }
            }

    Result:


    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 5, 2019 3:18 AM
    Moderator

All replies

  • Hi PM16,

    Thank you for posting here.

    Based on your description, you want to get the Sec-WebSocket-Key for WebSocket connection.

    I make a simple code, you could have a look.

    Client Code:

    static void Main(string[] args)
            {
                using (var ws = new WebSocket("ws://localhost:9998/service"))
                {
                    ws.OnMessage += (sender, e) =>
                      Console.WriteLine("Laputa says: " + e.Data);
    
                    ws.Connect();
                    Console.ReadKey(true);
                }
            }

    I used WebScoket from this link.

    Server code:

     static void Main(string[] args)
            {
                TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"), 9998);
                server.Start();
                var client = server.AcceptTcpClient();
                var stream = client.GetStream();
    
                while (true)
                {
                    var buffer = new byte[1024];
                    // wait for data to be received
                    var bytesRead = stream.Read(buffer, 0, buffer.Length);
                    var r = System.Text.Encoding.UTF8.GetString(buffer);
                    foreach (var item in r.Split('\n'))
                    {
                        if(item.Contains("Sec-WebSocket-Key"))
                        {
                            Console.WriteLine(item);
                            string m = item.Split(':')[1].Trim();
                            Console.WriteLine(m);
                        }
                    }
                }
            }

    Result:


    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 5, 2019 3:18 AM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 7:20 AM
    Moderator
  • Hi Jack.

    Thanks. yes my problem solved.

    Sorry i was little bit busy with other stuffs so missed to reply.

    But i have Another problem.. i need to connect to a secured websocket url i should send the client certificate via connection..

    i have .p7b, p12, base64 .cer, DER .cer, csr and .key files. but i dont know how to send and which certificate to be sent via client connection.

    Can you please help me to solve this?

    Thanks.

    Wednesday, June 26, 2019 1:34 PM