2016-04-08 30 views
1

GÜNCELLEMEAzure/yerel ASP.NET 5 WebSocket'e giderme

Ben Fleck vazgeçmiş ve WebSocket bağlantıları kendim işlemek için karar verdik. Aşağıda kodumu güncelledim. Bu soru şu anda Azure hakkında daha az şey değil, ama hala ASP.NET 5/Middlewares/etc'de yapılan değişikliklerle ilgili olarak 'konuyla ilgili' olduğunu hissediyorum. istemeden dağıtımı Azure'a etkiler.

Sorun: Aşağıdaki kodumla, şimdi bir websocket bağlantısı açtım ve tarayıcı konsolunda gördüğüm, oluşturulduğunda "ping" yazılı bir metin iletisi gönderiyorum. Sorun şu ki, bağlantı hala açık olsa da ek mesaj gönderme girişimlerim işe yaramıyor. Bu sorun yerel olarak ve Azure'da gerçekleşir.

Startup.cs - Bir WebSocket Ara Katman

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); 
    } 

JavaScript İlgili

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);}; 

Herhangi fikirleri ortaya paketi "Microsoft.AspNet.WebSockets.Server" eklendi?

+0

Nasıl Azure konak uygulamanızın üzerinde değil mi? Azure'da kullanılabilecek birkaç farklı hizmet var. – Hypnobrew

+0

Visual Studio'dan dağıtılan bir "Azure web uygulaması hizmeti" dir, genellikle şu bağlantıyı takip edebilirsiniz: https://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started/ –

cevap

1

Web yuvalarını Azure portalından etkinleştirdiniz mi? Varsayılan olarak, Azure web yuvalarını devre dışı bırakır ve sitenizin ayarlarında bunu açıkça etkinleştirmeniz gerekir.

Portal.azure.com sayfasından sitenizin blade'ine gidin, ardından şuraya gidin: Tüm ayarlar -> Uygulama ayarları. Web Sockets altında, On'a tıklayın.

Ardından, Kaydet'i tıklatın.

fazla bilgi için buraya bakınız: https://azure.microsoft.com/en-us/blog/introduction-to-websockets-on-windows-azure-web-sites/

+0

Evet, bunu yaptım. Aslında bu kesin bağlantıyı kullandım –