How to fix Java client signalR connection fail?

Jay Lee 0 Reputation points
2023-07-12T15:30:19.4933333+00:00

[dependency]

implementation 'com.microsoft.signalr:signalr:7.0.0'

[error]

Exception in thread "main" com.microsoft.signalr.HttpRequestException: Unexpected status code returned from negotiate: 404 .

public static void main(String[] args) {
        HubConnection hubConnection1;
        String endpoint = "https://xxxxx.service.signalr.net";
        String accessToken = "wwyx1G3HnlIjPduQLIR+pq1f1c0r5......."; // Optional: If you're using access token authentication

        hubConnection1 = HubConnectionBuilder
                .create(endpoint)
                .withAccessTokenProvider(Single.just(accessToken))
                .build();

        //hubConnection1.on("Send", (message) -> {
            //System.out.println("New Message: " + message);
        //}, String.class);
        hubConnection1.start().blockingAwait();
        //hubConnection1.start();

        hubConnection1.send("Send", "test1234");
        hubConnection1.stop();
    }
Azure SignalR Service
Azure SignalR Service
An Azure service that is used for adding real-time communications to web applications.
122 questions
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 15,351 Reputation points Microsoft Employee
    2023-07-13T03:22:54.9466667+00:00

    @Jay Lee Based on the error message you provided, it seems that the SignalR Java client library is unable to negotiate a connection with the SignalR hub. This could be due to a few different reasons, such as:

    Incorrect endpoint URL: Please ensure that the endpoint URL you are using is correct and matches the URL of the SignalR hub you are trying to connect to. You can verify the URL by checking the SignalR hub configuration in the Azure portal or by contacting the SignalR hub administrator.

    Authentication issues: If your SignalR hub is configured to use access token authentication, please ensure that you are providing a valid access token in the accessToken variable. You can check the SignalR hub configuration to see if access token authentication is enabled and verify that the access token you are using is valid.

    • Network connectivity issues: Please ensure that your Java client is able to connect to the internet and that there are no network connectivity issues that could be preventing the client from connecting to the SignalR hub. You can try pinging the SignalR hub URL from your Java client to verify that the client is able to reach the hub.

    To troubleshoot this issue further, you can try enabling SignalR logging in your Java client and checking the logs for any error messages or warnings that could provide more information about the issue. You can enable logging by adding the following line of code before creating the HubConnection object:

    LoggerFactory.getLogger("com.microsoft.signalr").setLevel(Level.ALL);
    

    This will enable logging for the SignalR Java client library and output the logs to the console. You can then check the logs for any error messages or warnings that could provide more information about the issue.

    0 comments No comments