websockets

package
v3.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2016 License: AGPL-3.0, AGPL-3.0-or-later Imports: 20 Imported by: 0

Documentation

Overview

Package websockets manages active websocket connections and messages received from and sent to them

Index

Constants

This section is empty.

Variables

View Source
var Clients = ClientMap{
	// contains filtered or unexported fields
}

Clients stores all synchronized websocket clients in a thread-safe map

Functions

func ConstructThread

func ConstructThread(req ThreadCreationRequest, ip string, parseBody bool) (
	id uint64, timeStamp int64, hasImage bool, err error,
)

ConstructThread creates a new tread and writes it to the database. Returns the ID of the thread and its creation timestamp

func EncodeMessage

func EncodeMessage(typ MessageType, msg interface{}) ([]byte, error)

EncodeMessage encodes a message for sending through websockets or writing to the replication log.

func Handler

func Handler(res http.ResponseWriter, req *http.Request)

Handler is an http.HandleFunc that responds to new websocket connection requests.

func Listen

func Listen() error

Listen initializes and starts listening for post updates from RethinkDB

func UpdatePost

func UpdatePost(id uint64, key string, val interface{}, msg []byte) error

UpdatePost post updates a single field of an existing post with the appropriate replication log update and timestamp modification.

Types

type Client

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

Client stores and manages a websocket-connected remote client and its interaction with the server and database

func (*Client) Close

func (c *Client) Close(err error)

Close closes a websocket connection with the provided status code and optional reason

func (*Client) Send

func (c *Client) Send(msg []byte)

Send a message to the client. Can be used concurrently.

type ClientMap

type ClientMap struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

ClientMap is a thread-safe store for all clients connected to this server instance

func (*ClientMap) Clear

func (c *ClientMap) Clear()

Clear removes all clients from the map

func (*ClientMap) CountByIP

func (c *ClientMap) CountByIP() int

CountByIP returns the number of unique IPs synchronized with the server

func (*ClientMap) GetSync

func (c *ClientMap) GetSync(cl *Client) (bool, SyncID)

GetSync returns if the current client is synced and the thread and board it is synced to.

type ImageRequest

type ImageRequest struct {
	Spoiler     bool
	Token, Name string
}

ImageRequest contains data for allocating an image

type MessageType

type MessageType uint8

MessageType is the identifier code for websocket message types

const (
	MessageInvalid MessageType = iota
	MessageInsertThread
	MessageInsertPost
	MessageAppend
	MessageBackspace
	MessageSplice
	MessageClosePost
	MessageLink
	MessageBacklink
	MessageCommand
	MessageInsertImage
	MessageSpoiler
)

1 - 29 modify post model state

const (
	MessageSynchronise MessageType = 30 + iota
	MessageReclaim

	// Send new post ID to client
	MessagePostID

	// Concatenation of multiple websocket messages to reduce transport overhead
	MessageConcat

	// Message from the client meant to invoke no operation. Mostly used as a
	// one way ping, because the JS Websocket API does not provide access to
	// pinging.
	MessageNOOP
)

>= 30 are miscellaneous and do not write to post models

type ReplyCreationRequest

type ReplyCreationRequest struct {
	Image                      ImageRequest
	Name, Auth, Password, Body string
}

ReplyCreationRequest contains common fields for both thread and reply creation

type SyncID

type SyncID struct {
	OP    uint64
	Board string
}

SyncID contains the board and thread the client are currently synced to. If the client is on the board page, thread = 0.

type ThreadCreationRequest

type ThreadCreationRequest struct {
	ReplyCreationRequest
	Subject, Board string
	common.Captcha
}

ThreadCreationRequest contains data for creating a new thread

Jump to

Keyboard shortcuts

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