simplysocket

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2024 License: MIT Imports: 8 Imported by: 0

README

SimplySocket

SimplySocket is a Golang package designed for pushing messages to clients and managing rooms and events. This package is a part of the Words Battle game project.

Introduction

SimplySocket is built for real-time communication with clients, efficiently managing message broadcasting, room creation, and event handling. It's designed to scale well for multiplayer games and similar applications that require synchronized messaging across clients.

Features
  • Message Broadcasting: Efficiently push messages to multiple clients.
  • Room Management: Create, delete, and manage rooms for organized communication.
  • Event Handling: Trigger and handle various events seamlessly.
Project Architecture

check the Project Architecure file.

Contributing

We welcome contributions! Please feel free to submit issues, pull requests, or suggestions.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

For more detailed information, check the Documentation file.

Documentation

Index

Constants

View Source
const (
	MeshGlobalRoom = "mesh-global" //MeshGlobalRoom is a room where a client gets joined when he connects to a websocket . MeshGlobalRoom facilitates creation of rooms
)

Variables

This section is empty.

Functions

func NewMeshServer

func NewMeshServer(name string, meshconf *MeshServerConfig, rd RoomData) *meshServer

NewMeshServer initialize new websocket server

func NewRoom

func NewRoom(roomslug string, clientslug string, rd RoomData, srv *meshServer) *room

func ServeWs

func ServeWs(meshserv *meshServer, w http.ResponseWriter, r *http.Request)

ServeWs handles websocket requests from clients requests.

Types

type MeshServer

type MeshServer interface {
	GetClients() map[string]*client
	GetClientAuthMetadata(clientslug string) []string
	GetRooms() []string
	GetGameName() string
	GetClientsInRoom() map[string]map[string]*client
	DeleteRoom(name string)
	JoinClientRoom(roomname string, clientname string, rd RoomData)
	RemoveClientRoom(roomname string, clientname string)
	//PushMessage is to push message from the code not from the UI thats broadcast
	//returns a send only channel
	PushMessage() chan<- *Message
	//ReceiveMessage is to receive message from readpumps of the clients this can be used to manipulate
	//returns a receive only channel
	RecieveMessage() <-chan *Message
	//EventTriggers Track
	//Get the updates on the clients in room changes and act accordingly
	//Returns receive only channel []string length of 3 [0]-->event type [1]-->roomname [1]-->clientslug
	//event types :- client-joined-room , client-left-room
	EventTriggers() <-chan []string
}

type MeshServerConfig

type MeshServerConfig struct {
	DirectBroadCast bool
}

type Message

type Message struct {
	Action         string                 `json:"action"`       //action
	MessageBody    map[string]interface{} `json:"message_body"` //message
	IsTargetClient bool                   //not imported if its true then the Target string is a client which is one
	Target         string                 `json:"target"` //target the room
	Sender         string                 `json:"sender"` //whose readpump is used
}

Message struct is the structure of the message which is send in mesh server

func (*Message) Encode

func (message *Message) Encode() []byte

type Room

type Room interface {
	GetRoomSlugInfo() string
	GetRoomMakerInfo() string
	GetAuthMetadata() []string
	// This is to indicate that there are no clients in the room to send the message
	// If there are no clients in the room the room gets deleted from the map and this channel is closed.
	// The HandleRoomData go routine will be closed if implemented.
	RoomStopped() <-chan struct{}
	//ConsumeRoomMessage receives the messages it gets directly from the clients.
	ConsumeRoomMessage() <-chan *Message
	//This are the events such as client-joined-room,client-left-room .
	//Consist of list of 3 values :- [event,roomname,clientid]
	EventTriggers() <-chan []string
	//BroadcastMessage pushes the message to all the clients in the room .
	//Use IsTargetClient to true if you have to send the message to a particular client of the room .
	BroadcastMessage(message *Message)
}

type RoomData

type RoomData interface {
	//HandleRoomData use your struct which has all the data related to room and do the changes accordingly
	HandleRoomData(room Room, server MeshServer)
}

Jump to

Keyboard shortcuts

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