(java)使用HtmlUnit监听websocket消息 [英] (java) Listen to websocket messages with HtmlUnit

查看:520
本文介绍了(java)使用HtmlUnit监听websocket消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在特定页面上收听websocket消息.

I am trying to listen to websocket messages on a particular page.

似乎HtmlUnit支持WebSockets,但是我找不到任何文档/教程,甚至找不到答案:"是否可以用htmlUnit 截取websocket数据"?

It seems like HtmlUnit has support for WebSockets, but I can't find any documentation/tutorial or even an answer to: "is it possible to intercept websocket data with htmlUnit"?

我已经尝试过的:

try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) {

    webClient.getOptions().setJavaScriptEnabled(true);
    webClient.getOptions().setCssEnabled(false);
    webClient.setAjaxController(new NicelyResynchronizingAjaxController());

    final HtmlPage page = webClient.getPage("url");
    webClient.waitForBackgroundJavaScript(10 * 60 * 1000);
}

我收到有关websocket连接和此错误的INFO消息:

I get an INFO message about the websocket connection and this error:

INFO: Opening transport: xhr-streaming  url:https://stream71.forexpros.com:443/echo/340/3exyl8ko  RTO:357
Mar 23, 2017 5:40:07 PM com.gargoylesoftware.htmlunit.HttpWebConnection downloadContent
WARNING: Connection was closed while reading from stream.

推荐答案

到目前为止,此

As of now, WebSocket is supported only from the the JavaScript side by this class, and there is no API from HtmlUnit to control it.

当前实现是基本的,我将看看是否有一种方法可以拦截来自

The current implementation is basic, I will see if there is a way to intercept the traffic from Jetty Websocket client, it can be added to the public API.

更新:

WebClient现在具有getInternals().addListener().调用listener.created(WebSocket)后,应使用WebSocketListener调用webSocket.setWebSocketListener().

WebClient now has getInternals().addListener(). Once the listener.created(WebSocket) is called, you should invoke webSocket.setWebSocketListener() with the WebSocketListener.

测试用例为此功能包含在最新版本中构建.

这篇关于(java)使用HtmlUnit监听websocket消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆