Skip to content

server

import "github.com/alimasry/go-collab-editor/server"

Index

Constants

Message types exchanged over WebSocket.

const (
    MsgJoin  = "join"
    MsgLeave = "leave"
    MsgOp    = "op"
    MsgAck   = "ack"
    MsgDoc   = "doc"
    MsgError = "error"
)

func NewHandler

func NewHandler(hub *Hub) http.Handler

NewHandler creates the HTTP handler with all routes.

type Client

Client represents a single WebSocket connection.

type Client struct {
    ID    string
    Name  string
    Color string
    // contains filtered or unexported fields
}

func (*Client) Info

func (c *Client) Info() ClientInfo

func (*Client) ReadPump

func (c *Client) ReadPump()

ReadPump reads messages from the WebSocket and routes them.

func (*Client) WritePump

func (c *Client) WritePump()

WritePump writes messages from the send channel to the WebSocket.

type ClientInfo

ClientInfo describes a connected user.

type ClientInfo struct {
    ID    string `json:"id"`
    Name  string `json:"name"`
    Color string `json:"color"`
}

type ClientMessage

ClientMessage is a message from client to server.

type ClientMessage struct {
    Type     string       `json:"type"`
    DocID    string       `json:"docId,omitempty"`
    Revision int          `json:"revision"`
    Op       ot.Operation `json:"op,omitempty"`
}

type Hub

Hub manages document sessions and routes clients to the right session.

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

func NewHub

func NewHub(st store.DocumentStore, engine ot.Engine) *Hub

func (*Hub) GetSession

func (h *Hub) GetSession(docID string) *Session

GetSession returns the session for a document, if active.

func (*Hub) Run

func (h *Hub) Run()

Run is the hub's main loop.

type ServerMessage

ServerMessage is a message from server to client.

type ServerMessage struct {
    Type     string       `json:"type"`
    DocID    string       `json:"docId,omitempty"`
    Content  string       `json:"content"`
    Revision int          `json:"revision"`
    Op       ot.Operation `json:"op,omitempty"`
    ClientID string       `json:"clientId,omitempty"`
    Name     string       `json:"name,omitempty"`
    Color    string       `json:"color,omitempty"`
    Message  string       `json:"message,omitempty"`
    Clients  []ClientInfo `json:"clients,omitempty"`
}

func (ServerMessage) Encode

func (m ServerMessage) Encode() []byte

Encode serializes a ServerMessage to JSON bytes.

type Session

Session manages collaboration for a single document. All operations are serialized through a single goroutine.

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

func (*Session) Run

func (s *Session) Run()

Run is the session's main loop. It serializes all operations.

Generated by gomarkdoc