websocket

package
v1.4.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2020 License: MIT Imports: 5 Imported by: 0

README

websocket

使用

import (
  "net/http"
  "github.com/go-eyas/toolkit/websocket"
)
func main() {
  ws := websocket.New(&Config{
    MsgType: websocket.TextMessage, // 消息类型 websocket.TextMessage | websocke.BinaryMessage
  })

  http.HandleFunc("/ws", ws.HTTPHandler)

  go func() {
    rec := ws.Receive()
    for {
      req, _ := <-rec
      req.Response([]byte("1234556"))
    }
  }()

  http.ListenAndServe("127.0.0.1:8800", nil)
}

服务

已经准备了一个开箱即用的服务,该服务按照特定协议工作,详情请查看

示例概览

import (
  "net/http"
  "github.com/go-eyas/toolkit/websocket"
  "github.com/go-eyas/toolkit/websocket/wsrv"
)
func main() {
  server := wsrv.New(&Config{
    MsgType: websocket.TextMessage, // 消息类型 websocket.TextMessage | websocke.BinaryMessage
  })
  server.Use(func(c *wsrv.Context) {
    if c.CMD != "register" {
      _, ok := c.Get("uid").(int)
      if !ok {
        c.Abort()
      }
    }
  })

  server.Handle("register", func(c *wsrv.Context) {
    c.Set("uid", 1001)
    c.OK()
  })
  server,Handle("userinfo", func(c *wsrv.Context) {
    uid := c.Get("uid").(int)
    c.OK(GetUserInfoByID(uid))
  })

  http.HandleFunc("/ws", server.Engine.HTTPHandler)
  http.ListenAndServe("127.0.0.1:8800", nil)
}

协议

godoc

API 文档

Documentation

Index

Constants

View Source
const (
	// 文本消息
	TextMessage = 1

	// 二进制数据消息
	BinaryMessage = 2
)

Variables

View Source
var EmptyLogger = &l{}

Functions

This section is empty.

Types

type Config

type Config struct {
	MsgType         int                      // 消息类型 TextMessage | BinaryMessage
	ReadBufferSize  int                      // 读取缓存大小
	WriteBufferSize int                      // 写入缓存大小
	CheckOrigin     func(*http.Request) bool // 检查跨域来源是否允许建立连接
	Logger          LoggerI                  // 用于打印内部产生日志
}

Config 配置项

type Conn

type Conn struct {
	Socket *websocket.Conn // 连接

	ID uint64
	// contains filtered or unexported fields
}

Conn 连接实例

func (*Conn) Destroy

func (c *Conn) Destroy() error

Destroy 销毁该连接

func (*Conn) Init

func (c *Conn) Init()

Init 初始化该连接

func (*Conn) Send

func (c *Conn) Send(msg *Message) error

Send 往该连接发送数据

type EventHandle added in v1.2.16

type EventHandle func(*Conn)

type LoggerI added in v1.2.16

type LoggerI interface {
	Info(...interface{})
	Infof(string, ...interface{})
	Error(...interface{})
	Errorf(string, ...interface{})
}

type Message

type Message struct {
	SID     uint64
	Payload []byte

	Socket  *Conn
	MsgType int
	// contains filtered or unexported fields
}

Message ws 接收到的消息

func (*Message) Response

func (m *Message) Response(v []byte) error

Response 在发送本消息的当前连接发送数据

type WS

type WS struct {
	Clients  map[uint64]*Conn
	Upgrader *websocket.Upgrader

	MsgType int
	// contains filtered or unexported fields
}

WS ws 连接

func New

func New(conf *Config) *WS

New 新建 websocket 服务

func (*WS) Connect added in v1.3.1

func (ws *WS) Connect(w http.ResponseWriter, r *http.Request) (*Conn, error)

从 http 连接获取连接实例

func (*WS) DestroyConn added in v1.3.2

func (ws *WS) DestroyConn(cid uint64)

DestroyConn 销毁连接

func (*WS) HTTPHandler

func (ws *WS) HTTPHandler(w http.ResponseWriter, r *http.Request)

HTTPHandler 给 http 控制器绑定使用

func (*WS) HandleClose added in v1.2.16

func (ws *WS) HandleClose(fn EventHandle)

func (*WS) HandleCreate added in v1.2.16

func (ws *WS) HandleCreate(fn EventHandle)

func (*WS) Playground

func (ws *WS) Playground(w http.ResponseWriter, r *http.Request)

func (*WS) Receive

func (ws *WS) Receive() <-chan *Message

Receive 获取接收数据的 chan

func (*WS) Send

func (ws *WS) Send(msg *Message) error

Send 发送数据

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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