我正在构建一个Xamarin Android应用,它使用SignalR进行实时聊天。当我断开连接后再次连接到服务器时应用程序崩溃,当应用程序重新启动时,它连接正常。 我有一个单例类,用于在我的解决方案中使用相同的引用。单例类具有以下连接到服务器的方法:
public async Task<string> StartConnection()
{
hubConnection.Headers.Add("User-Agent", "mobile");
hubConnection.Headers.Add("username", loggedUser);
try
{
await hubConnection.Start();
return "Connected..";
}
catch (Exception e)
{
return e.Message;
}
}
以下方法断开连接:
public void StopConnection()
{
hubConnection.Stop();
}
连接在应用的MainActivity中启动,如下所示:
Task.Factory.StartNew(async () =>
{
await SignalRClientHelper.StartConnection();
SignalRClientHelper.InvokeGetPendingConversations(loggedonuser.UserName);
});
用户注销时断开连接,如下所示:
SignalRClientHelper.StopConnection();
客户端可以首次连接到signalR服务器而没有任何问题,但是当他们注销并再次登录应用程序时,应用程序在第二次尝试服务器时崩溃。 这是错误日志:
ava.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
... 2 more
Caused by: md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable: System.InvalidOperationException: Data cannot be sent because the connection is in the disconnected state. Call start before sending any data.
at Microsoft.AspNet.SignalR.Client.Connection.Send (System.String data) [0x0001e] in <filename unknown>:0
at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[TResult,TProgress] (System.String method, System.Action`1 onProgress, System.Object[] args) [0x000e1] in <filename unknown>:0
at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke[T] (System.String method, System.Object[] args) [0x00000] in <filename unknown>:0
at Microsoft.AspNet.SignalR.Client.Hubs.HubProxy.Invoke (System.String method, System.Object[] args) [0x00000] in <filename unknown>:0
at Yourtime.SignalRClientHelper+<InvokeSendMessage>d__11.MoveNext () [0x0000d] in <filename unknown>:0
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <filename unknown>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<ThrowAsync>m__0 (System.Object state) [0x00000] in <filename unknown>:0
at Android.App.SyncContext+<Post>c__AnonStorey0.<>m__0 () [0x00000] in <filename unknown>:0
at Java.Lang.Thread+RunnableImplementor.Run () [0x0000b] in <filename unknown>:0
at Java.Lang.IRunnableInvoker.n_Run (IntPtr jnienv, IntPtr native__this) [0x00009] in <filename unknown>:0
at (wrapper dynamic-method) System.Object:44f67f43-349f-4a1b-8234-1604bc812b68 (intptr,intptr)
at mono.java.lang.RunnableImplementor.n_run(Native Method)
at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4511)
有没有人知道这里可能出现什么问题?
答案 0 :(得分:1)
SignalR断开连接有点错误IMO,但是你的错误清楚地表明你在断开状态时试图调用某些东西。
我建议您更改连接/断开连接的方式如下:
<强>断开强>
var connectionToDispose = _connection; // _connection is the current connection
_connection = null;
_proxy = null;
// connection disposing can block the UI thread for about 20 seconds
// this doesn´t always happen but just in case we run it on a new thread
Task.Run(() =>
{
try
{
connectionToDispose.Dispose();
}
catch (Exception ex)
{
_tracer?.WriteLine($"[{_className}] Connection could not be disposed: {ex.Message}");
}
});
连接强>
if (_connection != null)
{
return false;
}
// always create a new Hub
_connection = new HubConnection(_endpointUri);
// TODO add headers
// TODO create proxy again and subscribe to server events (proxy.On...), etc
try
{
_tracer?.WriteLine($"[{_className}] CONNECTING...");
await _connection.Start();
return true;
}
catch (Exception ex)
{
_tracer?.WriteLine($"[{_className}] CONNECTION START ERROR: {ex.Message}");
return false;
}
仅供参考,请查看此class。我在一个连接/断开多次没有问题的应用程序中使用过。你可以从中得到一些想法。