当前位置: 首页>>代码示例>>C#>>正文


C# HttpContextBase.AcceptWebSocketRequest方法代码示例

本文整理汇总了C#中System.Web.HttpContextBase.AcceptWebSocketRequest方法的典型用法代码示例。如果您正苦于以下问题:C# HttpContextBase.AcceptWebSocketRequest方法的具体用法?C# HttpContextBase.AcceptWebSocketRequest怎么用?C# HttpContextBase.AcceptWebSocketRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.HttpContextBase的用法示例。


在下文中一共展示了HttpContextBase.AcceptWebSocketRequest方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: BeginProcessRequest

        public IAsyncResult BeginProcessRequest(HttpContextBase context, AsyncCallback callback, object state)
        {
            var tcs = new TaskCompletionSource<Action>(state);
            if (callback != null)
                tcs.Task.ContinueWith(task => callback(task), TaskContinuationOptions.ExecuteSynchronously);

            var request = context.Request;
            var response = context.Response;

            var pathBase = request.ApplicationPath;
            if (pathBase == "/" || pathBase == null)
                pathBase = "";

            if (_root != null)
                pathBase += _root;

            var path = request.Path;
            if (path.StartsWith(pathBase))
                path = path.Substring(pathBase.Length);

            var serverVarsToAddToEnv = request.ServerVariables.AllKeys
                .Where(key => !key.StartsWith("HTTP_") && !string.Equals(key, "ALL_HTTP") && !string.Equals(key, "ALL_RAW"))
                .Select(key => new KeyValuePair<string, object>(key, request.ServerVariables.Get(key)));

            var env = new Dictionary<string, object>();
            env[OwinConstants.Version] = "1.0";
            env[OwinConstants.RequestMethod] = request.HttpMethod;
            env[OwinConstants.RequestScheme] = request.Url.Scheme;
            env[OwinConstants.RequestPathBase] = pathBase;
            env[OwinConstants.RequestPath] = path;
            env[OwinConstants.RequestQueryString] = request.ServerVariables["QUERY_STRING"];
            env[OwinConstants.RequestProtocol] = request.ServerVariables["SERVER_PROTOCOL"];
            env["aspnet.HttpContextBase"] = context;
            env[OwinConstants.CallCompleted] = tcs.Task;

#if ASPNET_WEBSOCKETS
            if (context.IsWebSocketRequest)
                env[OwinConstants.WebSocketSupport] = "WebSocketFunc";
#endif

            foreach (var kv in serverVarsToAddToEnv)
                env["server." + kv.Key] = kv.Value;

            var requestHeaders = request.Headers.AllKeys
                    .ToDictionary(x => x, x => request.Headers.GetValues(x), StringComparer.OrdinalIgnoreCase);

            var requestStream = request.InputStream;

            try
            {
                _app.Invoke(env, requestHeaders, requestStream)
                    .ContinueWith(taskResultParameters =>
                    {
                        if (taskResultParameters.IsFaulted)
                        {
                            tcs.TrySetException(taskResultParameters.Exception.InnerExceptions);
                        }
                        else if (taskResultParameters.IsCanceled)
                        {
                            tcs.TrySetCanceled();
                        }
                        else
                        {
                            try
                            {
                                var resultParameters = taskResultParameters.Result;
                                var properties = resultParameters.Item1;
                                var responseStatus = resultParameters.Item2;
                                var responseHeader = resultParameters.Item3;
                                var responseCopyTo = resultParameters.Item4;

                                response.BufferOutput = false;
                                response.StatusCode = responseStatus;

                                object reasonPhrase;
                                if (properties.TryGetValue(OwinConstants.ReasonPhrase, out reasonPhrase))
                                    response.StatusDescription = Convert.ToString(reasonPhrase);

                                if (responseHeader != null)
                                {
                                    foreach (var header in responseHeader)
                                    {
                                        foreach (var headerValue in header.Value)
                                            response.AddHeader(header.Key, headerValue);
                                    }
                                }

#if ASPNET_WEBSOCKETS
                                object tempWsBodyDelegate;
                                if (responseStatus == 101 &&
                                    properties.TryGetValue(OwinConstants.WebSocketBodyDelegte, out tempWsBodyDelegate) &&
                                    tempWsBodyDelegate != null)
                                {
                                    var wsBodyDelegate = (WebSocketAction)tempWsBodyDelegate;
                                    context.AcceptWebSocketRequest(async websocketContext =>
                                    {
                                        env["aspnet.AspNetWebSocketContext"] = websocketContext;
                                        var webSocket = websocketContext.WebSocket;

                                        await wsBodyDelegate(WebSocketSendAsync(webSocket), WebSocketReceiveAsync(webSocket), WebSocketCloseAsync(webSocket));
//.........这里部分代码省略.........
开发者ID:prabirshrestha,项目名称:socket-io-net,代码行数:101,代码来源:SimpleOwinAspNetHost.cs


注:本文中的System.Web.HttpContextBase.AcceptWebSocketRequest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。