ayu

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 14, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

ayu

Go Reference Go Report Card

ayu is WebRTC Signaling Server with ayame-like protocol.

  • Scalable: ayu uses Redis to store room states, so it can be used on serverless platforms (e.g. Cloud Run).
  • Composable: ayu provides a net/http compatible WebSocket handler.
  • Customizable: ayu provides authentication and logger interface, which can be customized.
  • No vendor lock-in: ayu depends only on Redis. It is not locked in to any specific cloud provider.

Usage

In the following example, ws://localhost:8080/signaling will be the endpoint of the signaling server. Please see ayame-spec for details.

package main

import (
	"log"
	"net/http"

	"github.com/castaneai/ayu"
	"github.com/go-redis/redis/v8"
)

func main() {
	redisClient := redis.NewClient(&redis.Options{Addr: "x.x.x.x:6379"})
	sv := ayu.NewServer(redisClient)
	defer sv.Shutdown()
	http.Handle("/signaling", sv)

	addr := ":8080"
	log.Printf("listening on %s...", addr)
	log.Fatal(http.ListenAndServe(addr, nil))
}

Testing

make test

License

Apache-2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AcceptMessage

type AcceptMessage struct {
	Type          MessageType  `json:"type"`
	IceServers    []*ICEServer `json:"iceServers"`
	IsExistClient bool         `json:"isExistClient"`
	IsExistUser   bool         `json:"isExistUser"` // for compatibility
}

AcceptMessage represents a register message. See https://github.com/OpenAyame/ayame-spec for details

type Authenticator

type Authenticator interface {
	// Authenticate authenticates a client registration.
	Authenticate(ctx context.Context, req *AuthnRequest) (*AuthnResponse, error)
}

Authenticator is a authentication logic for registration.

type AuthnRequest

type AuthnRequest struct {
	RoomID        RoomID
	ClientID      ClientID
	ConnectionID  connectionID
	AuthnMetadata map[string]interface{}
}

AuthnRequest is a request for Authenticator.

type AuthnResponse

type AuthnResponse struct {
	Allowed       bool
	Reason        string
	ICEServers    []*ICEServer
	AuthzMetadata map[string]interface{}
}

AuthnResponse is a response for Authenticator.

type ByeMessage

type ByeMessage struct {
	Type MessageType `json:"type"`
}

ByeMessage represents a bye message. See https://github.com/OpenAyame/ayame-spec for details

type CandidateMessage

type CandidateMessage struct {
	Type         MessageType       `json:"type"`
	ICECandidate *ICECandidateInit `json:"ice,omitempty"`
}

CandidateMessage represents a candidate message. See https://github.com/OpenAyame/ayame-spec for details

type ClientID

type ClientID string

ClientID represents a client ID.

type ICECandidateInit

type ICECandidateInit struct {
	Candidate        string  `json:"candidate"`
	SDPMid           *string `json:"sdpMid"`
	SDPMLineIndex    *uint16 `json:"sdpMLineIndex"`
	UsernameFragment *string `json:"usernameFragment"`
}

ICECandidateInit is used to serialize ice candidates.

type ICEServer

type ICEServer struct {
	URLs       []string `json:"urls"`
	UserName   string   `json:"username,omitempty"`
	Credential string   `json:"credential,omitempty"`
}

ICEServer represents ICE server's information.

type Logger

type Logger interface {
	Debugf(template string, args ...interface{})
	Infof(template string, args ...interface{})
	Warnf(template string, args ...interface{})
	Errorf(template string, args ...interface{})
}

Logger is a logger interface

type MessageType

type MessageType string

MessageType represents a type of messages in ayame protocol. See https://github.com/OpenAyame/ayame-spec for details.

const (
	MessageTypeAccept    MessageType = "accept"
	MessageTypeAnswer    MessageType = "answer"
	MessageTypeBye       MessageType = "bye"
	MessageTypeCandidate MessageType = "candidate"
	MessageTypeOffer     MessageType = "offer"
	MessageTypePing      MessageType = "ping"
	MessageTypePong      MessageType = "pong"
	MessageTypeRegister  MessageType = "register"
	MessageTypeReject    MessageType = "reject"
)

type PingPongMessage

type PingPongMessage struct {
	Type MessageType `json:"type"`
}

PingPongMessage represents a ping-pong message. See https://github.com/OpenAyame/ayame-spec for details

type RegisterMessage

type RegisterMessage struct {
	Type          MessageType            `json:"type"`
	RoomID        RoomID                 `json:"roomId"`
	ClientID      ClientID               `json:"clientId"`
	AuthnMetadata map[string]interface{} `json:"authnMetadata,omitempty"`
}

RegisterMessage represents a register message. See https://github.com/OpenAyame/ayame-spec for details

type RejectMessage

type RejectMessage struct {
	Type   MessageType `json:"type"`
	Reason string      `json:"reason"`
}

RejectMessage represents a reject message. See https://github.com/OpenAyame/ayame-spec for details

type RoomID

type RoomID string

RoomID represents a room ID.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a server of ayu.

func NewServer

func NewServer(redisClient *redis.Client, opts ...ServerOption) *Server

NewServer creates a new ayu server.

func (*Server) ServeHTTP

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

ServeHTTP implements the http.Handler interface for a WebSocket.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown shuts down the server. When Shutdown is called, all connections (WebSocket and Redis) will be forcibly disconnected.

type ServerOption

type ServerOption interface {
	// contains filtered or unexported methods
}

ServerOption represents an option for Server.

func WithAuthenticator

func WithAuthenticator(authn Authenticator) ServerOption

WithAuthenticator specifies the custom authentication for registration.

func WithLogger

func WithLogger(logger Logger) ServerOption

WithLogger specifies the custom logger.

func WithPingInterval

func WithPingInterval(interval time.Duration) ServerOption

WithPingInterval specifies the custom ping interval.

func WithRoomExpiration

func WithRoomExpiration(expiration time.Duration) ServerOption

WithRoomExpiration specifies the custom room expiration in Redis.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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