gows

package module
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: May 14, 2022 License: MIT Imports: 7 Imported by: 0

README

goWs

基于 gorilla 封装的并发安全的 Golang Websocket 组件

快速开始

import (
   "fmt"
   ws "github.com/lcr2000/goWs"
   "net/http"
)

func WsHandler(w http.ResponseWriter, r *http.Request) {
   // 新建连接实例
   conn := ws.NewConnection()
   // 开启连接
   if err := conn.Open(w, r); err != nil {
   	return
   }
   // 关闭连接
   defer conn.Close()
   for {
   	// 读取消息
   	msg, err := conn.Receive()
   	if err != nil {
   		break
   	}
   	fmt.Println(string(msg.Data))
   	// 发送消息
   	err = conn.Write(&ws.Message{
   		MessageType: msg.MessageType,
   		Data:        msg.Data,
   	})
   	if err != nil {
   		break
   	}
   }
}

Documentation

Index

Constants

View Source
const (
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	TextMessage = 1

	// BinaryMessage denotes a binary data message.
	BinaryMessage = 2

	// CloseMessage denotes a close control message. The optional message
	// payload contains a numeric code and text. Use the FormatCloseMessage
	// function to format a close message payload.
	CloseMessage = 8

	// PingMessage denotes a ping control message. The optional message payload
	// is UTF-8 encoded text.
	PingMessage = 9

	// PongMessage denotes a pong control message. The optional message payload
	// is UTF-8 encoded text.
	PongMessage = 10
)

The message types are defined in RFC 6455, section 11.8.

View Source
const (
	// DefaultInChanSize 默认读队列大小
	DefaultInChanSize = 1024

	// DefaultOutChanSize 默认写队列大小
	DefaultOutChanSize = 1024

	// DefaultHeartbeatInterval 默认心跳检测间隔
	DefaultHeartbeatInterval = 300
)

Variables

View Source
var (
	// ErrConnClose 连接已关闭
	ErrConnClose = errors.New("connection already closed")
)

Functions

This section is empty.

Types

type Conn

type Conn interface {
	// Close 关闭连接
	Close() error
	// Open 开启连接
	Open(w http.ResponseWriter, r *http.Request) error
	// Receive 接收数据
	Receive() (msg *Message, err error)
	// Write 写入数据
	Write(msg *Message) (err error)
}

Conn 长连接接口

type Connection added in v0.1.1

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

Connection 维护的长连接.

func NewConnection added in v0.1.1

func NewConnection(opts ...*Options) *Connection

NewConnection 新建 Connection实例.

func (*Connection) Close added in v0.1.1

func (c *Connection) Close() error

Close 关闭连接

func (*Connection) GetConnID added in v0.1.1

func (c *Connection) GetConnID() string

GetConnID 获取连接ID

func (*Connection) GetRemoteAddr added in v0.1.9

func (c *Connection) GetRemoteAddr() net.Addr

GetRemoteAddr 获取远程地址

func (*Connection) KeepHeartbeat added in v0.1.9

func (c *Connection) KeepHeartbeat()

KeepHeartbeat 保持心跳

func (*Connection) Open added in v0.1.1

Open 开启连接

func (*Connection) Receive added in v0.1.1

func (c *Connection) Receive() (msg *Message, err error)

Receive 接收数据

func (*Connection) Write added in v0.1.1

func (c *Connection) Write(msg *Message) (err error)

Write 写入数据

type Message added in v0.1.1

type Message struct {
	// MessageType The message types are defined in RFC 6455, section 11.8.
	MessageType int
	// Data 消息内容
	Data []byte
}

Message 定义了一个消息实体.

type Options added in v0.1.9

type Options struct {
	// InChanSize 读队列大小, 默认1024
	InChanSize int
	// OutChanSize 写队列大小, 默认1024
	OutChanSize int
	// HeartbeatInterval 心跳检测间隔, 当心跳间隔大于这个时间连接将断开, 默认300s
	HeartbeatInterval int
}

Options 可选参数

Jump to

Keyboard shortcuts

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