C# Azure/local ASP.NET 5 Websocket疑难解答

C# Azure/local ASP.NET 5 Websocket疑难解答,c#,asp.net,stackexchange.redis,C#,Asp.net,Stackexchange.redis,更新 我已经放弃了Fleck,决定自己处理websocket连接。我已经更新了我的代码,如下所示。现在,这个问题与Azure的关系不大,但我觉得它仍然是“热门话题”,因为它涉及到对ASP.NET 5/Middleware/等所做的更改,这些更改无意中影响了Azure的部署 问题:使用下面的代码,我现在成功地打开了websocket连接,并在创建时发送一条“ping”文本消息,我在浏览器控制台中看到了这条消息。问题是,尽管连接仍然打开,但我发送附加消息的尝试似乎不起作用。此问题发生在本地和Azur

更新

我已经放弃了Fleck,决定自己处理websocket连接。我已经更新了我的代码,如下所示。现在,这个问题与Azure的关系不大,但我觉得它仍然是“热门话题”,因为它涉及到对ASP.NET 5/Middleware/等所做的更改,这些更改无意中影响了Azure的部署

问题:使用下面的代码,我现在成功地打开了websocket连接,并在创建时发送一条“ping”文本消息,我在浏览器控制台中看到了这条消息。问题是,尽管连接仍然打开,但我发送附加消息的尝试似乎不起作用。此问题发生在本地和Azure上

Startup.cs--我添加了包
“Microsoft.AspNet.WebSockets.Server”
,它公开了一个websocket中间件

public class Startup
{
    public static List<WebSocket> allSockets = new List<WebSocket>();

    /* Redis PUB/SUB is where I SendAsync to my websocket(s) */
    static ConnectionMultiplexer conRedis = ConnectionMultiplexer.Connect(
        "connection string here, non local redis instance");
    static ISubscriber sub = conRedis.GetSubscriber();
    ...
    ...

    /*New WebSockets middleware*/
    app.UseWebSockets();

    /*Basic websocket handler, stolen from blog, will link*/
    app.Use(async (http, next) =>
    {
            if (http.WebSockets.IsWebSocketRequest)
            {
                var webSocket = await http.WebSockets.AcceptWebSocketAsync();
                if (webSocket != null && webSocket.State == WebSocketState.Open)
                {
                    // TODO: Handle the socket here.
                    System.Diagnostics.Debug.WriteLine("Adding to List");
                    allSockets.Add(webSocket);

                    /* This send works */
                    await webSocket.SendAsync(
                        new ArraySegment<byte>(Encoding.UTF8.GetBytes("Ping")), WebSocketMessageType.Text, true, CancellationToken.None);
                }
            }
            else
            {
                // Nothing to do here, pass downstream.  
                await next();
            }

    });

    /*Routes here*/
    app.UseMvc(routes =>
    {
        ...
        ...
    }

    ...

    public static void Main(string[] args)
    {
       /* This is from StackExchange.Redis package */
       /* I need the PUB/SUB functionality */
       sub.Subscribe("msg", async (channel, msg) =>
        {
            var data = Encoding.UTF8.GetBytes(msg);
            var buffer = new ArraySegment<byte>(data);
            var token = CancellationToken.None;

            /* Doesn't work */
            await Task.WhenAll(allSockets.Where(s=>s.State == WebSocketState.Open)
                .Select(s => s.SendAsync(buffer, WebSocketMessageType.Text, true, token)));

            /* Doesn't work */
            foreach (var s in allSockets)
                await s.SendAsync(buffer, WebSocketMessageType.Text, true, token);
        }

        WebApplication.Run<Startup>(args);
    }

有什么想法吗?

您是否从Azure门户启用了web套接字?默认情况下,Azure禁用web套接字,您必须在站点设置中显式启用它

从portal.azure.com页面,转到站点的刀片服务器,然后导航到:所有设置->应用程序设置。在Web套接字下,单击

然后单击保存


有关更多详细信息,请参见此处:

您如何在Azure上托管应用程序?Azure中有几种不同的服务可供使用。Is是从Visual Studio部署的“Azure web app服务”,通常遵循以下链接:是的,我已经完成了。事实上,我使用了这个确切的链接
var wsImpl = window.WebSocket || window.MozWebSocket;
window.socket = new wsImpl('ws://mysitehere.azurewebsites.net/sock');
socket.onopen = function () { console.log("Connection opened"); };
socket.onclose = function () { console.log("Connection closed"); };
/* Logs 'Ping' */
socket.onmessage = function (msg) { console.log(msg);};