websocket

package
v0.0.0-...-22a3922 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BLACK int = 1

BLACK int representing color black in chess

View Source
var DEBUG_DECODE = true

DEBUG_DECODE toggles decoding debug messages in the console.

View Source
var WHITE int = 0

WHITE int representing the color white in chess

Functions

func Upgrade

func Upgrade(w http.ResponseWriter, r *http.Request) (*websocket.Conn, error)

Upgrade turns a regular http Poll into a bi-directional websocket connection

Types

type Client

type Client struct {
	ID   string
	Conn *websocket.Conn
	Pool *Pool
}

Client represents a new websocket connection, with the user's id, and the pool it communicates through.

func (*Client) Read

func (c *Client) Read()

type GameClient

type GameClient struct {
	ID       string
	Conn     *websocket.Conn
	GamePool *GamePool
}

GameClient represents a new websocket connection to manage games, with the user's id, and the game pool it is connected to.

func (*GameClient) GameRead

func (c *GameClient) GameRead()

GameRead performs the parsing of JSON message, and then appropriately broadcasts the transformed message to other users.

type GameInfo

type GameInfo struct {
	Ids     []string
	Players []string
}

GameInfo stores the info that should be sent to users seeking to display a list of gamerooms.

type GameMessage

type GameMessage struct {
	Type             int                        `json:"type"` // 0 = player connected, 1 = board update, 2 = message, 3= opponent leave, 4= spectator join/leave
	Pid              string                     `json:"pid"`
	Color            int                        `json:"color"`
	GameStart        bool                       `json:"start"`
	GameEnd          bool                       `json:"end"`
	Message          string                     `json:"message"`
	Move             [2]int                     `json:"move"`
	Board            [64]int                    `json:"board"`
	Entanglements    map[string]interface{}     `json:"entanglements"`
	Pieces           map[string]interface{}     `json:"pieces"`
	NewBoard         [64]int                    `json:"newBoard"`
	NewPieces        quantumchess.Pieces        `json:"newPieces"`
	NewEntanglements quantumchess.Entanglements `json:"newEntanglements"`
}

GameMessage allows us to unpack the content of JSON transmitted through the game pool.

type GamePool

type GamePool struct {
	ID         string
	Register   chan *GameClient
	Unregister chan *GameClient
	Clients    map[*GameClient]int // maps to BLACK or WHITE, both cannot be the same obviously
	Broadcast  chan GameMessage
	//Timer channel
	//Timeout channel
	//Players [2]string
	Start bool
	Over  bool
}

GamePool manages the communication channels of a specific Game room.

func NewGamePool

func NewGamePool(id string) *GamePool

NewGamePool builds a new game room with the given id.

func (*GamePool) StartGame

func (pool *GamePool) StartGame()

StartGame activates the websocket "listener" to manage the communication channels of the Game room.

type Message

type Message struct {
	Type    int    `json:type`
	Players string `json:"players"`
	ID      string `json:"id"`
	QueueId string `json:"queue"`
}

Message target object to decode the JSON messages of the general websocket connection.

type Pool

type Pool struct {
	Register   chan *Client
	Unregister chan *Client
	Clients    map[*Client]int
	Broadcast  chan Message
}

Pool manages the communication channels of a websocket

func NewPool

func NewPool() *Pool

NewPool creates an empty instance of a pool

func (*Pool) Start

func (pool *Pool) Start()

Start starts the websocket "listener" for the communication channels

type Rooms

type Rooms struct {
	Privacy map[string]bool //false is public, true is private
	Games   map[string]*GamePool
}

Rooms represent a struct that manages all games and their privacy.

func NewRooms

func NewRooms() *Rooms

NewRooms creates a new empty instance of Rooms

Jump to

Keyboard shortcuts

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