-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v5' into ui-extensions
- Loading branch information
Showing
4 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/backend/effects/builtin/send-custom-websocket-event.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { EffectType } from "../../../types/effects"; | ||
import { EffectCategory } from "../../../shared/effect-constants"; | ||
import logger from "../../logwrapper"; | ||
import WebSocketServerManager from "../../../server/websocket-server-manager"; | ||
|
||
const model: EffectType<{ | ||
eventName: string; | ||
eventData: string; | ||
}> = { | ||
definition: { | ||
id: "firebot:send-custom-websocket-event", | ||
name: "Send Custom WebSocket Event", | ||
description: "Sends a custom event and any relevant data to all connected WebSocket clients", | ||
icon: "fad fa-plug", | ||
categories: [EffectCategory.ADVANCED, EffectCategory.SCRIPTING], | ||
dependencies: [] | ||
}, | ||
optionsTemplate: ` | ||
<eos-container header="Event Name"> | ||
<p class="muted">Enter the name of the event you'd like to send. It will be sent as:<br/><code>custom-event:eventname</code></p> | ||
<firebot-input | ||
model="effect.eventName" | ||
placeholder-text="Enter event name" | ||
menu-position="under" | ||
/> | ||
</eos-container> | ||
<eos-container header="Event Data" pad-top="true"> | ||
<p class="muted">Enter any event data that you'd like to include with the event.</p> | ||
<firebot-input | ||
model="effect.eventData" | ||
placeholder-text="Enter event data" | ||
use-text-area="true" | ||
rows="4" | ||
cols="40" | ||
menu-position="under" | ||
/> | ||
</eos-container> | ||
`, | ||
optionsController: () => { }, | ||
optionsValidator: (effect) => { | ||
const errors = []; | ||
if (!(effect.eventName?.length > 0)) { | ||
errors.push("Please input an event name."); | ||
} | ||
return errors; | ||
}, | ||
onTriggerEvent: async ({ effect }) => { | ||
try { | ||
let data: unknown = effect.eventData ?? {}; | ||
|
||
try { | ||
data = JSON.parse(effect.eventData); | ||
} catch { } | ||
WebSocketServerManager.triggerEvent(`custom-event:${effect.eventName}`, data as object); | ||
} catch (error) { | ||
logger.error(`Error sending custom WebSocket event ${effect.eventName}`, error); | ||
} | ||
} | ||
}; | ||
|
||
module.exports = model; |