编写我自己的简单 Bayeux 客户端
Writing my own trivial Bayeux client
我正在尝试了解 Bayeux 协议。我还没有找到详细解释 bayeux 客户端在技术上如何工作的网络资源。
来自 this 资源,
The Bayeux protocol requires that the first message a new client sends
be a handshake message (a message sent on /meta/handshake channel).
The client processes the handshake reply, and if it is successful,
starts – under the covers – a heartbeat mechanism with the server, by
exchanging connect messages (a message sent on a /meta/connect
channel).
The details of this heartbeat mechanism depend on the client
transport used, but can be seen as the client sending a connect
message and expecting a reply after some time.
Connect messages continue to flow between client and server until
either side decides to disconnect by sending a disconnect message (a
message sent on the /meta/disconnect channel).
我在 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();
}
}
});
}
您的代码不正确。
/meta/connect
频道上的消息没有 subscription
字段。
必须在 /meta/subscribe
频道发送订阅。
您想研究Bayeux Specification for further details, in particular the meta messages section and the event messages section。
一个建议是 launch the CometD Demo 并查看客户端交换的消息,并在您的实现中模仿这些消息。
我正在尝试了解 Bayeux 协议。我还没有找到详细解释 bayeux 客户端在技术上如何工作的网络资源。
来自 this 资源,
The Bayeux protocol requires that the first message a new client sends be a handshake message (a message sent on /meta/handshake channel).
The client processes the handshake reply, and if it is successful, starts – under the covers – a heartbeat mechanism with the server, by exchanging connect messages (a message sent on a /meta/connect channel).
The details of this heartbeat mechanism depend on the client transport used, but can be seen as the client sending a connect message and expecting a reply after some time.
Connect messages continue to flow between client and server until either side decides to disconnect by sending a disconnect message (a message sent on the /meta/disconnect channel).
我在 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();
}
}
});
}
您的代码不正确。
/meta/connect
频道上的消息没有 subscription
字段。
必须在 /meta/subscribe
频道发送订阅。
您想研究Bayeux Specification for further details, in particular the meta messages section and the event messages section。
一个建议是 launch the CometD Demo 并查看客户端交换的消息,并在您的实现中模仿这些消息。