api

package
v0.0.0-...-90fdf19 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Protocol:

1) We log in into a room Request: {"user":"user1","room":"room1", "avatar": 12} Response: "OK"

{ "type": "login_success" } { "type": "login_fail", "reason": "already_logged_in" } { "type": "login_fail", "reason": "room full" }

2) User can send a chat message into the room Request: {"type":"chat","text":"Hello world"}

3) Server can push back other users' chat messages Pushing:

{
	"type":"chat",
	"messages": [
		{
			"text":"Hello world",
			"user":"user2",
			"created":"2020-01-01 00:00:00"
		},
		{
			"text":"Hello world",
			"user":"user3",
			"created":"2020-01-01 00:00:00"
		},
	]
}

4) Sets the current state of the room. Pushing {"type":"room","users":[{name:"user1", avatar:12, is_host:true}]}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChatBatch

type ChatBatch struct {
	Type     string          `json:"type"`
	Messages []ChatBatchItem `json:"messages"`
}

type ChatBatchItem

type ChatBatchItem struct {
	User    string `json:"user"`
	Text    string `json:"text"`
	Created string `json:"created"`
}

type GameAction

type GameAction struct {
	Action  string          `json:"action"`            // kind of action
	Payload json.RawMessage `json:"payload,omitempty"` // optional payload
}

type KickMessage

type KickMessage struct {
	User string `json:"user"`
}

type KickResp

type KickResp struct {
	Type   string `json:"type"`
	Reason string `json:"reason,omitempty"`
}

type LoginReq

type LoginReq struct {
	User   string `json:"user"`
	Room   string `json:"room"`
	Avatar int    `json:"avatar"`
}

type LoginResp

type LoginResp struct {
	Type   string `json:"type"`
	Room   string `json:"room"`
	Reason string `json:"reason,omitempty"`
}

type NewMessage

type NewMessage struct {
	Text string `json:"text"`
}

type Room

type Room struct {
	Name     string
	Host     string // name of the user who created the room
	Registry map[string]StartGameFn
	// contains filtered or unexported fields
}

func NewRoom

func NewRoom(registry map[string]StartGameFn, name, host string) *Room

func (*Room) Broadcast

func (r *Room) Broadcast(msg any)

func (*Room) Handle

func (r *Room) Handle(c *websocket.Conn, l *LoginReq) error

func (*Room) SendGameAction

func (r *Room) SendGameAction(action string, payload any, users ...string)

func (*Room) SendGameChat

func (r *Room) SendGameChat(m *game.NewChatMessage, users ...string)

SendGameChat message to a given user, without adding it to the history.

func (*Room) Users

func (r *Room) Users() []string

type RoomMsg

type RoomMsg struct {
	Type  string     `json:"type"`
	Users []UserInfo `json:"users"`
	Game  string     `json:"game,omitempty"` // name of the game, if started
}

type Server

type Server struct {
	Registry map[string]StartGameFn

	Rooms map[string]*Room
	// contains filtered or unexported fields
}

func NewServer

func NewServer(games map[string]StartGameFn) *Server

func (*Server) Handle

func (s *Server) Handle(w http.ResponseWriter, r *http.Request)

type StartGameFn

type StartGameFn func(room *Room, setting json.RawMessage) (game.Game, error)

type StartGameMessage

type StartGameMessage struct {
	Game     string          `json:"game"`
	Settings json.RawMessage `json:"settings"`
}

type User

type User struct {
	Name   string
	Avatar int
	C      *websocket.Conn
}

type UserInfo

type UserInfo struct {
	Name   string `json:"name"`
	IsHost bool   `json:"is_host"`
	Avatar int    `json:"avatar"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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