wsserver

package module
v0.0.0-...-3396164 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2021 License: MIT Imports: 12 Imported by: 0

README

wsserver

Library for work with websockets.

Wrapper for gobwas/ws

Usage

package main

import (
	"ws"
	"github.com/rosberry/go-wsserver"
	"github.com/rosberry/go-wsserver/debugger"
)

var DebugMode bool

func main() {
	handlers := ws.GetHandlers()
	//start debug
	if _, err := debugger.New(&debugger.Config{
		Addr:     ":6007",
		Handlers: &handlers,
	}); err != nil {

		log.Println("Failed start debugger")
	}

	//start
	if _, err := wsserver.Start(&wsserver.Config{
			Addr:     ":6006",
			Handlers: handlers,
		}); err != nil {

		panic(err)
	}
}
package ws

type MyHandler struct{
	cc	wsserver.ConnController
}

func GetGetHandlers() *MyHandler {
	return &MyHandler{}
}

func (h *MyHandler) OnAuth(token string) (id uint, ok bool) {
	//do anything
	//get id by token
	id = a.GetIdByToken(token)
	return id, true
}

func (h *MyHandler) OnOnline(id uint) {
	//do anything
	log.Println("%b ONLINE", id)
}

func (h *MyHandler) OnOffline(id uint) {
	//do anything
	log.Println("%b OFFLINE", id)
}

func (h *MyHandler) OnText(id uint, msg []byte) {
	//do anything
	log.Printf("[Message IN](%v) %s\n", id, string(msg))
}

func (h *MyHandler) OnSend(id uint, msg []byte) (ok bool){
	fmt.Printf("[Message OUT](%v) %s\n", id, string(msg))
	return true
}

func (h *MyHandler) SetConnCtrlr(cc wsserver.ConnController) {
	h.cc = cc
}

About

This project is owned and maintained by Rosberry. We build mobile apps for users worldwide 🌏.

Check out our open source projects, read our blog or give us a high-five on 🐦 @rosberryapps.

License

This project is available under the MIT license. See the LICENSE file for more info.

Documentation

Index

Constants

View Source
const (
	TimeoutPing  = 30 * time.Second
	TimeoutClose = 15 * time.Second
)
View Source
const (
	LoggerDefaultPrefix = "[WS]"
	AuthTokenKey        = "token"
)

Variables

View Source
var (
	ErrEmptyConfig   = errors.New("Empty config")
	ErrBadAuthHeader = errors.New("Bad Authorization header")
	ErrAuthFailed    = errors.New("Bad token")
	ErrNotAuth       = errors.New("Token not found")
	ErrConnNotFound  = errors.New("Connection not found")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Addr     string
	Handlers Handlers
	Logger   Logger
}

type ConnController

type ConnController interface {
	WriteMessage(id uint, msg []byte) (err error)
	CloseConnection(id uint) (err error)
}

type Handlers

type Handlers interface {
	SetConnCtrlr(ctrlr ConnController)
	OnAuth(token string) (id uint, ok bool)
	OnOnline(id uint)
	OnText(id uint, msg []byte)
	OnSend(id uint, msg []byte) (ok bool)
	OnOffline(id uint)
}

type Logger

type Logger interface {
	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})
	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})
	Print(v ...interface{})
	Printf(format string, v ...interface{})
	Println(v ...interface{})
}

type Message

type Message struct {
	Body []byte
	Op   ws.OpCode
	Err  error
}

type WS

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

func Start

func Start(cfg *Config) (*WS, error)

func (*WS) CloseConnection

func (w *WS) CloseConnection(id uint) error

func (*WS) WriteMessage

func (w *WS) WriteMessage(id uint, msg []byte) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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