简体   繁体   English

编写我自己的琐碎的Bayeux客户

[英]Writing my own trivial Bayeux client

I am trying to understand the Bayeux protocol. 我正在尝试了解Bayeux协议。 I haven't found a web-resource explaining how the bayeux client will technically work, in detail. 我还没有找到详细解释Bayeux客户端在技术上如何工作的网络资源。

From this resource, 通过资源,

The Bayeux protocol requires that the first message a new client sends be a handshake message (a message sent on /meta/handshake channel). 贝叶协议要求新客户端发送的第一条消息是握手消息(在/ meta / handshake通道上发送的消息)。

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). 客户端处理握手回复,如果成功,则通过交换连接消息(在/ meta / connect通道上发送的消息),在幕后启动与服务器的心跳机制。

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). 连接消息继续在客户端和服务器之间流动,直到双方通过发送断开消息(在/ meta / disconnect通道上发送的消息)决定断开连接为止。

I have written in Java methods to first do a handshake, then subscribe to a particular channel. 我用Java方法编写的代码首先要进行握手,然后订阅特定的频道。 I made use of the Apache HttpClient library to do the HTTP POST requests. 我利用Apache HttpClient库执行HTTP POST请求。

Now comes the part of connect. 现在是连接的一部分。

My understanding is that, I need to keep a request open to the bayeux server and whenever I receive a response, make another request. 我的理解是,我需要保持对Bayeux服务器的打开请求,并且每当收到响应时,都发出另一个请求。

I have the written the below code. 我写了下面的代码。 Is my understanding correct and does this bayeux client exhibit the correct connect functionality? 我的理解是否正确,并且该Bayeux客户端是否显示正确的连接功能? (please ignore the missing disconnect, unsubscribe methods) (请忽略丢失的断开连接,退订方法)

Also, I have tested the code against a bayeux server and it works correctly. 另外,我已经针对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();
            }

        }
    });
}

Your code is not correct. 您的代码不正确。

Messages on the /meta/connect channel do not have the subscription field. /meta/connect通道上的消息没有subscription字段。

Subscriptions must be sent on the /meta/subscribe channel. 订阅必须在/meta/subscribe频道上发送。

You want to study the Bayeux Specification for further details, in particular the meta messages section and the event messages section . 您想研究Bayeux规范以获取更多详细信息,尤其是元消息部分事件消息部分

A suggestion is to launch the CometD Demo and look at the messages exchanged by the client, and mimic those in your implementation. 建议启动CometD演示,并查看客户端交换的消息,并模仿您的实现中的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM