socket

package
v0.0.0-...-c857007 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2020 License: MIT Imports: 6 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

func CloseConn

func CloseConn(conn *websocket.Conn, reason string)

CloseConn closes the websocket connection without reporting any errors

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
	// Log is used to log errors and other information
	Log *log.Logger
	// TimeFunc is a function which should supply the current time since the unix epoch.
	// Used to set ping/pong deadlines
	TimeFunc func() int64
	// PongPeriod is the amount of time that between messages that can bass before the connection is invalid
	PongPeriod time.Duration
	// PingPeriod is the amount of time between sending ping messages to the connection to keep it active
	// Should be less than pongPeriod
	PingPeriod time.Duration
	// IdlePeroid is the amount of time that can pass between handling messages that are not pings before the connection is idle and will be disconnected
	IdlePeriod time.Duration
	// HTTPPingPeriod is the amount of time between sending requests for the connection to send a http ping on a different socket
	// Heroku servers shut down if 30 minutes passess between HTTP requests
	HTTPPingPeriod time.Duration
}

Config contains commonly shared Socket properties

func (Config) NewSocket

func (cfg Config) NewSocket(conn *websocket.Conn, playerName game.PlayerName) (*Socket, error)

NewSocket creates a socket

type Socket

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

Socket reads and writes messages to the browsers

func (*Socket) Run

func (s *Socket) Run(ctx context.Context, removeSocketFunc context.CancelFunc, readMessages chan<- game.Message, writeMessages <-chan game.Message)

Run writes Socket messages to the messages channel and reads incoming messages on separate goroutines The Socket runs until the connection fails for an unexpected reason or a message is received on the "done"< channel. Messages the socket receives are sent to the provided channel. Messages the socket sends are consumed from the returned channel.

Jump to

Keyboard shortcuts

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