wserver

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2018 License: MIT Imports: 10 Imported by: 9

README

wserver

Build Status GoDoc Go Report Card

Tool for setup server with WebSocket

  • basic feature ok
  • add more tests(not stable) --- in process

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrRequestIllegal = errors.New("request data illegal")

ErrRequestIllegal describes error when data of the request is unaccepted.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Conn *websocket.Conn

	AfterReadFunc   func(messageType int, r io.Reader)
	BeforeCloseFunc func()
	// contains filtered or unexported fields
}

Conn wraps websocket.Conn with Conn. It defines to listen and read data from Conn.

func NewConn

func NewConn(conn *websocket.Conn) *Conn

NewConn wraps conn.

func (*Conn) GetID

func (c *Conn) GetID() string

GetID returns the id generated using UUID algorithm.

func (*Conn) Listen

func (c *Conn) Listen()

Listen listens for receive data from websocket connection. It blocks until websocket connection is closed.

func (*Conn) Write

func (c *Conn) Write(p []byte) (n int, err error)

Write write p to the websocket connection. The error returned will always be nil if success.

type RegisterMessage

type RegisterMessage struct {
	Token string
	Event string
}

RegisterMessage defines message struct client send after connection to the server.

type Server

type Server struct {
	// Address for server to listen on
	Addr string

	// Path for websocket request, default "/ws".
	WSPath string

	// Path for send message, default "/send".
	SendPath string

	// Upgrader is for upgrade connection to websocket connection using
	// "github.com/gorilla/websocket".
	//
	// If Upgrader is nil, default upgrader will be used. Default upgrader is
	// set ReadBufferSize and WriteBufferSize to 1024, and CheckOrigin always
	// returns true.
	Upgrader *websocket.Upgrader

	// Check token if it's valid and return userID. If token is invalid, err
	// should not be nil.
	AuthToken func(token string) (userID string, err error)

	// Authorize send request. Message will be sent if it returns true,
	// otherwise the request will be discarded. Default nil and send request
	// will always be accepted.
	SendAuth func(r *http.Request) bool
	// contains filtered or unexported fields
}

Server defines parameters for running websocket server.

func NewServer

func NewServer(addr string) *Server

NewServer creates a new Server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe listens on the TCP network address and handle websocket request.

func (*Server) Push

func (s *Server) Push(userID, event, message string) (int, error)

Push filters connections by userID and event, then write message

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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