client

package
v0.0.0-...-0e46d6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2024 License: GPL-3.0 Imports: 23 Imported by: 0

Documentation

Overview

Package client provides Go WebSocket client support for BareRTC.

Index

Constants

View Source
const (
	// Number of recent chat messages to hold onto.
	ScrollbackBuffer = 500

	// How long for the lobby room to be quiet before you'll greet the
	// next person who joins the room.
	LobbyDeadThreshold = 20 * time.Minute

	// Minimum time between greeting users who enter chat, IF we will
	// do so. When the rush hour picks up, don't spam too much and
	// greet everybody who enters.
	AutoGreetGlobalCooldown = 8 * time.Minute

	// Minimum time between re-greeting the same user.
	AutoGreetUserCooldown = 45 * time.Minute

	// Default (lobby) channel.
	LobbyChannel = "lobby"
)

Variables

View Source
var Embedded embed.FS

Functions

func AtMentioned

func AtMentioned(c *Client, message string) (bool, string)

AtMentioned checks if somebody has "at mentioned" your username (having your name at the beginning or end of their message). Returns whether the at mention was detected, along with the modified message without the at mention name on the end of it.

func NoReply

func NoReply(message string) bool

NoReply checks if a bot's reply contains the noreply tag.

func StripHTML

func StripHTML(s string) string

StripHTML removes HTML content from a message.

func WebSocketURL

func WebSocketURL(baseURL string) (string, error)

WebSocketURL converts the BareRTC base (https) URL into the WebSocket link.

Types

type BotHandlers

type BotHandlers struct {
	// contains filtered or unexported fields
}

BotHandlers holds onto a set of handler functions for the BareBot.

func (*BotHandlers) GetUser

func (h *BotHandlers) GetUser(username string) (*messages.WhoList, bool)

GetUser looks up a username from the Who List.

func (*BotHandlers) OnCut

func (h *BotHandlers) OnCut(msg messages.Message)

OnCut handles an admin telling us to cut our camera.

func (*BotHandlers) OnDisconnect

func (h *BotHandlers) OnDisconnect(msg messages.Message)

OnDisconnect handles kick messages from the backend (told: do not reconnect).

func (*BotHandlers) OnError

func (h *BotHandlers) OnError(msg messages.Message)

OnError handles ChatServer messages from the backend.

func (*BotHandlers) OnMe

func (h *BotHandlers) OnMe(msg messages.Message)

OnMe handles user status updates pushed by the server (renamed username, nsfw flag added)

func (*BotHandlers) OnMessage

func (h *BotHandlers) OnMessage(msg messages.Message)

OnMessage handles chat messages.

func (*BotHandlers) OnOpen

func (h *BotHandlers) OnOpen(msg messages.Message)

OnOpen handles the server echo to us wanting to open another user's webcam.

func (*BotHandlers) OnPing

func (h *BotHandlers) OnPing(msg messages.Message)

OnPing handles server keepalive pings.

func (*BotHandlers) OnPresence

func (h *BotHandlers) OnPresence(msg messages.Message)

OnPresence handles join/exit room events as well as kicked/banned messages.

func (*BotHandlers) OnReact

func (h *BotHandlers) OnReact(msg messages.Message)

OnReact handles emoji reactions to messages.

func (*BotHandlers) OnRing

func (h *BotHandlers) OnRing(msg messages.Message)

OnRing handles somebody requesting to open our webcam.

func (*BotHandlers) OnTakeback

func (h *BotHandlers) OnTakeback(msg messages.Message)

OnTakeback handles users requesting to take back messages they had sent.

func (*BotHandlers) OnUnwatch

func (h *BotHandlers) OnUnwatch(msg messages.Message)

OnUnwatch handles somebody removing themselves from our Watching list.

func (*BotHandlers) OnWatch

func (h *BotHandlers) OnWatch(msg messages.Message)

OnWatch handles somebody adding themselves to our Watching list.

func (*BotHandlers) OnWho

func (h *BotHandlers) OnWho(msg messages.Message)

OnWho handles Who List updates in chat.

func (*BotHandlers) SetUserVariables

func (h *BotHandlers) SetUserVariables(msg messages.Message)

SetUserVariables prepares RiveScript user variables before handling a message.

Example: it will set the user's `name` to their WhoList nickname, and other such flags.

User variables set include:

* name (nickname or username) * isAdmin (boolean operator status) * messageID (BareRTC MessageID)

Global variables (`<env>`) are also set here:

* numUsersOnline (int): length of who list

type Client

type Client struct {
	// Event handlers for your app to respond to.
	OnWho        HandlerFunc // Who's Online
	OnMe         HandlerFunc // Status updates for current user sent by server
	OnMessage    HandlerFunc
	OnTakeback   HandlerFunc
	OnReact      HandlerFunc
	OnPresence   HandlerFunc
	OnRing       HandlerFunc
	OnOpen       HandlerFunc
	OnWatch      HandlerFunc
	OnUnwatch    HandlerFunc
	OnCut        HandlerFunc
	OnError      HandlerFunc
	OnDisconnect HandlerFunc
	OnPing       HandlerFunc
	OnCandidate  HandlerFunc
	OnSDP        HandlerFunc
	// contains filtered or unexported fields
}

Client represents a WebSocket client connection to BareRTC.

func NewClient

func NewClient(url string, claims jwt.Claims) (*Client, error)

NewClient initializes the WebSocket connection (JWT claims required).

URL is like ws://localhost:9000/ws

func (*Client) Authenticate

func (c *Client) Authenticate() (string, error)

Authenticate with the BareRTC server, returning a signed JWT token.

This posts to the /api/authenticate endpoint on the BareRTC Web API. It is called automatically as part of the logon process in Run().

func (*Client) Handle

func (c *Client) Handle(msg messages.Message, fn HandlerFunc) error

Handle a WebSocket message. This is called internally on the read loop. It basically passes the message into the HandlerFunc, or returns an error if the HandlerFunc is nil (not defined).

Note: handler funcs are run on a background goroutine, so they can be free to use time.Sleep and delay message sending if needed.

func (*Client) Run

func (c *Client) Run() error

Run the client, connecting to the WebSocket and returning only on error or disconnect.

func (*Client) Send

func (c *Client) Send(msg messages.Message) error

Send a WebSocket message.

func (*Client) SetupChatbot

func (c *Client) SetupChatbot() error

SetupChatbot configures a sensible set of default handlers for the BareBot application.

This function is very opinionated and is designed for the BareBot program. It will initialize a RiveScript bot using the brain found at the "./brain" folder, and register handlers for the various WebSocket messages on chat.

func (*Client) Username

func (c *Client) Username() string

Username returns the bot's username.

type HandlerFunc

type HandlerFunc func(messages.Message)

HandlerFunc for WebSocket chat protocol events.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL