gpt4 book ai didi

java - onStateChange 在 Atmosphere 框架中被多次调用

转载 作者:行者123 更新时间:2023-11-29 03:36:00 26 4
gpt4 key购买 nike

我在我的应用程序中使用了 atmosphere 框架。

https://github.com/Atmosphere/atmosphere

我扩展了 AbstractReflectorAtmosphereHandler 类并实现了

-onRequest
-destroy
-onstatechanged

方法。

当客户端想要向服务器发送消息时:

subSocket.push(jQuery.stringifyJSON({ data: "blahblah", source:"client" }));

onRequest函数被调用;然而消息

Object message = atmosphereResource.getAtmosphereResourceEvent().getMessage();

为空。

比我尝试使用每次调用的 onstatechanged

(1) The remote connection gets closed, either by a browser or a proxy
(2) The remote connection reach its maximum idle time (AtmosphereResource.suspend))
(3) Everytime a broadcast operation is executed (broadcaster.broadcast)

然而即使在过滤掉 1 和 2 之后

 public void onStateChange(AtmosphereResourceEvent event)
throws IOException {
if (source.equals("client") && !event.isResumedOnTimeout() && !event.isResuming()){
System.out.println("message form client");
System.out.println(message.toString());
} else {
//normal onstatechanged code from AbstractReflectorAtmosphereHandler
}

但是消息会随机打印 2 到 4 次。它应该只被调用一次。

所以我的问题是:我能否在 onRequest 方法中访问消息,或者为什么 onStateChange 被调用了这么多次。

编辑:根据 jF 给出的答案,我已经能够访问 onRequest 函数内的消息。 (不过我不确定这是否是他真正的意思)。

public void onRequest(AtmosphereResource resource) throws IOException {
//Object message = resource.getAtmosphereResourceEvent().getMessage(); //is empty why?

//leave connection open
resource.suspend();

BufferedReader reader = resource.getRequest().getReader();
Object message = reader.readLine();

if (message !=null){
System.out.println("**onRequest: "+message.toString());
}
}

最佳答案

您需要在您的 onStateChange 中读取请求的主体:

atmosphereResource.getRequest().getReader (or getInputStream). 

关于java - onStateChange 在 Atmosphere 框架中被多次调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15524521/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com