npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@four-leaf-studios/rl-overlay-tool

v1.0.1

Published

A tool for Rocket League overlays

Downloads

8

Readme

WebSocket Module Documentation

This module provides functionality to connect to a WebSocket server, handle incoming data, and automatically manage reconnections. It includes methods to convert data from Rocket League's SOS plugin into a custom format and to listen to WebSocket events.

Functions

convertData(data, mapping)

Converts data based on a given mapping object.

  • Parameters:
    • data (Object): The data to be converted.
    • mapping (Object): The mapping object defining the conversion.
  • Returns:
    • Object: Converted data.

convertSosData(sosData, mapping)

Converts Rocket League SOS plugin data to a custom format.

  • Parameters:
    • sosData (Object): Data from Rocket League's SOS plugin.
    • mapping (Object): Custom data mapping.
  • Returns:
    • Object: Custom formatted data.

connectToWebSocket(wsUrl, onData, config)

Attempts to connect to the WebSocket server, handling automatic reconnection.

  • Parameters:
    • wsUrl (string): The WebSocket server URL.
    • onData (function): Callback function to handle incoming data.
    • config (Object): Configuration object.
      • mapping (Object): Mapping object for data conversion.
      • eventFilters (Array): List of events to filter.
      • errorHandling (Object): Error handling configuration.
        • retryInterval (number): Interval between reconnection attempts.
        • maxRetries (number): Maximum number of reconnection attempts.
      • logging (boolean): Enable or disable logging.
      • dataTransformation (function): Function to transform data before passing to the callback.
      • callbacks (Object): Callback functions for connection events.
        • onConnect (function): Function to call on connection.
        • onDisconnect (function): Function to call on disconnection.
      • reconnect (function): Function returning a boolean indicating whether to attempt reconnection.
      • enable (function): Function returning a boolean indicating whether to enable the connection.

listenToWebSocket(wsUrl, onData, config)

Listens to the WebSocket server and streams data to the client.

  • Parameters:
    • wsUrl (string): The WebSocket server URL.
    • onData (function): Callback function to handle incoming data.
    • config (Object): Configuration object (same as connectToWebSocket).
  • Returns:
    • function: Function to stop the WebSocket connection.

listenToWebSocketOnPort(onData, config)

Listens to the WebSocket server on a specific port and streams data to the client.

  • Parameters:
    • onData (function): Callback function to handle incoming data.
    • config (Object): Configuration object (same as connectToWebSocket).
  • Returns:
    • function: Function to stop the WebSocket connection.

Configuration Fields

  • mapping (Object): Defines how to convert the incoming SOS plugin data into a custom format.

    • event (string or function): Maps the event type.
    • game (Object): Maps the game data fields.
      • arena (string or function): Maps the arena field.
      • ball (string or function): Maps the ball field.
      • teams (string or function): Maps the teams field.
      • time (string or function): Maps the time field.
      • winner (string or function): Maps the winner field.
    • players (function): Maps the players' data.
  • eventFilters (Array): Specifies which events to listen for. Only these events will trigger the onData callback.

  • errorHandling (Object): Configuration for handling connection errors.

    • retryInterval (number): Time in milliseconds between reconnection attempts.
    • maxRetries (number): Maximum number of reconnection attempts before giving up.
  • logging (boolean): Enables or disables logging of connection events and errors.

  • dataTransformation (function): A function to transform the data before passing it to the onData callback.

  • callbacks (Object): Defines callback functions for connection events.

    • onConnect (function): Called when a connection is established.
    • onDisconnect (function): Called when a connection is closed.
  • reconnect (function): A function that returns a boolean indicating whether to attempt reconnection. This can be used to control reconnection logic dynamically.

  • enable (function): A function that returns a boolean indicating whether the connection should be enabled. This can be used to enable or disable the connection dynamically.

Example Usage

const { listenToWebSocketOnPort } = require("rl-overlay-tool");
const config = {
  mapping: {
    event: "event",
    game: {
      arena: (data) => data?.arena,
      ball: (data) => data?.ball,
      teams: (data) => data?.teams,
      time: (data) => data?.time_seconds,
      winner: (data) => data?.winner,
    },
    players: (data) =>
      data.players
        ? Object.keys(data.players).map((playerId) => {
            const player = data.players[playerId];
            return {
              id: player.id,
              name: player.name,
              team: player.team,
              score: player.score,
              goals: player.goals,
              assists: player.assists,
              saves: player.saves,
              shots: player.shots,
            };
          })
        : [],
  },
  eventFilters: [
    "sos:version",
    "game:match_created",
    "game:initialized",
    "game:pre_countdown_begin",
    "game:post_countdown_begin",
    "game:update_state",
    "game:ball_hit",
    "game:statfeed_event",
    "game:goal_scored",
    "game:replay_start",
    "game:replay_will_end",
    "game:replay_end",
    "game:match_ended",
    "game:podium_start",
    "game:match_destroyed",
  ],
  errorHandling: {
    retryInterval: 1000,
    maxRetries: 5,
  },
  logging: true,
  connectionSettings: {
    url: "ws://localhost",
    port: 49122,
  },
  dataTransformation: (data) => {
    if (data.players) {
      data.players.forEach((player) => {
        player.displayName = `${player.name} (${player.team})`;
      });
    }
    return data;
  },
  callbacks: {
    onConnect: () => console.log("Connected to WebSocket server"),
    onDisconnect: () => console.log("Disconnected from WebSocket server"),
  },
  reconnect: () => true,
  enable: () => true,
};

let stopWebSocket;

// Function to start listening to the WebSocket server
function startListening() {
  stopWebSocket = listenToWebSocketOnPort((data) => {
    console.log("Received data:", data);
  }, config);
  console.log("Started listening to the WebSocket server on port 49122");
}

// Function to stop listening to the WebSocket server
function stopListening() {
  if (stopWebSocket) {
    stopWebSocket();
    console.log("Stopped listening to the WebSocket server");
  }
}

// Start listening
startListening();

// Stop listening after some time (e.g., 10 seconds)
setTimeout(() => {
  stopListening();
}, 10000);