예제 #1
0
 public async Task RunWebSocket(IDictionary <string, object> websocketContext)
 {
     if (websocketContext.TryGetValue(typeof(WebSocketContext).FullName, out object value))
     {
         WebSocket webSocket = ((WebSocketContext)value).WebSocket;
         ClientInfo.ClientStream = new PipeNetworkStream(new WebsocketStream(webSocket));
         if (((WebSocketContext)value).Headers["SignalgoDuplexWebSocket"] == "true")
         {
             ClientInfo.StreamHelper = SignalGoStreamWebSocketLlight.CurrentWebSocket;
             await HttpProvider.AddSignalGoWebSocketHttpClient(ClientInfo, CurrentServerBase);
         }
         else
         {
             await HttpProvider.AddWebSocketHttpClient(ClientInfo, CurrentServerBase);
         }
     }
     else
     {
         //mWebSocket = new OwinWebSocket(websocketContext);
     }
 }
        public async Task Invoke(HttpContext context)
        {
            //context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
            //context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
            //// Added "Accept-Encoding" to this list
            //context.Response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Accept-Encoding, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name");
            //context.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET,PUT,PATCH,DELETE,OPTIONS");
            //// New Code Starts here
            //if (context.Request.Method == "OPTIONS")
            //{
            //    context.Response.StatusCode = (int)HttpStatusCode.OK;
            //    return _next.Invoke(context);
            //}

            string uri          = context.Request.Path.Value + context.Request.QueryString.ToString();
            string serviceName  = uri.Contains('/') ? uri.Substring(0, uri.LastIndexOf('/')).Trim('/') : "";
            bool   isWebSocketd = context.Request.Headers.ContainsKey("Sec-WebSocket-Key");

            if (!BaseProvider.ExistService(serviceName, CurrentServerBase) && !isWebSocketd && !context.Request.Headers.ContainsKey("signalgo") && !context.Request.Headers.ContainsKey("signalgo-servicedetail") && context.Request.Headers["content-type"] != "SignalGo Service Reference")
            {
                await _next.Invoke(context);

                return;
            }

            OwinClientInfo owinClientInfo = new OwinClientInfo(CurrentServerBase);

            owinClientInfo.ChangeStatusAction = (code) =>
            {
                context.Response.StatusCode = code;
            };

            owinClientInfo.ConnectedDateTime = DateTime.Now;
            owinClientInfo.IPAddressBytes    = context.Connection.RemoteIpAddress.GetAddressBytes();
            owinClientInfo.ClientId          = Guid.NewGuid().ToString();
            CurrentServerBase.Clients.TryAdd(owinClientInfo.ClientId, owinClientInfo);

            //owinClientInfo.OwinContext = context;
            owinClientInfo.RequestHeaders  = new HttpHeaderCollection(context.Request.Headers);
            owinClientInfo.ResponseHeaders = new HttpHeaderCollection(context.Response.Headers);
            if (context.Request.Headers.ContainsKey("signalgo") && context.Request.Headers["signalgo"] == "SignalGoHttpDuplex")
            {
                context.Response.Headers["Content-Length"] = "170";
                context.Request.EnableRewind();
                context.Request.EnableBuffering();
                owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase;
                owinClientInfo.ClientStream = new PipeNetworkStream(new DuplexStream(context.Request.Body, context.Response.Body));
                owinClientInfo.ProtocolType = ClientProtocolType.HttpDuplex;
                if (context.Request.Headers["SignalgoDuplexWebSocket"] == "true")
                {
                    owinClientInfo.StreamHelper = SignalGoStreamWebSocketLlight.CurrentWebSocket;
                    await HttpProvider.AddSignalGoWebSocketHttpClient(owinClientInfo, CurrentServerBase);
                }
                else
                {
                    await HttpProvider.AddSignalGoWebSocketHttpClient(owinClientInfo, CurrentServerBase);
                }
                await Task.FromResult <object>(null);
            }
            else if (isWebSocketd)
            {
                owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase;
                bool      web       = context.WebSockets.IsWebSocketRequest;
                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                owinClientInfo.ClientStream = new PipeNetworkStream(new WebsocketStream(webSocket));
                if (context.Request.Headers["SignalgoDuplexWebSocket"] == "true")
                {
                    owinClientInfo.StreamHelper = SignalGoStreamWebSocketLlight.CurrentWebSocket;
                    await HttpProvider.AddSignalGoWebSocketHttpClient(owinClientInfo, CurrentServerBase);
                }
                else
                {
                    await HttpProvider.AddWebSocketHttpClient(owinClientInfo, CurrentServerBase);
                }
                await Task.FromResult <object>(null);
            }
            else
            {
                owinClientInfo.StreamHelper = SignalGoStreamBase.CurrentBase;
                owinClientInfo.ClientStream = new PipeNetworkStream(new DuplexStream(context.Request.Body, context.Response.Body));
                await HttpProvider.AddHttpClient(owinClientInfo, CurrentServerBase, uri, context.Request.Method, null, null);
            }
        }