使用连接apache paho websocket client

时间:2017-05-31 14:44:50

标签: java websocket activemq paho broker

我有一个连接到代理的mqtt客户端。 我的经纪人拦截连接从中获取令牌并做一些工作。 我想在连接时发送一个令牌作为查询参数。

我的客户端连接如下:

        MqttAsyncClient sampleClient = new MqttAsyncClient(broker, clientId, persistence);
        MqttConnectOptions connOpts = new MqttConnectOptions();

        connOpts.setCleanSession(false);

        connOpts.setAutomaticReconnect(true);
        connOpts.setKeepAliveInterval(MqttConnectOptions.KEEP_ALIVE_INTERVAL_DEFAULT);

        connOpts.setConnectionTimeout(MqttConnectOptions.CONNECTION_TIMEOUT_DEFAULT); 
        IMqttToken token = sampleClient.connect(connOpts);

我该怎么做?有人能帮助我吗?

1 个答案:

答案 0 :(得分:0)

如果您修改班级WebSocketHandshake中的方法private void sendHandshakeRequest(String key) throws IOException{ pw.print("Upgrade: websocket" + LINE_SEPARATOR); pw.print("Connection: Upgrade" + LINE_SEPARATOR); pw.print("Sec-WebSocket-Key: " + key + LINE_SEPARATOR); pw.print("Sec-WebSocket-Protocol: mqttv3.1" + LINE_SEPARATOR); pw.print("Sec-WebSocket-Version: 13" + LINE_SEPARATOR); // TODO add the header with your token here ,则可以这样做:

private

由于该方法为internal且该类位于break;包中,因此您无其他选择,只能编译自己的Paho库自定义版本。