Skip to content

Chatbox Websocket client #5744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 16, 2025
Merged

Conversation

flleeppyy
Copy link
Member

@flleeppyy flleeppyy commented Mar 2, 2025

About The Pull Request

This PR adds a websocket client to the chatbox, that redirects all incoming payloads from the server, to the specified server, which allows for external integrations with the game. All of which is parsable JSON.

Some ideas include RGB feedback, triggering an event in VTubing software, or automating a physical device based on what's sent to the chat. (For example, a haptic suit, and have a regex for "in the right leg" or any body part)

Works on 515 and 516. (I didn't realize IE11 had websockets until I went to go and make sure my code to disable it worked, then had to double check myself because it was connecting to my websocket server, and working. so. LOL)

Here's a barebones snippet, not feature complete but it has enough where you can start with phrase

const ws = require("ws");
const phrases = require("./phrases");

const LISTENPORT = 8094;
const wss = new ws.Server({ port: LISTENPORT });

wss.on("listening", () => console.log(`Listening on ${LISTENPORT}`));

wss.on("connection", (socket) => {
  console.log("Client connected!");

  socket.on("message", async (data) => {
    try {
      const messageData = JSON.parse(data.toString("utf-8"));
      if (messageData.type !== "chat/message") return;
      const parsedPayload = JSON.parse(messageData.payload);

      if (!parsedPayload?.content?.html) return;

      const message = parsedPayload.content.html.toLowerCase();
      const matchedPhrase = phrases.phrases.find((phrase) =>
        message.match(phrase.regex)
      );

      if (matchedPhrase) {
        // Catched phrase could be something like "stabs you in the chest with the kitchen knife"
        // /(userdanger'>(?:.*you.*in the.*with the))/gi
        
        // example, setup a websocket client to connect to vNyan and send the command "modelBleed"
        if (matchedPhrase.type === "vnyanAction")
          vnyanWs.send(JSON.stringify(matchedPhrase.command))
      }
    } catch (e) {
      console.error("Message Processing Error:", e);
    }
  });

  socket.on("close", () => console.log("Client disconnected."));
});
modules.export = {
  phrases: [
      {
        regex: /(userdanger'>(?:.*you.*in the.*with the))/gi,
        type: "vnyanAction",
        command: "modelBleed"
    },
  ]
}

Why It's Good For The Game

QoL for people who want things outside of the game to respond to in-game actions. See above examples.

Changelog

🆑
add: Chatbox websocket feature
/:cl:

@Absolucy Absolucy added peak Feature: Feature The creature feature. featuring: the creature. labels Mar 2, 2025
@Addust
Copy link
Contributor

Addust commented Mar 2, 2025

changing music volume apparently triggers a "websocket disabled" message. how.

@flleeppyy
Copy link
Member Author

fixed

changing music volume apparently triggers a "websocket disabled" message. how.

@LikeLakers2
Copy link
Collaborator

This seems to be causing a problem for one user - #5791

@Absolucy Absolucy merged commit ff414f3 into Monkestation:master Mar 16, 2025
25 checks passed
github-actions bot added a commit that referenced this pull request Mar 16, 2025
Absolucy pushed a commit that referenced this pull request Mar 20, 2025
…#5881)

## About The Pull Request

Handful of things.
- Mostly ports tgstation/tgstation#89811
- - Code was changed so it's compatible with 515 and 516 via
`Byond.saveBlob`
- Replaces some `if (type === action.type)` with switch cases
- Renames a few files from `js` to `ts`
- Fixes resizable textareas not dynamically 

will likely conflict with #5744

## Why It's Good For The Game

Ability to export/import chat settings would be very helpful for the
515->516 migration.

## Changelog
:cl:Kashargul
add: You can now import and export chat settings
/:cl:
@Addust
Copy link
Contributor

Addust commented Apr 23, 2025

OOC > addust johnson hits you in the right leg with the mechanical toolbox > hurt the right legs of everyone who dares hook up a haptic suit to ss13

(add a note to make regex ignore OOC or face the consequences)

@flleeppyy
Copy link
Member Author

OOC > addust johnson hits you in the right leg with the mechanical toolbox > hurt the right legs of everyone who dares hook up a haptic suit to ss13

(add a note to make regex ignore OOC or face the consequences)

LOL hahehaheheh
thats something you gotta deal with in your own implementation. The websocket client sends all payloads to the chatbox, to the websocket server you connect it to. You'll be fine as long as you do something like

socket.on("message", async (data) => {
    try {
      const messageData = JSON.parse(data.toString("utf-8"));
      
      if (messageData.type !== "chat/message") return;
      
      const parsedPayload = JSON.parse(messageData.payload);

      if (!parsedPayload?.content?.html) return;

      const message = parsedPayload.content.html.toLowerCase();
      
      if (message.includes('<span class="prefix">OOC:</span>') || message.includes('<span class="ooc"')) return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Feature The creature feature. featuring: the creature. peak
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants