ws

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

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

Go to latest
Published: Jan 6, 2024 License: MIT Imports: 12 Imported by: 0

README

Silly-Code WSgoproxy.cn

简单 WebSocket服务脚手架工具。 以会话ws.Session为核心,每一个WS连接作为一个会话,实现自定义ws.SessionHook即可维护会话的各个声明周期。 ###会话生命周期

  • BeforeAccept 建立连接前调用,即服务器端收到客户端连接请求时调用
  • Accept 创建连接后调用(ws.Session对象创建后)
  • OnMessage 收到客户端消息的回调
  • OnClose 关闭连接,可在此处清理会话信息

安装

go get -u github.com/irealing/silly-code/ws

示例


package main

import (
   "context"
   "errors"
   "github.com/irealing/silly-code/ws"
   "log"
   "net/http"
   "strings"
)

// implements ws.SessionHook
type hook struct {
}

// call before Accept
func (h hook) BeforeAccept(r *http.Request) error {
   if strings.HasPrefix(r.RemoteAddr, "127.0.0.1") {
   	// block connection from localhost
   	return errors.New("deny")
   }
   return nil
}

// call OnAccept
func (h hook) OnAccept(session *ws.Session, r *http.Request) error {
   return nil
}

// OnMessage
func (h hook) OnMessage(session *ws.Session, message ws.Message) error {
   log.Printf("receive message from session %d", session.ID())
   return nil
}

// OnClose
func (h hook) OnClose(session *ws.Session) error {
   log.Print("close session", session.ID())
   // clean session storage here
   return nil
}

func main() {
   wsServer := ws.NewServer(context.Background(), &hook{}, 3, 3, 60)
   http.HandleFunc("/wsServer", func(writer http.ResponseWriter, request *http.Request) {
   	if err := wsServer.Accept(writer, request); err != nil {
   		log.Print(err)
   	}
   })
   http.ListenAndServe(":8080", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ByteMessage

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

func (*ByteMessage) Bytes

func (rm *ByteMessage) Bytes() []byte

func (*ByteMessage) Type

func (rm *ByteMessage) Type() int

type Message

type Message interface {
	Type() int
	Bytes() []byte
}

func NewByteMessage

func NewByteMessage(t int, body []byte) Message

type Server

type Server interface {
	Accept(w http.ResponseWriter, r *http.Request) error
	Dispose()
	Broadcast(message Message) (int, int, error)
	GetSession(...uint64) []*Session
}

func NewServer

func NewServer(ctx context.Context, logger *slog.Logger, hook SessionHook, rwCache, retry, ttl int) Server

type Session

type Session struct {
	TTL time.Duration

	Retry int
	// contains filtered or unexported fields
}

func (*Session) Addr

func (s *Session) Addr() net.Addr

func (*Session) Close

func (s *Session) Close()

func (*Session) Closed

func (s *Session) Closed() bool

func (*Session) ID

func (s *Session) ID() uint64

func (*Session) Read

func (s *Session) Read() (Message, error)

func (*Session) ReadJSON

func (s *Session) ReadJSON(v interface{}) error

func (*Session) Recv

func (s *Session) Recv()

func (*Session) Run

func (s *Session) Run() error

func (*Session) Send

func (s *Session) Send(m Message) error

type SessionHook

type SessionHook interface {
	// BeforeAccept
	//
	// Call before accept a websocket connection, validate
	BeforeAccept(r *http.Request) error
	// OnAccept
	//
	// call after a websocket a websocket connection established
	OnAccept(session *Session, r *http.Request) error
	// OnMessage
	//
	// callback when receive message
	OnMessage(session *Session, message Message) error
	// OnClose
	//
	// clean session storage here
	OnClose(session *Session) error
}

type SessionManager

type SessionManager struct {
	TTL time.Duration
	// contains filtered or unexported fields
}

func (*SessionManager) Broadcast

func (manager *SessionManager) Broadcast(m Message) (success int, failed int, err error)

func (*SessionManager) Clients

func (manager *SessionManager) Clients() int

func (*SessionManager) Close

func (manager *SessionManager) Close()

func (*SessionManager) Closed

func (manager *SessionManager) Closed() bool

func (*SessionManager) Context

func (manager *SessionManager) Context() context.Context

func (*SessionManager) GetSession

func (manager *SessionManager) GetSession(id uint64) (*Session, bool)

func (*SessionManager) NewSession

func (manager *SessionManager) NewSession(conn *websocket.Conn) *Session

func (*SessionManager) Put

func (manager *SessionManager) Put(session *Session) error

func (*SessionManager) Remove

func (manager *SessionManager) Remove(id uint64)

Jump to

Keyboard shortcuts

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