[英]Pusher: Decrease timeout for connection state
我目前正在使用适用于Java的Pusher库尝试使用websocket。
如果互联网连接断开,则Pusher会自动将其连接状态从CONNECTED更改为DISCONNECTED。 但是,这似乎仅在断开连接150秒后才发生。 这是非常不幸的,因为在那150年代,很多消息可能会丢失,事实上的旧消息仍然可以看作是最新消息。
我怎么知道最近收到的消息是否是最新的? 还是有什么办法可以减少连接状态的超时时间?
这是我正在使用的推送程序代码:
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();
}
}
}
}
}
刚找到答案:用PusherOptions-object发起Pusher-object。
这是PusherOptions类: http ://pusher.github.io/pusher-java-client/src-html/com/pusher/client/PusherOptions.html
这是一个简单的示例,我如何将连接超时从150秒减少到15秒:
// Define timeout parameters
PusherOptions opt = new PusherOptions();
opt.setActivityTimeout((long)10000L);
opt.setPongTimeout((long)5000L);
// Create a new Pusher instance
Pusher pusher = new Pusher(PUSHER_KEY, opt);
ActivityTimeout定义发送ping检查连接的频率,PongTimeout定义等待ping信号响应之前的等待时间。
最小ActivityTimeout为1000毫秒,但是Pusher强烈建议不要使用如此低的值,以免减少服务器流量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.