WebSocketRoute
Whenever a WebSocket
route is set up with Page.routeWebSocket() or BrowserContext.routeWebSocket(), the WebSocketRoute
object allows to handle the WebSocket, like an actual server would do.
Mocking
By default, the routed WebSocket will not connect to the server. This way, you can mock entire communcation over the WebSocket. Here is an example that responds to a "request"
with a "response"
.
page.routeWebSocket("wss://example.com/ws", ws -> {
ws.onMessage(message -> {
if ("request".equals(message))
ws.send("response");
});
});
Since we do not call WebSocketRoute.connectToServer() inside the WebSocket route handler, Playwright assumes that WebSocket will be mocked, and opens the WebSocket inside the page automatically.
Here is another example that handles JSON messages:
page.routeWebSocket("wss://example.com/ws", ws -> {
ws.onMessage(message -> {
JsonObject json = new JsonParser().parse(message).getAsJsonObject();
if ("question".equals(json.get("request").getAsString())) {
Map<String, String> result = new HashMap();
result.put("response", "answer");
ws.send(gson.toJson(result));
}
});
});
Intercepting
Alternatively, you may want to connect to the actual server, but intercept messages in-between and modify or block them. Calling WebSocketRoute.connectToServer() returns a server-side WebSocketRoute
instance that you can send messages to, or handle incoming messages.
Below is an example that modifies some messages sent by the page to the server. Messages sent from the server to the page are left intact, relying on the default forwarding.
page.routeWebSocket("/ws", ws -> {
WebSocketRoute server = ws.connectToServer();
ws.onMessage(message -> {
if ("request".equals(message))
server.send("request2");
else
server.send(message);
});
});
After connecting to the server, all messages are forwarded between the page and the server by default.
However, if you call WebSocketRoute.onMessage() on the original route, messages from the page to the server will not be forwarded anymore, but should instead be handled by the handler.
Similarly, calling WebSocketRoute.onMessage() on the server-side WebSocket will stop forwarding messages from the server to the page, and handler should take care of them.
The following example blocks some messages in both directions. Since it calls WebSocketRoute.onMessage() in both directions, there is no automatic forwarding at all.
page.routeWebSocket("/ws", ws -> {
WebSocketRoute server = ws.connectToServer();
ws.onMessage(message -> {
if (!"blocked-from-the-page".equals(message))
server.send(message);
});
server.onMessage(message -> {
if (!"blocked-from-the-server".equals(message))
ws.send(message);
});
});
Methods
close
Added in: v1.48Closes one side of the WebSocket connection.
Usage
WebSocketRoute.close();
WebSocketRoute.close(options);
Arguments
options
WebSocketRoute.CloseOptions
(optional)-
Optional close code.
-
Optional close reason.
-
Returns
connectToServer
Added in: v1.48By default, routed WebSocket does not connect to the server, so you can mock entire WebSocket communication. This method connects to the actual WebSocket server, and returns the server-side WebSocketRoute instance, giving the ability to send and receive messages from the server.
Once connected to the server:
- Messages received from the server will be automatically forwarded to the WebSocket in the page, unless WebSocketRoute.onMessage() is called on the server-side
WebSocketRoute
. - Messages sent by the
WebSocket.send()
call in the page will be automatically forwarded to the server, unless WebSocketRoute.onMessage() is called on the originalWebSocketRoute
.
See examples at the top for more details.
Usage
WebSocketRoute.connectToServer();
Returns
onClose
Added in: v1.48Allows to handle WebSocket.close
.
By default, closing one side of the connection, either in the page or on the server, will close the other side. However, when WebSocketRoute.onClose() handler is set up, the default forwarding of closure is disabled, and handler should take care of it.
Usage
WebSocketRoute.onClose(handler);
Arguments
-
Function that will handle WebSocket closure. Received an optional close code and an optional close reason.
onMessage
Added in: v1.48This method allows to handle messages that are sent by the WebSocket, either from the page or from the server.
When called on the original WebSocket route, this method handles messages sent from the page. You can handle this messages by responding to them with WebSocketRoute.send(), forwarding them to the server-side connection returned by WebSocketRoute.connectToServer() or do something else.
Once this method is called, messages are not automatically forwarded to the server or to the page - you should do that manually by calling WebSocketRoute.send(). See examples at the top for more details.
Calling this method again will override the handler with a new one.
Usage
WebSocketRoute.onMessage(handler);
Arguments
-
handler
Consumer<WebSocketFrame>#Function that will handle messages.
send
Added in: v1.48Sends a message to the WebSocket. When called on the original WebSocket, sends the message to the page. When called on the result of WebSocketRoute.connectToServer(), sends the message to the server. See examples at the top for more details.
Usage
WebSocketRoute.send(message);
Arguments
url
Added in: v1.48URL of the WebSocket created in the page.
Usage
WebSocketRoute.url();
Returns