发送查询参数与连接apache帕霍websocket客户端
问题描述:
我有一个mqtt客户端连接到一个经纪人。 我的经纪人拦截连接以从中获取令牌并执行一些操作。 我想连接时发送一个令牌作为查询参数。发送查询参数与连接apache帕霍websocket客户端
我的客户端连接是这样的:
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);
我怎样才能做到这一点?有人能帮助我吗?
答
如果您修改方法sendHandshakeRequest
在类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
包,你没有其他选择,但编译自己的定制Paho图书馆的版本。