(java)使用HtmlUnit监听websocket消息 [英] (java) Listen to websocket messages with HtmlUnit
问题描述
我正在尝试在特定页面上收听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屋!