websocket

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

websocket go

features

  • support multi namespace
  • support room join/leave

Documentation

Overview

Package websocket provides an easy way to setup a rich Websocket server and client side.

Index

Constants

View Source
const (
	// DefaultWebsocketWriteTimeout 15 * time.Second
	DefaultWebsocketWriteTimeout = 15 * time.Second
	// DefaultWebsocketPongTimeout 60 * time.Second
	DefaultWebsocketPongTimeout = 60 * time.Second
	// DefaultWebsocketPingPeriod (DefaultPongTimeout * 9) / 10
	DefaultWebsocketPingPeriod = (DefaultWebsocketPongTimeout * 9) / 10
	// DefaultWebsocketMaxMessageSize 1024
	DefaultWebsocketMaxMessageSize = 1024
	// DefaultWebsocketReadBufferSize 4096
	DefaultWebsocketReadBufferSize = 4096
	// DefaultWebsocketWriterBufferSize 4096
	DefaultWebsocketWriterBufferSize = 4096
)
View Source
const (
	// All is the string which the Emmiter use to send a message to all
	All = ""
)
View Source
const (
	// Version current version number
	Version = "0.0.1"
)

Variables

This section is empty.

Functions

func Random

func Random(n int) []byte

Random takes a parameter (int) and returns random slice of byte ex: var randomstrbytes []byte; randomstrbytes = utils.Random(32)

func RandomString

func RandomString(n int) string

Types

type Config

type Config struct {
	Error       func(res http.ResponseWriter, req *http.Request, status int, reason error)
	CheckOrigin func(req *http.Request) bool

	// we may want to custom the id generate from the request
	CustomIDFunc ConnectionIDFunc

	// WriteTimeout time allowed to write a message to the connection.
	// Default value is 15 * time.Second
	WriteTimeout time.Duration
	// PongTimeout allowed to read the next pong message from the connection
	// Default value is 60 * time.Second
	PongTimeout time.Duration
	// PingPeriod send ping messages to the connection with this period. Must be less than PongTimeout
	// Default value is (PongTimeout * 9) / 10
	PingPeriod time.Duration
	// MaxMessageSize max message size allowed from connection
	// Default value is 1024
	MaxMessageSize int64
	// BinaryMessages set it to true in order to denotes binary data messages instead of utf-8 text
	// compatible if you wanna use the Connection's EmitMessage to send a custom binary data to the client, like a native server-client communication.
	// defaults to false
	BinaryMessages bool
	// ReadBufferSize is the buffer size for the underline reader
	ReadBufferSize int
	// WriteBufferSize is the buffer size for the underline writer
	WriteBufferSize int
}

Config the websocket server configuration

type Connection

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

func (*Connection) Disconnect

func (c *Connection) Disconnect() error

func (*Connection) Emit

func (c *Connection) Emit(event string, data interface{}) error

func (*Connection) EmitError

func (c *Connection) EmitError(errorMessage string)

func (*Connection) EmitMessage

func (c *Connection) EmitMessage(nativeMessage []byte) error

func (*Connection) Get

func (c *Connection) Get(key string) string

func (*Connection) ID

func (c *Connection) ID() string

func (*Connection) Join

func (c *Connection) Join(roomName string)

func (*Connection) Leave

func (c *Connection) Leave(roomName string)

func (*Connection) List

func (c *Connection) List(room string) []string

func (*Connection) On

func (c *Connection) On(event string, cb MessageFunc)

func (*Connection) OnDisconnect

func (c *Connection) OnDisconnect(cb DisconnectFunc)

func (*Connection) OnError

func (c *Connection) OnError(cb ErrorFunc)

func (*Connection) OnMessage

func (c *Connection) OnMessage(cb NativeMessageFunc)

func (*Connection) Request

func (c *Connection) Request() *http.Request

func (*Connection) Set

func (c *Connection) Set(key string, value string)

func (*Connection) To

func (c *Connection) To(to string) Emmiter

type ConnectionFunc

type ConnectionFunc func(*Connection)

ConnectionFunc is the callback which fires when a client/connection is connected to the server. Receives one parameter which is the Connection

type ConnectionIDFunc

type ConnectionIDFunc func(*http.Request) string

type DisconnectFunc

type DisconnectFunc func()

DisconnectFunc is the callback which fires when a client/connection closed

type Emmiter

type Emmiter interface {
	// EmitMessage sends a native websocket message
	EmitMessage([]byte) error
	// Emit sends a message on a particular event
	Emit(string, interface{}) error
}

Emmiter is the message/or/event manager

type ErrorFunc

type ErrorFunc (func(string))

ErrorFunc is the callback which fires when an error happens

type MessageFunc

type MessageFunc interface{}

MessageFunc is the second argument to the Emmiter's Emit functions. A callback which should receives one parameter of type string, int, bool or any valid JSON/Go struct

type NameSpace

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

func (*NameSpace) List

func (n *NameSpace) List(room string) []string

func (*NameSpace) To

func (n *NameSpace) To(to string) Emmiter

type NativeMessageFunc

type NativeMessageFunc func([]byte)

NativeMessageFunc is the callback for native websocket messages, receives one []byte parameter which is the raw client's message

type Rooms

type Rooms map[string][]string

Rooms is just a map with key a string and value slice of string

type Server

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

func New

func New(cfg ...Config) *Server

New creates a websocket server and returns it

func (*Server) Connection

func (s *Server) Connection(cid string) *Connection

func (*Server) Connections

func (s *Server) Connections() []string

func (*Server) Handler

func (s *Server) Handler() http.Handler

func (*Server) Of

func (s *Server) Of(namespaceName string) *NameSpace

func (*Server) OnConnection

func (s *Server) OnConnection(cb ConnectionFunc)

OnConnection this is the main event you, as developer, will work with each of the websocket connections

func (*Server) Serve

func (s *Server) Serve()

Serve starts the websocket server

func (*Server) Stop

func (s *Server) Stop()

func (*Server) ToAll

func (s *Server) ToAll() Emmiter

type UsersRooms

type UsersRooms map[string][]string

UsersRooms is a user in rooms

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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