【问题标题】:Asp.Net Core SignalR Can Not Connect From Android Studio Emulator ClientAsp.Net Core SignalR 无法从 Android Studio 模拟器客户端连接
【发布时间】:2019-05-06 17:17:56
【问题描述】:

我有一个 SignalR 服务器在运行

http://localhost:50926/testhub

我可以使用 .net signalR 客户端连接到同一台机器上的集线器,它可以按预期工作。 我无法在 Android Studio 中运行的 Android 客户端上连接到它。我知道您无法从模拟器连接到本地主机,因此我设置了一个 ng rock 代理,代理似乎从邮递员连接到本地主机,但我无法从模拟器连接。 我知道 android 客户端只能使用 websockets,所以我已将集线器配置为使用这样的 web sockets:

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddSignalR();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseWebSockets();
        app.UseSignalR((configure) =>
        {
            var desiredTransports =
                HttpTransportType.WebSockets;

            configure.MapHub<TestHub>("/testhub", (options) =>
            {
                options.Transports = desiredTransports;
            });
        });
        app.Run(async (context) =>
           {
               await context.Response.WriteAsync("Hello World!");
           });
    }

这是我的 Android 客户端代码:

public class MainActivity extends AppCompatActivity {

HubConnection hubConnection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    hubConnection = HubConnectionBuilder.create("http://c03e07e9.ngrok.io/testhub").build();
    hubConnection.start();
    if (hubConnection.getConnectionState()== HubConnectionState.CONNECTED){
        hubConnection.send("ReceiveMessage","hello from android");
    }


}

} 还有 gradle:

 android {
compileSdkVersion 28
defaultConfig {
    applicationId "com.ct.sigrtest2"
    minSdkVersion 28
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
}

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.microsoft.signalr:signalr:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso- 
    core:3.0.2'

} android客户端不会抛出任何错误,也不会连接到ngrok 谁能告诉我为什么我无法连接到集线器?

【问题讨论】:

  • 您可以在 Android 设备上连接到“localhost”(开发电脑)。你需要把'localhost'改成开发电脑的IP地址,localhost:50926/testhub改成xxx.xxx.xxx.xxx:50926/testhub,即如果你的电脑有防火墙保护,你需要在防火墙上进行修改以允许50926 端口。

标签: asp.net-core android-emulator signalr-2


【解决方案1】:

Ngrok 似乎不支持 websockets :(

【讨论】:

    【解决方案2】:

    1) 在 AndroidManifext.xml 中检查您的权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    2) 尝试使用本地 ip 地址 10.0.2.2 而不是 localhost (如果你想要 localhost)

    hubConnection = HubConnectionBuilder.create("http://10.0.2.2:port/testhub").build();
    

    3) 使用 HubConnectionTask 类启动:

    class HubConnectionTask extends AsyncTask<HubConnection, Void, Void> {
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    
        @Override
        protected Void doInBackground(HubConnection... hubConnections) {
            HubConnection hubConnection = hubConnections[0];
            hubConnection.start().blockingAwait();
            return null;
        }
    }
    
    new HubConnectionTask().execute(hubConnection);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 2020-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多