2

I have an ASP.NET Core Web API project that is using SignalR, I have a hub there which I am connecting to it using k6 (I want to do some load testings) I manage to connect to my hub but I can not figure out how to call a function from my server, my code is

import ws from 'k6/ws';
import { check } from 'k6';

export default function () {
    var token = "Bearer userAccessToken";
    const url = 'wss://localhost:5001/session';
    const params = { headers: { "Authorization": token } };

    const res = ws.connect(url, params, function (socket) {
        socket.on('open', () => {
            console.log("opened");
            socket.send(JSON.stringify({ UserId: "aUserId", GameId: "AGameId" }))
        });
        socket.on('close', () => console.log('disconnected'));
    });

    check(res, { 'status is 101': (r) => r && r.status === 101 });
}

My function is called joinGameSession and it takes two variables the user id and the gameId

public async Task<bool> JoinGameSession(JoinGameRequest request)
{
    return true;
}

I have managed to trigger functions using Microsoft's SignalR client.

const signalR = require("@microsoft/signalr");
require('dotenv').config();

var token = process.env.token ?? "";

var questionIndex = 0;
let connection = new signalR.HubConnectionBuilder()
    .withUrl("http://localhost:5000/session", { headers: { "Authorization": token } })
    .withAutomaticReconnect()
    .build();

connection.start().then(() => {
    connection.invoke("JoinGameSession", { UserId: "a", GameId: "x" });
}).catch(e => {
    console.log(e);
})

but I can not do it with k6, is there any other tools to achieve my goal?

Thank you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MoTahir
  • 863
  • 7
  • 22
  • Can you check the messages sent by SignalR in your browser's dev console? Your k6 payload is missing the "JoinGameSession" string. The format is probably a little bit different – knittl Jul 19 '22 at 05:37
  • what do you mean by "my browser dev console"? in my javascript client I can easily get messages from signlar and I can call functions too, and ass for the missing "JoinGameSession" I know it is missing, the thing is, where do I add it? – MoTahir Jul 19 '22 at 18:27
  • How is SignalR sending the request? Can you run your JavaScript client in your browser? If you can, you can view the WebSocket messages and mimic their format. If you can't, you need to somehow log the raw messages on the server, intercept your data stream, or read the documentation to find out how those WebSocket messages are supposed to look like. – knittl Jul 19 '22 at 19:08

1 Answers1

0

I had a similar issue and found an old thread which managed to solve it.

  ws.connect(wsUrl, function (socket) {
    socket.on('open', () => {
      socket.send('{"protocol":"json","version":1}\x1e') // add this
    });
    socket.on('message', () => console.log('message')); 
    socket.on('close', () => console.log('closed')); 
  });

This should allow you to connect to your SignalR hub performing the appropriate handshake. In SignalR, the client and server communicate using a custom protocol. The message being sent in this case is a handshake message that informs the server about the protocol and version the client supports.

Luke Garrigan
  • 4,571
  • 1
  • 21
  • 29