我是signalR的新手,尝试连接到集线器并在成功连接后得到了connectionId。

我的密码

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);


服务器代码

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


问题发生在hub.on函数执行时,我从服务器什么都没得到。
任何帮助表示赞赏。

最佳答案

您可以使用订阅功能代替on函数。

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

    });


我希望这可以帮助你。

关于java - 没有从SignalR集线器接收任何数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56022262/

10-11 22:27
查看更多