使用OWIN的Websocket [英] Websockets using OWIN

查看:103
本文介绍了使用OWIN的Websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,所有通过Microsoft WebSockets使用Microsoft WebSockets的示例都使用IIS,该实现是在get方法上进行的,HTTP连接已升级到websocket,并且websocket处理程序的实例已传递到HTTPContext

All the examples using Microsoft WebSockets over a web-api that I've seen so far use IIS, the implementation is on the get method the HTTP connection is upgraded to a websocket and an instance of websocket handler is passed to the HTTPContext

public HttpResponseMessage Get() {
  if (HttpContext.Current.IsWebSocketRequest) {
     var noteHandler = new NoteSocketHandler();
     HttpContext.Current.AcceptWebSocketRequest(noteHandler);
  }
  return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
}

要达到的目的是在OWIN管道上执行相同的操作.面临的问题是,连接已升级为使用Websocket,但未使用我的websocket处理程序.我要去哪里错了?请提出建议.

What am trying to achieve is to do the same on an OWIN pipeline. The problem am facing is the connection is being upgraded to use Websockets but it is not utilizing my websocket handler. Where am I going wrong? Please suggest.

使用OwinContext的控制器(后接示例使用OWIN的Nancy中的WebSockets ) ,

Controller utilizing OwinContext (Followed the example WebSockets in Nancy using OWIN),

public HttpResponseMessage Get() {
   IOwinContext owinContext = Request.GetOwinContext();

   WebSocketAccept acceptToken = owinContext.Get<WebSocketAccept>("websocket.Accept");
   if (acceptToken != null) {
      var requestHeaders = GetValue<IDictionary<string, string[]>>(owinContext.Environment, "owin.RequestHeaders");

      Dictionary<string, object> acceptOptions = null;
      string[] subProtocols;
      if (requestHeaders.TryGetValue("Sec-WebSocket-Protocol", out subProtocols) && subProtocols.Length > 0) {
         acceptOptions = new Dictionary<string, object>();
         // Select the first one from the client
         acceptOptions.Add("websocket.SubProtocol", subProtocols[0].Split(',').First().Trim());
      }

      acceptToken(acceptOptions, async wsEnv => {
         var wsSendAsync = (WebSocketSendAsync)wsEnv["websocket.SendAsync"];
         var wsRecieveAsync = (WebSocketReceiveAsync)wsEnv["websocket.ReceiveAsync"];
         var wsCloseAsync = (WebSocketCloseAsync)wsEnv["websocket.CloseAsync"];
         var wsCallCancelled = (CancellationToken)wsEnv["websocket.CallCancelled"];

         //should I pass the handler to an event?
         var handler = new NoteSocketHAndler();               
      });

   } else {
      return new HttpResponseMessage(HttpStatusCode.BadRequest);
   }
   return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
}

处理程序代码:

using System;
using Socket = Microsoft.Web.WebSockets;
using Newtonsoft.Json;

public class NoteSocketHandler : Socket.WebSocketHandler {
   private static Socket.WebSocketCollection connections = new Socket.WebSocketCollection();

   public NoteSocketHandler() {
   }

   public override void OnOpen() {
      connections.Add(this);
   }

   public override void OnClose() {
      connections.Remove(this);
   }

   public override void OnMessage(string message) {
      ChatMessage chatMessage = JsonConvert.DeserializeObject<ChatMessage>(message);
      foreach (var connection in connections) {
         connection.Send(message);
      }
   }
}

推荐答案

我终于想出了解决该问题的方法.您可以在下面找到代码,我也编写了一个基本应用程序,该应用程序在OWIN上使用 websockets .

I finally figured out how to resolve the issue. You can find the code below, also I've written a basic app which uses websockets on OWIN.

using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.Owin;

namespace NoteApp.WebService.Controller {
   using System;
   using System.Net.WebSockets;
   using System.Text;
   using System.Threading;
   using System.Threading.Tasks;
   using NoteApp.WebService.Handler;
   using WebSocketAccept = System.Action<
                                System.Collections.Generic.IDictionary<string, object>, // WebSocket Accept parameters
                                System.Func< // WebSocketFunc callback
                                    System.Collections.Generic.IDictionary<string, object>, // WebSocket environment
                                    System.Threading.Tasks.Task>>;
   using WebSocketCloseAsync = System.Func<
                                    int, // closeStatus
                                    string, // closeDescription
                                    System.Threading.CancellationToken, // cancel
                                    System.Threading.Tasks.Task>;
   using WebSocketReceiveAsync = System.Func<
                  System.ArraySegment<byte>, // data
                  System.Threading.CancellationToken, // cancel
                  System.Threading.Tasks.Task<
                      System.Tuple< // WebSocketReceiveTuple
                          int, // messageType
                          bool, // endOfMessage
                          int>>>; // count
   // closeStatusDescription
   using WebSocketReceiveResult = System.Tuple<int, bool, int>;
   using WebSocketSendAsync = System.Func<
                                       System.ArraySegment<byte>, // data
                                       int, // message type
                                       bool, // end of message
                                       System.Threading.CancellationToken, // cancel
                                       System.Threading.Tasks.Task>;

   public class NoteController : ApiController {
      public HttpResponseMessage Get() {
         IOwinContext owinContext = Request.GetOwinContext();

         WebSocketAccept acceptToken = owinContext.Get<WebSocketAccept>("websocket.Accept");
         if (acceptToken != null) {
            var requestHeaders = GetValue<IDictionary<string, string[]>>(owinContext.Environment, "owin.RequestHeaders");

            Dictionary<string, object> acceptOptions = null;
            string[] subProtocols;
            if (requestHeaders.TryGetValue("Sec-WebSocket-Protocol", out subProtocols) && subProtocols.Length > 0) {
               acceptOptions = new Dictionary<string, object>();
               // Select the first one from the client
               acceptOptions.Add("websocket.SubProtocol", subProtocols[0].Split(',').First().Trim());
            }
            acceptToken(acceptOptions, ProcessSocketConnection);


         } else {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
         }
         return new HttpResponseMessage(HttpStatusCode.SwitchingProtocols);
      }

      private async Task ProcessSocketConnection(IDictionary<string, object> wsEnv) {
         var wsSendAsync = (WebSocketSendAsync)wsEnv["websocket.SendAsync"];
         var wsCloseAsync = (WebSocketCloseAsync)wsEnv["websocket.CloseAsync"];
         var wsCallCancelled = (CancellationToken)wsEnv["websocket.CallCancelled"];
         var wsRecieveAsync = (WebSocketReceiveAsync)wsEnv["websocket.ReceiveAsync"];

         //pass the sendasync tuple and the cancellation token to the handler. The handler uses the sendasync method to send message. Each connected client has access to this
         var handler = new NoteSocketHandler(wsSendAsync, CancellationToken.None);
         handler.OnOpen();
         var buffer = new ArraySegment<byte>(new byte[100]);
         try {
            object status;
            while (!wsEnv.TryGetValue("websocket.ClientCloseStatus", out status) || (int)status == 0) {
               WebSocketReceiveResult webSocketResultTuple = await wsRecieveAsync(buffer, CancellationToken.None);                   
               int count = webSocketResultTuple.Item3;

               handler.OnMessage(Encoding.UTF8.GetString(buffer.Array, 0, count));
            }
         } catch (Exception ex) {
            Console.WriteLine(ex.Message);
            throw ex;
         }
         handler.OnClose();
         await wsCloseAsync((int)WebSocketCloseStatus.NormalClosure, "Closing", CancellationToken.None);
      }

      T GetValue<T>(IDictionary<string, object> env, string key) {
         object value;
         return env.TryGetValue(key, out value) && value is T ? (T)value : default(T);
      }


   }
}

这篇关于使用OWIN的Websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆