nsocket

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

README

NSocket

Messaging framework with channels [rooms]

Inspired by Socket.io and Kataras Neffos

Installation
go get github.com/epikoder/nsocket
Usage
events := nsocket.Event{
    "/": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
     fmt.Printf("Namespace: [Default] -- GOT: %v ---- from ----  %v\n", i, s.RemoteAddr())
     if err := soc.Broadcast("namespace:default -- " + fmt.Sprintf("%v ------> %v", i, s.RemoteAddr())); err != nil {
      t.Error(err)
     }
    },
    "message": func(s *melody.Session, i interface{}, soc *nsocket.NSocket) {
     fmt.Printf("Namespace: [Default/Message] -- GOT: %v ---- from ----  %v\n", i, s.RemoteAddr())
     if err := soc.Emit("namespace:message -- "+fmt.Sprintf("%v ------> %v", i, s.RemoteAddr()), "message"); err != nil {
      t.Error(err)
     }
    },
   }
    
soc := nsocket.New(nsocket.Config{
  AuthFunc: func(r *http.Request) (ok bool) { // Optional: Add authentication : On https Websocket send cookie to the server
   c, err := r.Cookie("auth")
   if err != nil {
    return
   }
   return c.Value == authKey
  },
  AllowedOrigins: []string{"localhost:3000", "localhost:8000"}, //Optional: Set allowed origins if needed
  Namespace: nsocket.Namespace{
   nsocket.Default: events // Events functions are called when message is received from the client
  },
 })

 mux := http.NewServeMux()
 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
  if err := soc.Serve(w, r); err != nil {
   panic(err)
  }
 })

 fmt.Println("starting server on: http://localhost:8000")
 http.ListenAndServe(":8000", mux) // Start websocket server
Note

This library is still under development and not production ready, use with caution.

Documentation

Index

Constants

View Source
const (
	Default string = "default"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AckMessage

type AckMessage struct {
	Id     uuid.UUID `json:"id"`
	Type   string    `json:"type"`
	Action string    `json:"action"`
	Reason string    `json:"reason,omitempty"`
}

type Config

type Config struct {
	AllowedOrigins []string
	AuthFunc       func(r *http.Request) bool
	Namespace      Namespace
}
var (
	DefaultNSocketConfig Config = Config{}
)

type Event

type Event map[string]RecFunc

type Message

type Message struct {
	Id        uuid.UUID     `json:"id"`
	Type      MessageType   `json:"type"`
	Body      interface{}   `json:"body"`
	Action    MessageAction `json:"action,omitempty"`
	Namespace string        `json:"namespace,omitempty"`
}

type MessageAction

type MessageAction string
const (
	Nil         MessageAction = ""
	Subscribe   MessageAction = "subscribe"
	UnSubscribe MessageAction = "unsubscribe"

	OnNamespaceConnected  = "OnNamespaceConnected"
	OnNamespaceDisconnect = "OnNamespaceDisconnect"
)

type MessageType

type MessageType string

type NSocket

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

func New

func New(config Config) *NSocket

func (*NSocket) Broadcast

func (nsoc *NSocket) Broadcast(v interface{}, s *melody.Session) (err error)

func (*NSocket) BroadcastAll added in v1.0.6

func (nsoc *NSocket) BroadcastAll(v interface{}) (err error)

func (*NSocket) Emit

func (nsoc *NSocket) Emit(v interface{}, i ...interface{}) (err error)

func (*NSocket) EmitAll added in v1.0.6

func (nsoc *NSocket) EmitAll(v interface{}, namespace string) (err error)

func (*NSocket) Namespaces

func (nsoc *NSocket) Namespaces() map[string][]*melody.Session

func (*NSocket) Serve

func (nsoc *NSocket) Serve(w http.ResponseWriter, r *http.Request) (err error)

type Namespace

type Namespace map[string]Event

type RecFunc

type RecFunc func(*melody.Session, interface{}, *NSocket)

type SendFunc

type SendFunc func(interface{}, string) error

Jump to

Keyboard shortcuts

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