HubConnection not trigger registering methods in Android.
mucahitsidimi opened this issue · comments
When I try to use your sample code and start hubConnection in AsynTask send method works but on method does not work.
public class ChatActivity extends BaseActivity {
public HubConnection hubConnection;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
String bearerToken = localStorage.getString("KEY_AUTHORIZATION_TOKEN");
hubConnection = HubConnectionBuilder.create("https://....")
.withHeader("Authorization", bearerToken)
.build();
hubConnection.on("DeletedChat", chatModel -> {
EventBus.getDefault().post(new DeletedChatEvent(chatModel));
}, ChatModel.class);
hubConnection.on("NewChat", chatModel -> {
EventBus.getDefault().post(new NewChatEvent(chatModel));
}, ChatModel.class);
hubConnection.on("NewMessage", chatMessageModel -> {
EventBus.getDefault().post(new NewMessageEvent(chatMessageModel));
}, ChatMessageModel.class);
hubConnection.on("ReadedMessages", chatMessageModels -> {
EventBus.getDefault().post(new ReadedMessagesEvent(chatMessageModels));
}, ChatMessageModel[].class);
new HubConnectionTask().execute(hubConnection);
}
public void sendMessage(ChatModel chatModel){
hubConnection.send("SendMessage", chatModel);
}
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;
}
}
}
If I try to connect hubConnection in onCreate() without AsyncTask as below everything work fine but .blockingAwait() is blocking main thread and activity does not open for a while.
public class ChatActivity extends BaseActivity {
public HubConnection hubConnection;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
String bearerToken = localStorage.getString("KEY_AUTHORIZATION_TOKEN");
hubConnection = HubConnectionBuilder.create("https://...")
.withHeader("Authorization", bearerToken)
.build();
hubConnection.on("DeletedChat", chatModel -> {
EventBus.getDefault().post(new DeletedChatEvent(chatModel));
}, ChatModel.class);
hubConnection.on("NewChat", chatModel -> {
EventBus.getDefault().post(new NewChatEvent(chatModel));
}, ChatModel.class);
hubConnection.on("NewMessage", chatMessageModel -> {
EventBus.getDefault().post(new NewMessageEvent(chatMessageModel));
}, ChatMessageModel.class);
hubConnection.on("ReadedMessages", chatMessageModels -> {
EventBus.getDefault().post(new ReadedMessagesEvent(chatMessageModels));
}, ChatMessageModel[].class);
hubConnection.start().blockingAwait();
}
public void sendMessage(ChatModel chatModel){
hubConnection.send("SendMessage", chatModel);
}
}
If I trying to connection just with hubConnection.start(); , send methods work but on methods not working again.
public class ChatActivity extends BaseActivity {
public HubConnection hubConnection;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
String bearerToken = localStorage.getString("KEY_AUTHORIZATION_TOKEN");
hubConnection = HubConnectionBuilder.create("https://...")
.withHeader("Authorization", bearerToken)
.build();
hubConnection.on("DeletedChat", chatModel -> {
EventBus.getDefault().post(new DeletedChatEvent(chatModel));
}, ChatModel.class);
hubConnection.on("NewChat", chatModel -> {
EventBus.getDefault().post(new NewChatEvent(chatModel));
}, ChatModel.class);
hubConnection.on("NewMessage", chatMessageModel -> {
EventBus.getDefault().post(new NewMessageEvent(chatMessageModel));
}, ChatMessageModel.class);
hubConnection.on("ReadedMessages", chatMessageModels -> {
EventBus.getDefault().post(new ReadedMessagesEvent(chatMessageModels));
}, ChatMessageModel[].class);
hubConnection.start();
}
public void sendMessage(ChatModel chatModel){
hubConnection.send("SendMessage", chatModel);
}
}
How can we start activity and make the on methods work without locking the main thread?
What do you mean on
methods don't work? They are just registering handlers and will run when the client receives the corresponding message from the server.
Sorry I said method.
That's exactly what I mean. I am registering handlers and sending objects. As I explained in the above;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
...
hubConnection.on("NewMessage", chatMessageModel -> {
// expected : when new message comes from server, trigger this block
}, ChatMessageModel.class);
...
}
public void sendMessage(ChatModel chatModel){
// expected : when new message sending, send message with use this block
hubConnection.send("SendMessage", chatModel);
}
- When I try
new HubConnectionTask().execute(hubConnection);
orhubConnection.start();
it's not blocking main thread. Activity open normally.
// This method works. I can send message to server.
public void sendMessage(ChatModel chatModel){
hubConnection.send("SendMessage", chatModel);
}
hubConnection.on("NewMessage", chatMessageModel -> {
// This block doesn't get trigger. I am sending message from server but callback not work.
}, ChatMessageModel.class);
- When I try
hubConnection.start().blockingAwait();
. It's blocking main thread and activity does not open for a while. But everything works.
I want to learn how we can sending - receiving objects successfully without blocking main thread.
Ok, trying to gather all the info and verify I understand it correctly. Could you take a look at this list and verify that it's correct and that I'm not missing a scenario?
- Calling
hubConnection.start().blockingAwait();
fromonCreate
,.on(...)
handlers work? - Calling
hubConnection.start();
fromonCreate
,.on(...)
handlers don't work? - Calling
hubConnection.start();
orhubConnection.start().blockingAwait()
fromAsyncTask
,.on(...)
handlers don't work?
Also, can you enable logging so we can see whats happening in the client?
https://docs.microsoft.com/en-us/aspnet/core/signalr/java-client?view=aspnetcore-3.0#add-logging
Ok, trying to gather all the info and verify I understand it correctly. Could you take a look at this list and verify that it's correct and that I'm not missing a scenario?
- Calling
hubConnection.start().blockingAwait();
fromonCreate
,.on(...)
handlers work?- Calling
hubConnection.start();
fromonCreate
,.on(...)
handlers don't work?- Calling
hubConnection.start();
orhubConnection.start().blockingAwait()
fromAsyncTask
,.on(...)
handlers don't work?
- Yes,
.on(...)
handlers work but activity opening late. (blocking main thread) - Yes,
.on(...)
handlers don't work, activity opening normally. - Yes,
.on(...)
handlers don't work, activity opening normally.
Also, can you enable logging so we can see whats happening in the client?
https://docs.microsoft.com/en-us/aspnet/core/signalr/java-client?view=aspnetcore-3.0#add-logging
Did you get a chance to try this out?
I am so sorry. It was my fault. I have tried to use send objects before connection done. My handlers work only when I joined to chats room with
hubConnection.send("JoinChats", chatModelList);
I update my code and JoinChats after connection done. All cases work now.
Thank you again.