2

We need to integrate the WebSocket client into the Sitecore project. We are using controller rendering, but I am not able to understand how to establish duplex communication with a WebSocket server. I am able to create a WebSocket client in a console application, but the same need to be integrated into Controller rendering.

I have a requirement to get data from third-party organizations to retrieve data from their WebSocket. For that I have two options:

  1. Client-Side: I cannot go with this approach as security concerns.
  2. Server-Side: For the server Side, I am trying to get the data from WebSocket. For the same, I have written the below logic and invoked it.
 public static void Main(string[] args)
            {
                RunWebSockets().GetAwaiter().GetResult();
            }

        private static async Task RunWebSockets()
        {
            var client = new ClientWebSocket();
            //wss://echo.websocket.org
            await client.ConnectAsync(new Uri("wss://*****.execute-api.ap-south-1.****.com/dev"), CancellationToken.None);

            Console.WriteLine("Connected!");

            var sending = Task.Run(async () =>
            {
                string line;
                while ((line = Console.ReadLine()) != null && line != String.Empty)
                {
                    var bytes = Encoding.UTF8.GetBytes(line);
                    await client.SendAsync(new ArraySegment(bytes), WebSocketMessageType.Text, true, CancellationToken.None);
                }

                await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
            });

            var receiving = Receiving(client);

            await Task.WhenAll(sending, receiving);
        }

        private static async Task Receiving(ClientWebSocket client)
        {
            var buffer = new byte[1024 * 4];

            while (true)
            {
                var result = await client.ReceiveAsync(new ArraySegment(buffer), CancellationToken.None);

                if (result.MessageType == WebSocketMessageType.Text)
                    Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, result.Count));

                else if (result.MessageType == WebSocketMessageType.Close)
                {
                    await client.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
                    break;
                }
            }
        }

I am not able to establish a connection web socket. Does Sitecore provide any WebSocket integration?

4
  • 1
    I love websockets, but I'm confused what you're trying to achieve. Do you want your user to establish a websocket connection? Is the amazonaws endpoint a protected endpoint? Have you thought about proxying it with SignalR? – Steve McGill May 17 at 9:51
  • I am getting issue in establishing Web Socket connection. No, it's not aws endpoint. – Karan Kumar May 17 at 10:54
  • in iis did you enable option web socket protocol ? – Avinash J May 17 at 10:57
  • @Avinash J Yes, it is enabled in IIS – Karan Kumar May 17 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.