2

I'm trying to host a websockets server on Azure. I'm a bit confused and was hoping you could help me.

I've followed many articles but my code is close to the one from this article : https://azure.microsoft.com/en-us/blog/introduction-to-websockets-on-windows-azure-web-sites/

public void ProcessRequest(HttpContext context)
{
    if (context.IsWebSocketRequest)
    {
        context.AcceptWebSocketRequest(ProcessWS);
    }
}

public bool IsReusable { get { return false; } }

private async Task ProcessWS(AspNetWebSocketContext context)
{
    try
    {
        WebSocket socket = context.WebSocket;
        while (true)
        {
            var url = context.RequestUri;
            ArraySegment<byte> buffer = new ArraySegment<byte>(new byte[1024]);
            WebSocketReceiveResult result = await socket.ReceiveAsync(buffer, CancellationToken.None);
            if (socket.State == WebSocketState.Open)
            {
                string userMessage = Encoding.UTF8.GetString(buffer.Array, 0, result.Count)
                                            .Trim(new char[] { ' ' }); // We remove the spaces before and after
                // DO SOMETHING
            }
            else
            {
                break;
            }
        }
    }
    catch (Exception e)
    {
        Log.Info("Exception" + e.Message + " >>>" + e.StackTrace);
    }
}

This works fine, I'm able to get the messages from my devices and answer to them.

But in some cases I need to send a message to another device, example :

DEVICE A sends "Tell Device B to blink"

Since it's a websockets server and Device B has already talked with the server I should have somewhere a connection opened with Device B. And when Device A asks me for it I can send a message to Device B.

But how can I achieve that with my code ? How can I find the connection to device B ? If not possible how should I do it ?

I hope my problem is described enough to be understood.

Thank you,

user2088807
  • 1,378
  • 2
  • 25
  • 47

1 Answers1

2

But how can I achieve that with my code ? How can I find the connection to device B ? If not possible how should I do it ?

According to your scenario, I followed this tutorial about webapi-and-websockets and implement my Web API project that could establish connection between clients. Here is the screenshoot for test, you could refer to it:

enter image description here

Additionally, you could leverage SignalR and map your client (user) to signalR connections. For more details, you could refer to Mapping SignalR Users to Connections. Also, you could refer to the git sample Microsoft.AspNet.SignalR.Samples.

UPDATE:

How would you use signalR in that case ?

For a simple way, we could retain connection and user info stored in memory, For more details, you could refer to In-memory storage. Based on this scenario, I wrote a sample project AspDotNet-SignalR-Chat, you could refer to it, and here is the screenshot for test:

enter image description here

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35
  • Thank you for your answer ! However do you think it's ok to use Microsoft.Websockets since it's not developed anymore ? – user2088807 Jul 26 '17 at 10:11
  • At least, it could work for now and it could meet the requirement. For long consideration, I recommend you using SignalR. – Bruce Chen Jul 26 '17 at 11:10
  • How would you use signalR in that case ? According to this post : https://stackoverflow.com/questions/25668398/using-websockets-with-asp-net-web-api it's not possible to inherit from the WebSocketHandler of SignalR – user2088807 Jul 26 '17 at 11:29
  • Use `WebSocketHandler` is used to handle the websocket processing by yourself. While use SignalR, you need to follow the feature provided by it. I updated my answer for your question, you could refer to it. – Bruce Chen Jul 27 '17 at 03:31