【发布时间】:2016-02-12 11:16:30
【问题描述】:
我正在尝试了解 Bayeux 协议。我还没有找到详细解释bayeux 客户端在技术上如何工作的网络资源。
来自this 资源,
Bayeux 协议要求新客户端发送的第一条消息 是握手消息(在 /meta/handshake 通道上发送的消息)。
客户端处理握手回复,如果成功, 开始——在幕后——与服务器的心跳机制,通过 交换连接消息(在 /meta/connect 上发送的消息 频道)。
这种心跳机制的细节取决于客户端 使用了传输,但可以看作是客户端发送连接 消息并期待一段时间后的回复。
连接消息继续在客户端和服务器之间流动,直到 任何一方决定通过发送断开消息(a 在 /meta/disconnect 通道上发送的消息)。
我用 Java 编写了首先进行握手,然后订阅特定频道的方法。我利用 Apache HttpClient 库来执行 HTTP POST 请求。
现在是连接部分。
我的理解是,我需要保持对bayeux服务器的请求开放,并且每当我收到响应时,就发出另一个请求。
我已经编写了以下代码。我的理解是否正确,这个bayeux客户端是否表现出正确的连接功能? (请忽略缺少的断开连接、取消订阅方法)
另外,我已经针对 Bayeux 服务器测试了代码,它可以正常工作。
/* clientId - Unique clientId returned by bayeux server during handshake
responseHandler - see interface below */
private static void connect(String clientId, ResponseHandler responseHandler)
throws ClientProtocolException, UnsupportedEncodingException, IOException {
String message = "[{\"channel\":\"/meta/connect\","
+ "\"clientId\":\"" + clientId + "\"}]";
CloseableHttpClient httpClient = HttpClients.createDefault();
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (!doDisconnect) {
try {
CloseableHttpResponse response = HttpPostHelper.postToURL(ConfigurationMock.urlRealTime,
message, httpClient, ConfigurationMock.getAuthorizationHeader());
responseHandler.handleResponse(response);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
httpClient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
t.start();
}
/*Simple interface to define what happens with the response when it arrives*/
private interface ResponseHandler {
void handleResponse(CloseableHttpResponse httpResponse);
}
public static void main(String[] args) throws Exception{
String globalClientId = doHandShake(); //assume this method exists
subscribe(globalClientId,"/measurements/10500"); //assume this method exists
connect(globalClientId, new ResponseHandler() {
@Override
public void handleResponse(CloseableHttpResponse httpResponse) {
try {
System.out.println(HttpPostHelper.toStringResponse(httpResponse));
} catch (ParseException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
【问题讨论】: