Skip to content

Commit

Permalink
Update experimental websocket onping/onpong examples (#1475)
Browse files Browse the repository at this point in the history
  • Loading branch information
immavalls authored Jan 12, 2024
1 parent 363d7fb commit 10929f4
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ _A k6 script that initiates a WebSocket connection and sets up a handler for the
import { WebSocket } from 'k6/experimental/websockets';

export default function () {
const ws = new WebSocket('ws://localhost:10000');
const ws = new WebSocket('wss://test-api.k6.io/ws/crocochat/publicRoom/');

ws.onping = () => {
console.log('A ping happened!');
ws.close();
};
}
```

{{< /code >}}

The preceding example uses a WebSocket echo server, which you can run with the following command:
ws.onclose = () => {
console.log('WebSocket connection closed!');
}

{{< code >}}

```bash
$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server
ws.onopen = () => {
ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}` }));
}
ws.onerror = (err) => {
console.log(err)
}
}
```

{{< /code >}}
{{< /code >}}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export default function () {

ws.onpong = () => {
console.log('A pong happened!');
ws.close();
};

ws.onopen = () => {
ws.ping();
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ _A k6 script that initiates a WebSocket connection and sets up a handler for the
import { WebSocket } from 'k6/experimental/websockets';

export default function () {
const ws = new WebSocket('ws://localhost:10000');
const ws = new WebSocket('wss://test-api.k6.io/ws/crocochat/publicRoom/');

ws.onping = () => {
console.log('A ping happened!');
ws.close();
};
}
```

{{< /code >}}

The preceding example uses a WebSocket echo server, which you can run with the following command:
ws.onclose = () => {
console.log('WebSocket connection closed!');
}

{{< code >}}

```bash
$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server
ws.onopen = () => {
ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}` }));
}
ws.onerror = (err) => {
console.log(err)
}
}
```

{{< /code >}}
{{< /code >}}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export default function () {

ws.onpong = () => {
console.log('A pong happened!');
ws.close();
};

ws.onopen = () => {
ws.ping();
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,24 @@ _A k6 script that initiates a WebSocket connection and sets up a handler for the
import { WebSocket } from 'k6/experimental/websockets';

export default function () {
const ws = new WebSocket('ws://localhost:10000');
const ws = new WebSocket('wss://test-api.k6.io/ws/crocochat/publicRoom/');

ws.onping = () => {
console.log('A ping happened!');
ws.close();
};
}
```

{{< /code >}}

The preceding example uses a WebSocket echo server, which you can run with the following command:
ws.onclose = () => {
console.log('WebSocket connection closed!');
}

{{< code >}}

```bash
$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server
ws.onopen = () => {
ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}` }));
}
ws.onerror = (err) => {
console.log(err)
}
}
```

{{< /code >}}
{{< /code >}}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ export default function () {

ws.onpong = () => {
console.log('A pong happened!');
ws.close();
};

ws.onopen = () => {
ws.ping();
}
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ _A k6 script that initiates a WebSocket connection and sets up a handler for the
import { WebSocket } from 'k6/experimental/websockets';

export default function () {
const ws = new WebSocket('ws://localhost:10000');
const ws = new WebSocket('wss://test-api.k6.io/ws/crocochat/publicRoom/');

ws.onping = () => {
console.log('A ping happened!');
ws.close();
};
}
```

</CodeGroup>

The preceding example uses a WebSocket echo server, which you can run with the following command:
ws.onclose = () => {
console.log('WebSocket connection closed!');
}

<CodeGroup>

```bash
$ docker run --detach --rm --name ws-echo-server -p 10000:8080 jmalloc/echo-server
ws.onopen = () => {
ws.send(JSON.stringify({ 'event': 'SET_NAME', 'new_name': `Croc ${__VU}` }));
}
ws.onerror = (err) => {
console.log(err)
}
}
```

</CodeGroup>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export default function () {

ws.onpong = () => {
console.log('A pong happened!');
ws.close();
};

ws.onopen = () => {
ws.ping();
}
}
```

Expand Down

0 comments on commit 10929f4

Please sign in to comment.