I have a SignalR Server running on
http://localhost:50926/testhub
I can connect to the hub on the same machine with a .net signalR client and it works as expected. I can not connect to it on an Android Client running in Android Studio. I understand that you can not connect to localhost from an emulator so I have set up an ng rock proxy and the proxy appears to be connecting to localhost from postman but I can not connect from Emulator. I understand that the android client can only use websockets so I have configured the hub to use web sockets like this:
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!");
});
}
Here is my Android Client Code:
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");
}
}
} And the 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'
} The android client does not throw any errors and will not connect to ngrok Can anyone advise why I can not connect to the hub?
Ngrok it seems does not support websockets :(