gateway

package
v0.0.0-...-8ee38c3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2018 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gateway 长链接管理 用于推送服务

Index

Constants

View Source
const MaxDuration time.Duration = 1<<63 - 1

MaxDuration 无限等待 默认server会在指定的时间内没有读取到消息 关闭链接 设置为 MaxDuration 则不做读取检测

Variables

This section is empty.

Functions

func SplitCmd

func SplitCmd(content []byte) (string, []byte, error)

SplitCmd websocket常用的协议模式是`cmdType cmdBody` 提供Split来分割协议

Types

type AuthHandler

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

AuthHandler 验证

func NewAuthHandler

func NewAuthHandler(provider AuthProvider, manager *HubManager) *AuthHandler

NewAuthHandler 返回1个AuthHandler

func (*AuthHandler) Offline

func (handler *AuthHandler) Offline(ctx *gnet.ChannelHandlerContext)

Offline 客户端断开链接 一定再调用Close之后触发

func (*AuthHandler) Online

func (handler *AuthHandler) Online(ctx *gnet.ChannelHandlerContext)

Online 在线

func (*AuthHandler) Read

func (handler *AuthHandler) Read(ctx *gnet.ChannelHandlerContext, last interface{})

func (*AuthHandler) Write

func (handler *AuthHandler) Write(ctx *gnet.ChannelHandlerContext, last interface{})

write前依次调用

type AuthProvider

type AuthProvider interface {
	//是否是验证消息
	IsAuthRequest(ctx *gnet.ChannelHandlerContext, msg interface{}) bool
	//int64为UID 用于区别不同用户的链接
	//interface{} rsp 不为nil的话则发送给客户端
	//error不为nil 则链接直接关闭
	//登录如果成功的话 handler会设置 "sessionID", "uid" 域
	Authing(ctx *gnet.ChannelHandlerContext, msg interface{}) (int64, interface{}, error) //验证通过后返回回应
}

AuthProvider 验证器

type Gateway

type Gateway struct {
	Bootstrap *gnet.Bootstrap
	Manager   *HubManager
}

Gateway 服务器管理链接

func New

func New(server *gnet.Bootstrap, hm *HubManager) *Gateway

New 返回1个服务

func NewWSGateway

func NewWSGateway(
	address, path, certFile, keyFile string,
	config *gnet.BootstrapConfig,
	protocolProvider WSProtocolProvider,
	authProvider AuthProvider,
	msgChan chan<- PubMessage,
	node int64, epoch int64,
	keepAliveTimeout time.Duration) *Gateway

NewWSGateway 返回1个websocket协议的Gateway

type HubManager

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

HubManager 管理链接

func NewHubManager

func NewHubManager(idGenerator IDGenerator, msgChan chan<- PubMessage) *HubManager

NewHubManager 返回1个新的HubManager

func (*HubManager) Add

func (hm *HubManager) Add(uid int64, ctx *gnet.ChannelHandlerContext) (int64, error)

Add 添加一个Session 返回SessionID

func (*HubManager) RemoveByID

func (hm *HubManager) RemoveByID(id int64, directly bool) *Session

RemoveByID 通过sessionID 移除一个Session remove 可能是后端服务主动发起的 也有可能是客户端发起的 后端服务主动发起的 directly为true 避免发起一个消息

func (*HubManager) RouteByID

func (hm *HubManager) RouteByID(id int64, msg interface{}) error

RouteByID 通过SessionID来转发消息

type IDGenerator

type IDGenerator interface {
	Gen() int64
}

IDGenerator 用于生成SessionID 可以使用snowflake

type PubMessage

type PubMessage struct {
	SessionID int64 //Session.ID
	Type      PubType
	Data      interface{}
}

PubMessage 接受到的消息

type PubType

type PubType int

PubType 消息类型

const (
	ENTER PubType = iota + 1 //进入
	ROUTE
	LEAVE //离开
)

消息类型

type Session

type Session struct {
	ID  int64 //sessionID 使用snowflake保证唯一
	UID int64 //用户ID 区别不同用户 相同用户的登录会关闭之前登录的链接
	CTX *gnet.ChannelHandlerContext
}

Session 代表本次登录的用户

type WSProtocolHandler

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

WSProtocolHandler 解析协议

func NewWSProtocolHandler

func NewWSProtocolHandler(provider WSProtocolProvider) *WSProtocolHandler

NewWSProtocolHandler 返回handler 内部无状态可用在多个conn中

func (*WSProtocolHandler) Offline

func (handler *WSProtocolHandler) Offline(ctx *gnet.ChannelHandlerContext)

Offline 客户端断开链接 一定再调用Close之后触发

func (*WSProtocolHandler) Online

func (handler *WSProtocolHandler) Online(ctx *gnet.ChannelHandlerContext)

Online 在线

func (*WSProtocolHandler) Read

func (handler *WSProtocolHandler) Read(ctx *gnet.ChannelHandlerContext, last interface{})

func (*WSProtocolHandler) Write

func (handler *WSProtocolHandler) Write(ctx *gnet.ChannelHandlerContext, last interface{})

write前依次调用

type WSProtocolProvider

type WSProtocolProvider interface {
	//@wsType websocket消息类型 文本 二进制之类
	Decode(wsType int, content []byte) (interface{}, error)
	//int wsType
	Encode(interface{}) (int, []byte, error)
}

WSProtocolProvider 协议提供者

Jump to

Keyboard shortcuts

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