本文整理汇总了C#中Connection.Stop方法的典型用法代码示例。如果您正苦于以下问题:C# Connection.Stop方法的具体用法?C# Connection.Stop怎么用?C# Connection.Stop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Connection
的用法示例。
在下文中一共展示了Connection.Stop方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConnectionCanStartWithAuthenicatedUserAndQueryString
public void ConnectionCanStartWithAuthenicatedUserAndQueryString()
{
using (var host = new MemoryHost())
{
host.Configure(app =>
{
Func<AppFunc, AppFunc> middleware = (next) =>
{
return env =>
{
if (((string)env["owin.RequestQueryString"]).IndexOf("access_token") == -1)
{
return next(env);
}
var user = new CustomPrincipal
{
Name = "Bob",
IsAuthenticated = true,
Roles = new[] { "User" }
};
env["server.User"] = user;
return next(env);
};
};
app.Use(middleware);
app.MapConnection<MyAuthenticatedConnection>("/authenticatedConnection", new ConnectionConfiguration());
});
var connection = new Connection("http://foo/authenticatedConnection", "access_token=1234");
connection.Start(host).Wait();
Assert.Equal(connection.State, ConnectionState.Connected);
connection.Stop();
}
}
示例2: PollingLoop
private void PollingLoop(Connection connection, string data, Action initializeCallback, Action<Exception> errorCallback, bool raiseReconnect = false)
{
string url = connection.Url;
var reconnectTokenSource = new CancellationTokenSource();
int reconnectFired = 0;
if (connection.MessageId == null)
{
url += "connect";
}
url += GetReceiveQueryString(connection, data);
HttpHelper.PostAsync(url, PrepareRequest(connection)).ContinueWith(task =>
{
// Clear the pending request
connection.Items.Remove(HttpRequestKey);
bool shouldRaiseReconnect = false;
bool disconnectedReceived = false;
try
{
if (!task.IsFaulted)
{
if (raiseReconnect)
{
// If the timeout for the reconnect hasn't fired as yet just fire the
// event here before any incoming messages are processed
FireReconnected(connection, reconnectTokenSource, ref reconnectFired);
}
// Get the response
var raw = task.Result.ReadAsString();
ProcessResponse(connection, raw, out shouldRaiseReconnect, out disconnectedReceived);
}
}
finally
{
if (disconnectedReceived)
{
connection.Stop();
}
else
{
bool requestAborted = false;
bool continuePolling = true;
if (task.IsFaulted)
{
// Cancel the previous reconnect event
reconnectTokenSource.Cancel();
// Raise the reconnect event if we successfully reconnect after failing
shouldRaiseReconnect = true;
// Get the underlying exception
Exception exception = task.Exception.GetBaseException();
// If the error callback isn't null then raise it and don't continue polling
if (errorCallback != null)
{
// Raise on error
connection.OnError(exception);
// Call the callback
errorCallback(exception);
// Don't continue polling if the error is on the first request
continuePolling = false;
}
else
{
// Figure out if the request was aborted
requestAborted = IsRequestAborted(exception);
// Sometimes a connection might have been closed by the server before we get to write anything
// so just try again and don't raise OnError.
if (!requestAborted && !(exception is IOException))
{
// Raise on error
connection.OnError(exception);
// If the connection is still active after raising the error event wait for 2 seconds
// before polling again so we aren't hammering the server
if (connection.IsActive)
{
Thread.Sleep(2000);
}
}
}
}
// Only continue if the connection is still active and wasn't aborted
if (continuePolling && !requestAborted && connection.IsActive)
{
PollingLoop(connection, data, null, null, shouldRaiseReconnect);
}
}
//.........这里部分代码省略.........