[英]Asp.Net Core SignalR Can Not Connect From Android Studio Emulator Client
I have a SignalR Server running on我有一个正在运行的 SignalR 服务器
http://localhost:50926/testhub
I can connect to the hub on the same machine with a .net signalR client and it works as expected.我可以使用 .net signalR 客户端连接到同一台机器上的集线器,它按预期工作。 I can not connect to it on an Android Client running in Android Studio.我无法在 Android Studio 中运行的 Android 客户端上连接到它。 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.我知道您无法从模拟器连接到本地主机,所以我设置了一个 ng Rock 代理,代理似乎是从邮递员连接到本地主机,但我无法从模拟器连接。 I understand that the android client can only use websockets so I have configured the hub to use web sockets like this:我知道 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!");
});
}
Here is my Android Client Code:这是我的 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");
}
}
} And the gradle:和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? android 客户端不会抛出任何错误,也不会连接到 ngrok 谁能告诉我为什么我无法连接到集线器?
Ngrok 似乎不支持 websockets :(
1) Check your permissions in AndroidManifext.xml 1) 检查您在 AndroidManifext.xml 中的权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
2) Try to use local ip address 10.0.2.2 instead localhost (if you want localhost) 2) 尝试使用本地 ip 地址 10.0.2.2 而不是 localhost (如果你想要 localhost)
hubConnection = HubConnectionBuilder.create("http://10.0.2.2:port/testhub").build();
3) Use HubConnectionTask class for starting: 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.