本文整理汇总了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));
//.........这里部分代码省略.........