socket

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2018 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ConstHeader       = "FireHeader"
	ConstHeaderLength = 10
	ConstIntLength    = 4 // int转byte长度为4
	ConstSplitSpace   = " "
	ConstNewLine      = '\n'
)

协议中用到的常量

View Source
const (
	// PublishKey 与前端(客户端约定的推送关键字)
	PublishKey = "publish"
	// OfflineTopicByUserIdKey 踢除,将用户某个topic踢下线
	OfflineTopicByUserIdKey = "offline_topic_by_userid"
	// OfflineTopicKey 针对某个topic进行踢除
	OfflineTopicKey = "offline_topic"
	// OfflineUserKey 将某个用户踢下线
	OfflineUserKey = "offline_user"
)

Variables

View Source
var (
	// ErrorClose 连接关闭的错误信息
	ErrorClose = errors.New("firetower is collapsed")
	// ErrorBlock block错误信息
	ErrorBlock = errors.New("network congestion")
)
View Source
var (
	// LogLevel 日志打印的级别
	LogLevel = "INFO"
	// DefaultWriter 正常日志写入的地方
	DefaultWriter io.Writer = os.Stdout
	// DefaultErrorWriter 错误日志写入的地方
	DefaultErrorWriter io.Writer = os.Stderr
	// SendLogger 消息相关的日志处理方法
	SendLogger func(s *SendMessage, types, info string)

	// Green 绿色
	Green = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
	// White 白色
	White = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
	// Yellow 黄色
	Yellow = string([]byte{27, 91, 57, 55, 59, 52, 51, 109})
	// Red 红色
	Red = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
	// Blue 蓝色
	Blue = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
	// Magenta 品红
	Magenta = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
	// Cyan 青色
	Cyan = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
	// Reset 重置日志颜色
	Reset = string([]byte{27, 91, 48, 109})
)

Functions

func BytesToInt

func BytesToInt(b []byte) int

BytesToInt 字节转换成整形

func Depack

func Depack(buffer []byte, readerChannel chan *SendMessage) ([]byte, error)

Depack 解包

func Enpack

func Enpack(pushType, messageId, source, topic string, content []byte) ([]byte, error)

Enpack 封包

func IntToBytes

func IntToBytes(n int) []byte

IntToBytes 整形转换成字节

Types

type PushMessage

type PushMessage struct {
	MessageId string `json:"message_id"`
	Source    string `json:"source"`
	Topic     string `json:"topic"`
	Data      []byte `json:"data"`
	Type      string `json:"type"`
}

PushMessage 推送消息结构体

type SendMessage

type SendMessage struct {
	Context     *sendLife
	Type        string `json:"type"`
	MessageType int
	Data        json.RawMessage `json:"data"`
	Topic       string
}

SendMessage 发送的消息结构体 发送不用限制用户消息内容的格式

func GetSendMessage

func GetSendMessage(id, source string) *SendMessage

GetSendMessage 创建一条发送记录 id 记录消息id source 记录这条信息是用户推送还是平台推送 user | platform

func (*SendMessage) Error

func (s *SendMessage) Error(info string)

Error 记录一个ERROR级别的日志

func (*SendMessage) Info

func (s *SendMessage) Info(info string)

Info 记录一个INFO级别的日志

func (*SendMessage) Panic

func (s *SendMessage) Panic(info string)

Panic 记录一个Panic错误日志并回收SendMessage对象

func (*SendMessage) Recycling

func (s *SendMessage) Recycling()

Recycling 回收SendMessage对象

type TcpClient

type TcpClient struct {
	Address string

	Conn net.Conn
	// contains filtered or unexported fields
}

TcpClient tcp客户端结构体

func NewClient

func NewClient(address string) *TcpClient

NewClient 实例化一个tcp客户端

func (*TcpClient) Close

func (t *TcpClient) Close()

Close 关闭tcp连接

func (*TcpClient) Connect

func (t *TcpClient) Connect() error

Connect 建立tcp连接

func (*TcpClient) OnPush

func (t *TcpClient) OnPush(fn func(message *SendMessage))

OnPush 当有新的推送消息到达tcp客户端时触发

func (*TcpClient) Publish

func (t *TcpClient) Publish(messageId, source, topic string, data json.RawMessage) error

Publish 通过tcp来进行推送的方法

func (*TcpClient) Read

func (t *TcpClient) Read() (*SendMessage, error)

Read 从tcp通道中读取消息

Jump to

Keyboard shortcuts

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