簡體   English   中英

如何從Xamarin客戶端連接到SignalR服務器?

[英]How to connect to SignalR server, from Xamarin client?

(**)我創建了一個托管SignalR集線器的ASP.NET Web服務器。 我遵循了教程。 有用。 然后,我創建了一個Xamarin.Android客戶端(*)。 問題是我無法連接到服務器。 我認為,由於主機地址中存在“ localhost”,因此無法連接。 例外很大。

如果我的懷疑是正確的,我該如何解決? 如何使localhost看起來像普通服務器?

(*)-我也嘗試過Xamarin.Forms。

(**)-請查看問題底部的“編輯”。

這是我的方法:

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.MyButton);

        button.Click += async delegate 
        {
            button.Text = string.Format("{0} clicks!", count++);
            var hubConnection = new HubConnection("http://localhost:64642");
            var chatHubProxy = hubConnection.CreateHubProxy("ChatHub");
            try
            {
                // Start the connection
                await hubConnection.Start();

                // Invoke the 'UpdateNick' method on the server
                await chatHubProxy.Invoke("Send", new object[] { "TheMessage", "JohnDoe" });
            }
            catch(Exception ex)
            {

            }
        };
    }

{System.Net.WebException:錯誤:ConnectFailure(拒絕連接)---> System.Net.Sockets.SocketException:在System.Net.Sockets.Socket.Connect(System.Net.EndPoint remoteEP)[0x000cb]處拒絕連接System.Net.WebConnection.Connect上的/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1313(System.Net.HttpWebRequest請求) /Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/WebConnection.cs:195中的[0x0019b]-內部異常堆棧跟蹤的結尾-在系統上系統中/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/System/System.Net/HttpWebRequest.cs:1023中的.Net.HttpWebRequest.EndGetResponse(System.IAsyncResult asyncResult)[0x0005e] .Threading.Tasks.TaskFactory 1[TResult].FromAsyncCoreLogic (System.IAsyncResult iar, System.Func 2 [T,TResult] endFunction,System.Action 1[T] endAction, System.Threading.Tasks.Task 1 [TResult]承諾,System.Boolean requireSynchronization)[0x00014]在/ 用戶/助洗劑/數據/通道/ 3511 / 77cb8568 /源/單聲道/ MCS /類/ referencesource / mscorlib程序/系統/線程/任務/ FutureFactory.cs:550

---從之前引發異常的位置開始的堆棧跟蹤---位於/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:143在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任務)[0x00047]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187,位於System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任務)[ 0x0002e]在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156中位於System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd( System.Threading.Tasks.Task任務)[0x0000b]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / clas中 s / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1 + ConfiguredTaskAwaiter [TResult] .GetResult()在/ Users / builder / data / lanes / 3511中[0x00000] /77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535,位於/ Users / builder / data中的System.Net.Http.HttpClientHandler + c__async0.MoveNext()[0x003ce] /lanes/3511/77cb8568/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClientHandler.cs:379

---從之前引發異常的位置開始的堆棧跟蹤---位於/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:143在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任務)[0x00047]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187,位於System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任務)[ 0x0002e]在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156中位於System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd( System.Threading.Tasks.Task任務)[0x0000b]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / clas中 s / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128 at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1 + ConfiguredTaskAwaiter [TResult] .GetResult()在/ Users / builder / data / lanes / 3511中[0x00000] /77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:535,位於/ Users / builder / data中的System.Net.Http.HttpClient + c__async0.MoveNext()[0x000a9] /lanes/3511/77cb8568/source/mono/mcs/class/System.Net.Http/System.Net.Http/HttpClient.cs:276

---從之前引發異常的位置開始的堆棧跟蹤---位於/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中的System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()[0x0000c] mcs / class / referencesource / mscorlib / system / runtime / exceptionservices / exceptionservicescommon.cs:143在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task任務)[0x00047]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:187,位於System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task任務)[ 0x0002e]在/Users/builder/data/lanes/3511/77cb8568/source/mono/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/TaskAwaiter.cs:156中位於System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd( System.Threading.Tasks.Task任務)[0x0000b]在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono / mcs / clas中 s / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:128在/ Users / builder / data / lanes / 3511 / 77cb8568 / source / mono /中System.Runtime.CompilerServices.TaskAwaiter.GetResult()[0x00000] mcs / class / referencesource / mscorlib / system / runtime / compilerservices / TaskAwaiter.cs:113 at AndroidApp.MainActivity + <> c__DisplayClass1_0 + <b__0> d.MoveNext()[0x000a5]在C:\\ Data \\ Projects \\ SinglaRSamples \\ AndroidApp \\ AndroidApp \\ MainActivity.cs:36}

編輯:

正如梅森Jason)傑森(Jason)建議的那樣,我可能必須使用正確的IP而不是“ localhost”。 Android仿真器看到實際localhost的IP。 我正在努力尋找該IP,它位於Android設置中。 任何幫助將是巨大的!

您可以使用Google SDK仿真器中的http://10.0.2.2:64642訪問PC主機上運行的ASP.NET站點。 如果無法訪問服務器,則可能需要配置Windows防火牆以允許傳入的HTTP / HTTPS連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM