Skip to content

Commit

Permalink
Update experimental/websocket example
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 29, 2024
1 parent d6ded21 commit cd89816
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions examples/experimental/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
let chatRoomName = "publicRoom"; // choose your chat room name
let sessionDuration = randomIntBetween(5000, 60000); // user session between 5s and 1m

export default function () {
export default function() {
for (let i = 0; i < 4; i++) {
startWSWorker(i);
}
Expand All @@ -22,6 +22,7 @@ export default function () {
function startWSWorker(id) {
let url = `wss://test-api.k6.io/ws/crocochat/${chatRoomName}/`;
let ws = new WebSocket(url);
ws.binaryType = "arraybuffer";
ws.addEventListener("open", () => {
ws.send(
JSON.stringify({
Expand Down Expand Up @@ -54,15 +55,15 @@ function startWSWorker(id) {
);
}, randomIntBetween(2000, 8000)); // say something every 2-8seconds

let timeout1id = setTimeout(function () {
let timeout1id = setTimeout(function() {
clearInterval(intervalId);
console.log(
`VU ${__VU}:${id}: ${sessionDuration}ms passed, leaving the chat`
);
ws.send(JSON.stringify({ event: "LEAVE" }));
}, sessionDuration);

let timeout2id = setTimeout(function () {
let timeout2id = setTimeout(function() {
console.log(
`Closing the socket forcefully 3s after graceful LEAVE`
);
Expand Down

0 comments on commit cd89816

Please sign in to comment.