2
votes

I am new to signalR, tried to connect to a hub and got a connectionId after successful connection.

my code

Platform.loadPlatformComponent(new AndroidPlatformComponent());
    String host = "url";
    HubConnection connection = new HubConnection(host);
    HubProxy hub = connection.createHubProxy("pttdashboardhub");
    ClientTransport clientTransport = new ServerSentEventsTransport(connection.getLogger());
    SignalRFuture<Void> signalRFuture = connection.start(clientTransport);
    try {
        signalRFuture.get();
        System.out.println(connection.getConnectionId());

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    hub.on("onEvent",
            new SubscriptionHandler1<String>() {
                @Override
                public void run(String s) {
                    System.out.println("================ "+s);
                }
    }, String.class);

server code

var eventHub = GlobalHost.ConnectionManager.GetHubContext<EventHub>();
          var result = eventHub.Clients.Group("pttdashboard").onEvent(data);

the problem comes when hub.on function executes i get nothing from the server. Any help is appreciated.

1

1 Answers

0
votes

You can use subscribe function instead of on function.

   hub.subscribe("onEvent").addReceivedHandler(new Action<JsonElement[]>(){
       @Override
       public void run(JsonElement obj) {
          System.out.println("================ "+s);
       }

    });

I hope this helps you.