请帮助我理解 Java 中的 Pusher websocket API

Please help me understand Pusher websocket API in Java

我想使用 official Java library 的 Pusher Websocket 连接。我获得了基本功能 运行,例如将其连接到数据服务和接收消息。但是,我很难实现其他功能,例如读取连接状态。也许你可以通过回答以下一些问题来帮助我:

1) 启动推送器并将其连接到服务时会发生什么 (pusher.connect())?已连接的推送器 运行 是否在单独的线程中?

2) 如何获取推送器连接的当前状态?

3) 如果我的推送器正在更新一个被其他服务并行使用的对象,这会导致问题吗?

4) 如何处理 errors/exceptions?到目前为止我还没有收到任何错误,错误看起来像正常异常还是处理方式不同?

谢谢!

下面我复制了我当前的代码。请注意,底部的 while 循环仅用于测试,因此代码保持 运行.

import com.pusher.client.Pusher;
import com.pusher.client.channel.Channel;
import com.pusher.client.channel.ChannelEventListener;
import com.pusher.client.channel.SubscriptionEventListener;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;


public class Testing {

    public static void main(String[] args) throws Exception {


        // Create a new Pusher instance
        Pusher pusher = new Pusher("PusherKey");

        pusher.connect(new ConnectionEventListener() {

            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {
                System.out.println("State changed to " + change.getCurrentState() +
                                   " from " + change.getPreviousState());
            }

            @Override
            public void onError(String message, String code, Exception e) {
                System.out.println("There was a problem connecting!");
            }
        }, ConnectionState.ALL);

        // Subscribe to a channel
         Channel channel = pusher.subscribe("channel", new ChannelEventListener() {
             @Override
             public void onSubscriptionSucceeded(String channelName) {
                 System.out.println("Subscribed!");
             }

             @Override
             public void onEvent(String channelName, String eventName, String data) {
                 System.out.println("desilo se");
             }
         });

         // Bind to listen for events called "my-event" sent to "my-channel"
         channel.bind("my-event", new SubscriptionEventListener() {
             @Override
             public void onEvent(String channel, String event, String data) {
                 System.out.println("Received event with data: " + data);
             }
         });

        while(true){
            try {
            Thread.sleep(1000);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
            }

        }
}

}

1) What happens when initiating a pusher and connecting it to a service(pusher.connect())? Is the connected pusher running in a separate thread?

可以在以下两个文件中看到线程管理和交互:

因此,与底层 WebSocket 连接的交互发生在指定队列(名为 eventQueue)中。

2) How can I get the current state of my pusher-connection?

ConnectionState state = pusher.getConnection.getState();

3) If my pusher is updating an object that is in parallel used by other services, can this cause problems?

如果您有一个多线程应用程序,您需要自己管理对共享对象的访问。

所有与库外代码(您的代码)的交互都发生在 eventQueue 线程上。

4) How can I handle errors/exceptions? So far I haven't received any, would an error look like a normal exception or is it handled differently?

在可能和适当的情况下,Pusher Java 客户端库将捕获异常并通过回调将它们提供给您的代码,例如ConnectionEventListener.onError.

在发生库未处理的异常的任何情况下,您都会知道,因为您的 IDE 会告诉您不处理异常 case/your 代码将无法编译,例如Pusher.connect @throws