mirror of
https://github.com/microsoft/playwright.git
synced 2024-11-28 01:15:10 +03:00
docs: use WebSocketFrame abstraction for Java & .NET (#33211)
This commit is contained in:
parent
aebceb345e
commit
0351fd9401
@ -3691,8 +3691,8 @@ await page.routeWebSocket('/ws', ws => {
|
||||
|
||||
```java
|
||||
page.routeWebSocket("/ws", ws -> {
|
||||
ws.onMessage(message -> {
|
||||
if ("request".equals(message))
|
||||
ws.onMessage(frame -> {
|
||||
if ("request".equals(frame.text()))
|
||||
ws.send("response");
|
||||
});
|
||||
});
|
||||
@ -3722,8 +3722,8 @@ page.route_web_socket("/ws", handler)
|
||||
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("/ws", ws => {
|
||||
ws.OnMessage(message => {
|
||||
if (message == "request")
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text == "request")
|
||||
ws.Send("response");
|
||||
});
|
||||
});
|
||||
|
@ -18,8 +18,8 @@ await page.routeWebSocket('wss://example.com/ws', ws => {
|
||||
|
||||
```java
|
||||
page.routeWebSocket("wss://example.com/ws", ws -> {
|
||||
ws.onMessage(message -> {
|
||||
if ("request".equals(message))
|
||||
ws.onMessage(frame -> {
|
||||
if ("request".equals(frame.text()))
|
||||
ws.send("response");
|
||||
});
|
||||
});
|
||||
@ -47,8 +47,8 @@ page.route_web_socket("wss://example.com/ws", lambda ws: ws.on_message(
|
||||
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
|
||||
ws.OnMessage(message => {
|
||||
if (message == "request")
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text == "request")
|
||||
ws.Send("response");
|
||||
});
|
||||
});
|
||||
@ -70,8 +70,8 @@ await page.routeWebSocket('wss://example.com/ws', ws => {
|
||||
|
||||
```java
|
||||
page.routeWebSocket("wss://example.com/ws", ws -> {
|
||||
ws.onMessage(message -> {
|
||||
JsonObject json = new JsonParser().parse(message).getAsJsonObject();
|
||||
ws.onMessage(frame -> {
|
||||
JsonObject json = new JsonParser().parse(frame.text()).getAsJsonObject();
|
||||
if ("question".equals(json.get("request").getAsString())) {
|
||||
Map<String, String> result = new HashMap();
|
||||
result.put("response", "answer");
|
||||
@ -105,8 +105,8 @@ page.route_web_socket("wss://example.com/ws", lambda ws: ws.on_message(
|
||||
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
|
||||
ws.OnMessage(message => {
|
||||
using var jsonDoc = JsonDocument.Parse(message);
|
||||
ws.OnMessage(frame => {
|
||||
using var jsonDoc = JsonDocument.Parse(frame.Text);
|
||||
JsonElement root = jsonDoc.RootElement;
|
||||
if (root.TryGetProperty("request", out JsonElement requestElement) && requestElement.GetString() == "question")
|
||||
{
|
||||
@ -140,11 +140,11 @@ await page.routeWebSocket('/ws', ws => {
|
||||
```java
|
||||
page.routeWebSocket("/ws", ws -> {
|
||||
WebSocketRoute server = ws.connectToServer();
|
||||
ws.onMessage(message -> {
|
||||
if ("request".equals(message))
|
||||
ws.onMessage(frame -> {
|
||||
if ("request".equals(frame.text()))
|
||||
server.send("request2");
|
||||
else
|
||||
server.send(message);
|
||||
server.send(frame.text());
|
||||
});
|
||||
});
|
||||
```
|
||||
@ -180,11 +180,11 @@ page.route_web_socket("/ws", handler)
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("/ws", ws => {
|
||||
var server = ws.ConnectToServer();
|
||||
ws.OnMessage(message => {
|
||||
if (message == "request")
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text == "request")
|
||||
server.Send("request2");
|
||||
else
|
||||
server.Send(message);
|
||||
server.Send(frame.Text);
|
||||
});
|
||||
});
|
||||
```
|
||||
@ -215,13 +215,13 @@ await page.routeWebSocket('/ws', ws => {
|
||||
```java
|
||||
page.routeWebSocket("/ws", ws -> {
|
||||
WebSocketRoute server = ws.connectToServer();
|
||||
ws.onMessage(message -> {
|
||||
if (!"blocked-from-the-page".equals(message))
|
||||
server.send(message);
|
||||
ws.onMessage(frame -> {
|
||||
if (!"blocked-from-the-page".equals(frame.text()))
|
||||
server.send(frame.text());
|
||||
});
|
||||
server.onMessage(message -> {
|
||||
if (!"blocked-from-the-server".equals(message))
|
||||
ws.send(message);
|
||||
server.onMessage(frame -> {
|
||||
if (!"blocked-from-the-server".equals(frame.text()))
|
||||
ws.send(frame.text());
|
||||
});
|
||||
});
|
||||
```
|
||||
@ -263,13 +263,13 @@ page.route_web_socket("/ws", handler)
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("/ws", ws => {
|
||||
var server = ws.ConnectToServer();
|
||||
ws.OnMessage(message => {
|
||||
if (message != "blocked-from-the-page")
|
||||
server.Send(message);
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text != "blocked-from-the-page")
|
||||
server.Send(frame.Text);
|
||||
});
|
||||
server.OnMessage(message => {
|
||||
if (message != "blocked-from-the-server")
|
||||
ws.Send(message);
|
||||
server.OnMessage(frame => {
|
||||
if (frame.Text != "blocked-from-the-server")
|
||||
ws.Send(frame.Text);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
@ -451,8 +451,8 @@ await page.routeWebSocket('wss://example.com/ws', ws => {
|
||||
|
||||
```java
|
||||
page.routeWebSocket("wss://example.com/ws", ws -> {
|
||||
ws.onMessage(message -> {
|
||||
if ("request".equals(message))
|
||||
ws.onMessage(frame -> {
|
||||
if ("request".equals(frame.text()))
|
||||
ws.send("response");
|
||||
});
|
||||
});
|
||||
@ -480,8 +480,8 @@ page.route_web_socket("wss://example.com/ws", lambda ws: ws.on_message(
|
||||
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
|
||||
ws.OnMessage(message => {
|
||||
if (message == "request")
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text == "request")
|
||||
ws.Send("response");
|
||||
});
|
||||
});
|
||||
@ -504,11 +504,11 @@ await page.routeWebSocket('wss://example.com/ws', ws => {
|
||||
```java
|
||||
page.routeWebSocket("wss://example.com/ws", ws -> {
|
||||
WebSocketRoute server = ws.connectToServer();
|
||||
ws.onMessage(message -> {
|
||||
if ("request".equals(message))
|
||||
ws.onMessage(frame -> {
|
||||
if ("request".equals(frame.text()))
|
||||
server.send("request2");
|
||||
else
|
||||
server.send(message);
|
||||
server.send(frame.text());
|
||||
});
|
||||
});
|
||||
```
|
||||
@ -544,11 +544,11 @@ page.route_web_socket("wss://example.com/ws", handler)
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("wss://example.com/ws", ws => {
|
||||
var server = ws.ConnectToServer();
|
||||
ws.OnMessage(message => {
|
||||
if (message == "request")
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text == "request")
|
||||
server.Send("request2");
|
||||
else
|
||||
server.Send(message);
|
||||
server.Send(frame.Text);
|
||||
});
|
||||
});
|
||||
```
|
||||
|
@ -13,8 +13,8 @@ New methods [`method: Page.routeWebSocket`] and [`method: BrowserContext.routeWe
|
||||
|
||||
```csharp
|
||||
await page.RouteWebSocketAsync("/ws", ws => {
|
||||
ws.OnMessage(message => {
|
||||
if (message == "request")
|
||||
ws.OnMessage(frame => {
|
||||
if (frame.Text == "request")
|
||||
ws.Send("response");
|
||||
});
|
||||
});
|
||||
|
@ -12,8 +12,8 @@ New methods [`method: Page.routeWebSocket`] and [`method: BrowserContext.routeWe
|
||||
|
||||
```java
|
||||
page.routeWebSocket("/ws", ws -> {
|
||||
ws.onMessage(message -> {
|
||||
if ("request".equals(message))
|
||||
ws.onMessage(frame -> {
|
||||
if ("request".equals(frame.text()))
|
||||
ws.send("response");
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user