No results for

Powered byAlgolia

Websockets

suggest edits

Testing a WebSocket API

websocket-example.js
1import ws from 'k6/ws';
2import { check } from 'k6';
3
4export default function () {
5 var url = 'ws://echo.websocket.org';
6 var params = { tags: { my_tag: 'hello' } };
7
8 var res = ws.connect(url, params, function (socket) {
9 socket.on('open', function open() {
10 console.log('connected');
11
12 socket.setInterval(function timeout() {
13 socket.ping();
14 console.log('Pinging every 1sec (setInterval test)');
15 }, 1000);
16 });
17
18 socket.on('ping', function () {
19 console.log('PING!');
20 });
21
22 socket.on('pong', function () {
23 console.log('PONG!');
24 });
25
26 socket.on('close', function () {
27 console.log('disconnected');
28 });
29
30 socket.setTimeout(function () {
31 console.log('2 seconds passed, closing the socket');
32 socket.close();
33 }, 2000);
34 });
35
36 check(res, { 'status is 101': (r) => r && r.status === 101 });
37}