zero

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

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 11 Imported by: 0

README

zero

A Lightweight Socket Service with heartbeat, Can be easily used in TCP server development.

Build Status Go Report Card GoDoc

Wiki Page https://github.com/9b9387/zero/wiki

Requirements

Go version: 1.9.x or later

Usage

go get -u github.com/9b9387/zero
import "github.com/9b9387/zero"

func main() {
 	host := "127.0.0.1:18787"

 	ss, err := zero.NewSocketService(host)
	if err != nil {
		return
	}

	// set Heartbeat
	ss.SetHeartBeat(5*time.Second, 30*time.Second)

	// net event
	ss.RegMessageHandler(HandleMessage)
	ss.RegConnectHandler(HandleConnect)
	ss.RegDisconnectHandler(HandleDisconnect)

	ss.Serv()
}


Example Code: https://github.com/9b9387/zero/blob/master/service_test.go

Demo

Unity Client Repository https://github.com/9b9387/zero_unity_demo

Documentation

Index

Constants

View Source
const (
	// STUnknown Unknown
	STUnknown = iota
	// STInited Inited
	STInited
	// STRunning Running
	STRunning
	// STStop Stop
	STStop
)
View Source
const (
	// MsgHeartbeat heartbeat
	MsgHeartbeat = iota
)

Variables

This section is empty.

Functions

func Encode

func Encode(msg *Message) ([]byte, error)

Encode from Message to []byte

Types

type Conn

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

Conn wrap net.Conn

func NewConn

func NewConn(c net.Conn, hbInterval time.Duration, hbTimeout time.Duration) *Conn

NewConn create new conn

func (*Conn) Close

func (c *Conn) Close()

Close close connection

func (*Conn) GetName

func (c *Conn) GetName() string

GetName Get conn name

func (*Conn) SendMessage

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

SendMessage send message

type Message

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

Message struct

func Decode

func Decode(data []byte) (*Message, error)

Decode from []byte to Message

func NewMessage

func NewMessage(msgID int32, data []byte) *Message

NewMessage create a new message

func (*Message) GetData

func (msg *Message) GetData() []byte

GetData get message data

func (*Message) GetID

func (msg *Message) GetID() int32

GetID get message ID

func (*Message) String

func (msg *Message) String() string

func (*Message) Verify

func (msg *Message) Verify() bool

Verify verify checksum

type Session

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

Session struct

func NewSession

func NewSession(conn *Conn) *Session

NewSession create a new session

func (*Session) BindUserID

func (s *Session) BindUserID(uid string)

BindUserID bind a user ID to session

func (*Session) GetConn

func (s *Session) GetConn() *Conn

GetConn get zero.Conn pointer

func (*Session) GetSessionID

func (s *Session) GetSessionID() string

GetSessionID get session ID

func (*Session) GetSetting

func (s *Session) GetSetting(key string) interface{}

GetSetting get setting

func (*Session) GetUserID

func (s *Session) GetUserID() string

GetUserID get user ID

func (*Session) SetConn

func (s *Session) SetConn(conn *Conn)

SetConn set a zero.Conn to session

func (*Session) SetSetting

func (s *Session) SetSetting(key string, value interface{})

SetSetting set setting

type SocketService

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

SocketService struct

func NewSocketService

func NewSocketService(laddr string) (*SocketService, error)

NewSocketService create a new socket service

func (*SocketService) Broadcast

func (s *SocketService) Broadcast(msg *Message)

Broadcast Broadcast to all connections

func (*SocketService) GetConnsCount

func (s *SocketService) GetConnsCount() int

GetConnsCount get connect count

func (*SocketService) GetStatus

func (s *SocketService) GetStatus() int

GetStatus get socket service status

func (*SocketService) RegConnectHandler

func (s *SocketService) RegConnectHandler(handler func(*Session))

RegConnectHandler register connect handler

func (*SocketService) RegDisconnectHandler

func (s *SocketService) RegDisconnectHandler(handler func(*Session, error))

RegDisconnectHandler register disconnect handler

func (*SocketService) RegMessageHandler

func (s *SocketService) RegMessageHandler(handler func(*Session, *Message))

RegMessageHandler register message handler

func (*SocketService) Serv

func (s *SocketService) Serv()

Serv Start socket service

func (*SocketService) SetHeartBeat

func (s *SocketService) SetHeartBeat(hbInterval time.Duration, hbTimeout time.Duration) error

SetHeartBeat set heart beat

func (*SocketService) Stop

func (s *SocketService) Stop(reason string)

Stop stop socket service with reason

func (*SocketService) Unicast

func (s *SocketService) Unicast(sid string, msg *Message)

Unicast Unicast with session ID

Jump to

Keyboard shortcuts

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