signaling

package
v0.0.0-...-7a63d8b Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package signaling provides a server for signaling between clients setting up peer-to-peer WebRTC connections.

Index

Constants

View Source
const (
	MsgPeerOffer         string = "peer-offer"
	MsgPeerAnswer        string = "peer-answer"
	MsgICECandidate      string = "ice-candidate"
	MsgJoinPeers         string = "join-peers"
	MsgLeavePeers        string = "leave-peers"
	MsgPeerJoined        string = "peer-joined"
	MsgPeerLeft          string = "peer-left"
	MsgConnectionSuccess string = "connection-success"
	MsgError             string = "error"
)

Message types allowed to and from this server.

Variables

This section is empty.

Functions

func DeserializeMsg

func DeserializeMsg(rawMessage []byte, pointer any, sender *User) (ok bool)

Deserializes the given raw message into the given pointer. If deserialization failed, sends an error message to the given sender and returns false. Otherwise returns true.

func StartServer

func StartServer(port string) error

Starts a WebRTC signaling server on the given port. Keeps running until an error occurs.

Types

type ConnectionSuccessMessage

type ConnectionSuccessMessage struct {
	Message       // Type: MsgConnectionSuccess
	PeerCount int `json:"peerCount"` // Number of active peers in the stream.
}

Message sent when a user successfully establishes a socket connection to the server.

type ErrorMessage

type ErrorMessage struct {
	Message             // Type: MsgError
	ErrorMessage string `json:"errorMessage"`
}

Message sent from server to client when a received message causes an error.

type JoinPeersMessage

type JoinPeersMessage struct {
	Message        // Type: MsgJoinPeers
	Name    string `json:"name"` // The name that the client wants to use in the stream.
}

Message sent by client when they want to join the peer-to-peer stream.

type LeavePeersMessage

type LeavePeersMessage struct {
	Message // Type: MsgLeavePeers
}

Message sent by client when they want to leave the peer-to-peer stream. Identical to base Message; included here for documentation purposes.

type Message

type Message struct {
	Type string `json:"type"`
}

Base struct to embed in all message types.

type PeerExchangeMessage

type PeerExchangeMessage struct {
	Message           // Type: MsgPeerOffer/MsgPeerAnswer/MsgICECandidate
	ReceiverID int    `json:"receiverId"`
	SenderID   int    `json:"senderId"`
	SenderName string `json:"senderName"`

	// WebRTC data for setting up peer-to-peer connection. Format depends on message type.
	//
	// For peer offer/answer messages:
	// SDP (Session Description Protocol) object with initial config proposal for the connection.
	//
	// For ICE (Interactive Connectivity Establishment) candidate messages:
	// Candidate object used to negotiate peer-to-peer connection setup.
	//
	// Typed as any, as the server only forwards the message and does not care about its type.
	Data any `json:"data"`
}

Message sent between two clients when initiating a peer-to-peer connection between each other.

func (*PeerExchangeMessage) Validate

func (message *PeerExchangeMessage) Validate(sender *User) (receiver *websocket.Conn, valid bool)

Validates the given peer exchange message. If valid, sets the message's sender fields to the given sender's ID and name, and returns the receiving peer's connection.

type PeerJoinedMessage

type PeerJoinedMessage struct {
	Message        // Type: MsgPeerJoined/MsgPeerLeft
	ID      int    `json:"id"`
	Name    string `json:"name"`
}

Message sent by server to notify users that a new peer has joined the stream.

type PeerLeftMessage

type PeerLeftMessage struct {
	Message     // Type: MsgPeerLeft
	ID      int `json:"id"`
}

Message sent by the server to notify users of a peer leaving.

type User

type User struct {
	ID     int
	Name   string
	Socket *websocket.Conn
	Lock   *sync.RWMutex
}

A user connected to the signaling server.

func NewUser

func NewUser(socket *websocket.Conn) (user *User, userID int)

Creates a new user from the given socket connection. Adds the user to the map of users, and adds a handler for removing them on socket close. Also starts a goroutine to listen to their messages.

func (*User) HandleMessage

func (user *User) HandleMessage(rawMessage []byte)

Handles the incoming message from the given user.

func (*User) HandlePeerLeft

func (user *User) HandlePeerLeft()

Sends a message to all other users that the given user has left the peer-to-peer stream.

func (*User) IsPeer

func (user *User) IsPeer() bool

Returns whether the given user has joined the peer-to-peer stream.

func (*User) JoinPeers

func (user *User) JoinPeers(username string) error

Joins the peer-to-peer stream with the given username, and notifies other users. Returns error if joining failed.

func (*User) LeavePeers

func (user *User) LeavePeers()

Removes the user from the peer-to-peer stream, and notifies other users.

func (*User) Listen

func (user *User) Listen()

Listens for WebSocket messages from the given user, and forwards them to HandleMessage. Stops when the socket is closed.

func (*User) Register

func (user *User) Register() (userID int)

Adds the given user to the users map, giving them a unique userID and returning it.

func (*User) SetName

func (user *User) SetName(username string) error

Sets the name of the user to the given username. Returns error if it fails.

type Users

type Users struct {
	Map  map[int]*User
	Lock *sync.RWMutex
}

Map of user IDs to users connected to the server, with a mutex for thread-safe modification.

func (*Users) Get

func (users *Users) Get(userID int) (user *User, ok bool)

Returns the user of the given userID from the users map, or ok=false if not found.

func (*Users) PeerCount

func (users *Users) PeerCount() int

Returns number of users who have joined the peer-to-peer stream.

Jump to

Keyboard shortcuts

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