I am doing a POC where I need to establish one connection with SignalR and subscribe for notification. But while hitting the 1st negotiate api itself I am getting 404 error, but the thing is I am getting success in Postman for same url with some custom headers.
SignalRClient.java
import android.util.Log;
import com.microsoft.signalr.HubConnection;
import com.microsoft.signalr.HubConnectionBuilder;
import com.microsoft.signalr.HubConnectionState;
import com.microsoft.signalr.OnClosedCallback;
import com.microsoft.signalr.TransportEnum;
import java.util.HashMap;
import java.util.Map;
public class SignalRClient {
private static final String TAG = "xxxxxxx";
private static final String HUB_URL = "xxxxxxx";
private final HubConnection hubConnection;
public SignalRClient(String token, String username) {
Map<String, String> headers = new HashMap<>();
headers.put("token", token);
headers.put("userName", username);
// Build the connection
hubConnection = HubConnectionBuilder
.create(HUB_URL).withHeaders(headers)
.withTransport(TransportEnum.LONG_POLLING)
.build();
}
public void startConnection() {
if (hubConnection != null && hubConnection.getConnectionState() == HubConnectionState.DISCONNECTED) {
hubConnection.start()
.blockingAwait(); // You may handle the result asynchronously
Log.d(TAG, "Connection started");
}
}
public void stopConnection() {
if (hubConnection != null && hubConnection.getConnectionState() == HubConnectionState.CONNECTED) {
hubConnection.stop();
Log.d(TAG, "Connection stopped");
}
}
public void sendMessage(String message) {
if (hubConnection != null && hubConnection.getConnectionState() == HubConnectionState.CONNECTED) {
hubConnection.send("SendMessage", message);
Log.d(TAG, "Message sent: " + message);
} else {
Log.e(TAG, "Connection not established or closed. Unable to send message.");
}
}
}
I am just initializing SignalRClient from different class and start connection after that. Please guide me if I am done something wrong.