Documentation ¶
Overview ¶
Package signaling provides a server for signaling between clients setting up peer-to-peer WebRTC connections.
Index ¶
- Constants
- func DeserializeMsg(rawMessage []byte, pointer any, sender *User) (ok bool)
- func StartServer(port string) error
- type ConnectionSuccessMessage
- type ErrorMessage
- type JoinPeersMessage
- type LeavePeersMessage
- type Message
- type PeerExchangeMessage
- type PeerJoinedMessage
- type PeerLeftMessage
- type User
- func (user *User) HandleMessage(rawMessage []byte)
- func (user *User) HandlePeerLeft()
- func (user *User) IsPeer() bool
- func (user *User) JoinPeers(username string) error
- func (user *User) LeavePeers()
- func (user *User) Listen()
- func (user *User) Register() (userID int)
- func (user *User) SetName(username string) error
- type Users
Constants ¶
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 ¶
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 ¶
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 ¶
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 ¶
Message sent by the server to notify users of a peer leaving.
type User ¶
A user connected to the signaling server.
func NewUser ¶
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 ¶
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) JoinPeers ¶
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.