Asp.Net 核心 SignalR 无法从 Android Studio 模拟器客户端连接

Asp.Net Core SignalR Can Not Connect From Android Studio Emulator Client

我在

上有一个 SignalR 服务器 运行
http://localhost:50926/testhub

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

 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 谁能告诉我为什么我无法连接到集线器?

Ngrok 好像不支持 websockets :(

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 开始:

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);