socket

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Rendered for js/wasm

Overview

Package socket contains the logic to communicate with the server for the game via websocket communication

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DOM added in v1.2.0

type DOM interface {
	QuerySelector(query string) js.Value
	QuerySelectorAll(document js.Value, query string) []js.Value
	SetChecked(query string, checked bool)
	NewWebSocket(url string) js.Value
	EncodeURIComponent(str string) string
	NewJsFunc(fn func()) js.Func
	NewJsEventFunc(fn func(event js.Value)) js.Func
	AlertOnPanic()
}

DOM interacts with the page.

type Game

type Game interface {
	// ID is the id for the game.
	ID() game.ID
	// Leave removes the user from his current game.
	Leave()
	// UpdateInfo updates the game for the specified message.
	UpdateInfo(m message.Message)
}

Game is the game the user is currently playing.

type Lobby

type Lobby interface {
	SetGameInfos(gameInfos []game.Info, username string)
}

Lobby is used to display available games and give users a place to join a game from.

type Log added in v1.2.0

type Log interface {
	Info(text string)
	Warning(text string)
	Error(text string)
	Chat(text string)
}

Log is notify users about changes to the game.

type Socket

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

Socket can be used to easily push and pull messages from the server.

func New

func New(dom DOM, log Log, user User, game Game, lobby Lobby) *Socket

New creates a new socket.

func (*Socket) Close

func (s *Socket) Close()

Close releases the websocket

func (*Socket) Connect

func (s *Socket) Connect(event js.Value) error

Connect establishes the websocket connection if it has not yet been established.

func (*Socket) InitDom

func (s *Socket) InitDom(ctx context.Context, wg *sync.WaitGroup)

InitDom registers socket dom functions.

func (*Socket) Send

func (s *Socket) Send(m message.Message)

Send delivers a message to the server via it's websocket.

type User

type User interface {
	// JWT gets the user's Java Web Token.
	JWT() string
	// Username gets the user's username.
	Username() string
	// Logout releases the use credentials from the browser.
	Logout()
}

User is the state of the current user.

Jump to

Keyboard shortcuts

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