socket

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package socket handles communication with a player using a websocket connection

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Debug is a flag that causes the socket to log the types non-ping/pong messages that are read/written
	Debug bool
	// ReadWait is the amout of time that can pass between receiving client messages before timing out.
	ReadWait time.Duration
	// WriteWait is the amout of time that the socket can take to write a message.
	WriteWait time.Duration
	// PingPeriod is how often ping messages should be sent.  Should be less than WriteWait.
	PingPeriod time.Duration
	// HTTPPingPeriod is how frequently to ask the client to send an HTTP request, as Heroku servers shut down if 30 minutes passess between HTTP requests.
	HTTPPingPeriod time.Duration
	// TimeFunc is a function which should supply the current time since the unix epoch.
	// Used to update the read deadline.
	TimeFunc func() int64
}

Config contains commonly shared Socket properties

func (Config) NewSocket

func (cfg Config) NewSocket(log *log.Logger, pn player.Name, conn Conn) (*Socket, error)

NewSocket creates a socket

type Conn

type Conn interface {
	// ReadJSON reads the next message from the connection.
	ReadMessage(m *message.Message) error
	// WriteJSON writes the message to the connection.
	WriteMessage(m message.Message) error
	// SetReadDeadline sets how long a read can take before it returns an error.
	SetReadDeadline(t time.Time) error
	// SetWriteDeadline sets how long a read can take before it returns an error.
	SetWriteDeadline(t time.Time) error
	// SetPongHandler is triggered when the server receives a pong response from a previous ping
	SetPongHandler(h func(appData string) error)
	// Close closes the connection.
	Close() error
	// WritePing writes a ping message on the connection.
	WritePing() error
	// WriteClose writes a close message on the connection.  The connestion is NOT closed.
	WriteClose(reason string) error
	// IsNormalClose determines if the error message is an error that implies a normal close or is unexpected.
	IsNormalClose(err error) bool
	// RemoteAddr gets the remote network address of the connection.
	RemoteAddr() net.Addr
}

Conn is the connection than backs the socket

type Runner

type Runner struct {
	RunnerConfig
	// contains filtered or unexported fields
}

Runner handles sending messages to different sockets. The runner allows for players to open multiple sockets, but multiple sockets cannot play in the same game before first leaving.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup, in <-chan message.Message, inSM <-chan message.Socket) <-chan message.Message

Run consumes messages from the message channel. This channel is used to create sockets and send messages from games to them. The messages received from sockets are sent on an "out" channel to be read by games.

type RunnerConfig

type RunnerConfig struct {
	// Debug is a flag that causes the game to log the types messages that are read.
	Debug bool
	// The maximum number of sockets.
	MaxSockets int
	// The maximum number of sockets each player can open.  Must be no more than maxSockets.
	MaxPlayerSockets int
	// The config for creating new sockets
	SocketConfig Config
}

RunnerConfig is used to create a socket Runner.

func (RunnerConfig) NewRunner

func (cfg RunnerConfig) NewRunner(log *log.Logger) (*Runner, error)

NewRunner creates a new socket runner from the config.

type Socket

type Socket struct {
	Conn       Conn
	PlayerName player.Name
	Addr       message.Addr
	Config
	// contains filtered or unexported fields
}

Socket reads and writes messages to the browsers

func (*Socket) Run

func (s *Socket) Run(ctx context.Context, wg *sync.WaitGroup, in <-chan message.Message, out chan<- message.Message)

Run writes messages from the connection to the shared "out" channel. Run writes messages received from the "in" channel to the connection, The run stays active even if it errors out. The only way to stop it is by closing the 'in' channel. If the connection has an error, the socket will send a socketClose message on the out channel, but will still consume and ignore messages from the in channel until it is closed this prevents a channel blockage.

Directories

Path Synopsis
Package gorilla implements a websocket connection by wrapping gorilla/websocket.
Package gorilla implements a websocket connection by wrapping gorilla/websocket.

Jump to

Keyboard shortcuts

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