websocket

package
v0.0.0-...-964918b Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2016 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Localhost           = "127.0.0.1"
	WSServerPort        = 8000
	MaxConnectionNumber = 4096
	IdleSessionTimeOut  = 120
	IdleCheckInterval   = 30
)
View Source
const (
	Error_Code_Successful = 0
	Error_Code_Failure    = 4001
)

Variables

View Source
var ErrorMsgMap = map[int]string{
	0:    "successful",
	4001: "failure",
}

ErrorMsgMap is the map from status code to error message.

View Source
var MsgHandlerMap = map[string]MsgHandler{
	"watch_log":       watchLogHandler,
	"heart_beat":      heartBeatHandler,
	"worker_push_log": workerPushLogHandler,
}

MsgHandlerMap is the map of web client message handler

Functions

func AnalysisMessage

func AnalysisMessage(dp *DataPacket) bool

AnalysisMessage analysis message receive from the web client.

func CreateTopicName

func CreateTopicName(sAPI string, sUserID string, sServiceID string,
	sVersionID string) string

CreateTopicName creat topic name as: api__userid__serviceid__versionid

func LoadServerConfig

func LoadServerConfig() error

LoadServerConfig load config of push-log websocket server

func PacketHeartBeat

func PacketHeartBeat(sID string) []byte

PacketHeartBeat packet the data frame of heart beat

func PacketPushLog

func PacketPushLog(SAPI string, sUserID string, sServiceID string,
	sVersion string, sLog string, sID string) []byte

PacketPushLog packet the data frame of push log

func PacketResponse

func PacketResponse(sResponse string, sIDAck string, nErrorCode int) []byte

PacketResponse packet the data frame of reponse

func PacketWatchLog

func PacketWatchLog(SAPI string, sUserID string, sServiceID string,
	sVersion string, sOperation string, sID string) []byte

PacketWatchLog packet the data frame of watch log

func PushTopic

func PushTopic(wss *WSSession, pWatchLog *WatchLogPacket)

PushTopic push log from special topic to web client

func StartServer

func StartServer() (err error)

StartServer start the websocket server

Types

type DataPacket

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

DataPacket is the type for data packet.

func (*DataPacket) GetData

func (p *DataPacket) GetData() []byte

GetData gets date from data packet.

func (*DataPacket) GetLength

func (p *DataPacket) GetLength() int

GetLength gets the length of data.

func (*DataPacket) GetReceiveFrom

func (p *DataPacket) GetReceiveFrom() string

GetReceiveFrom gets the session id of the date receive from.

func (*DataPacket) GetSendTo

func (p *DataPacket) GetSendTo() string

GetSendTo gets the session id of the date need send to.

func (*DataPacket) SetData

func (p *DataPacket) SetData(byrFrame []byte)

SetData sets data in data packet.

func (*DataPacket) SetLength

func (p *DataPacket) SetLength(nLen int)

SetLength sets length of data.

func (*DataPacket) SetReceiveFrom

func (p *DataPacket) SetReceiveFrom(sReceiveFrom string)

SetReceiveFrom sets the session id of the date receive from.

func (*DataPacket) SetSendTo

func (p *DataPacket) SetSendTo(sSendTo string)

SetSendTo sets the session id of the date need send to.

type HeartBeatPacket

type HeartBeatPacket struct {
	Action string `json:"action"`
	Id     string `json:"id"`
}

HeartBeatPacket is the type for heart_beat packet.

type IDataPacket

type IDataPacket interface {
	GetLength() int
	GetData() []byte
	GetSendTo() string
	GetReceiveFrom() string
}

IDataPacket is the interface for data packet.

type ISession

type ISession interface {
	OnClosed()
	OnStart(sSessionID string)
	OnReceive(datapacket IDataPacket) bool
	Send(datapacket IDataPacket)
	GetSessionID() string
}

ISession is the interface for session.

type MsgHandler

type MsgHandler func(sReceiveFrom string, jsonPacket []byte)

MsgHandler is the type for message handler.

type PushLogPacket

type PushLogPacket struct {
	Action    string `json:"action"`
	Api       string `json:"api"`
	UserId    string `json:"user_id"`
	ServiceId string `json:"service_id"`
	VersionId string `json:"version_id"`
	Log       string `json:"log"`
	Id        string `json:"id"`
}

PushLogPacket is the type for push_log packet.

type ResponsePacket

type ResponsePacket struct {
	Action    string `json:"action"`
	Response  string `json:"response"`
	IdAck     string `json:"id_ack"`
	ErrorCode int    `json:"error_code"`
	ErrorMsg  string `json:"error_msg"`
}

ResponsePacket is the type for response packet.

type ServerConfig

type ServerConfig struct {
	ServerIp            string `yml:"ServerIp"`
	Port                int    `yml:"ServerPort"`
	MaxConnectionNumber int    `yml:"MaxConnectionNumber"`
	IdleSessionTimeOut  int64  `yml:"IdleSessionTimeOut"`
	IdleCheckInterval   int    `yml:"IdleCheckInterval"`
	ServerCertificate   string `yml:"ServerCertificate"`
	ServerKey           string `yml:"ServerKey"`
}

ServerConfig is the config of push-log websocket server

func GetConfig

func GetConfig() *ServerConfig

GetConfig get config of push-log websocket server

type SessionList

type SessionList struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SessionList is the type for session list.

func GetSessionList

func GetSessionList() *SessionList

GetSessionList get web client session list

func (*SessionList) GetSession

func (sl *SessionList) GetSession(sSessionID string) ISession

GetSession get one session from web client session list by sessionid

type WSSession

type WSSession struct {

	//sync log for tapic enable flags
	sync.RWMutex
	// contains filtered or unexported fields
}

WSSession is the type for web socket session.

func CreateWSSession

func CreateWSSession(wcConn *websocket.Conn) (wssSession *WSSession, err error)

CreateWSSession create new session and incert it to sessionlist

func (*WSSession) ClearTopicEnable

func (wss *WSSession) ClearTopicEnable()

ClearTopicEnable clear the map of tapic enable flags

func (*WSSession) GetSessionID

func (wss *WSSession) GetSessionID() string

GetSessionID get the session id

func (*WSSession) GetTopicEnable

func (wss *WSSession) GetTopicEnable(sTopic string) bool

GetTopicEnable get sepcial topic enable flag

func (*WSSession) OnClosed

func (wss *WSSession) OnClosed()

OnClosed handle something when the session close remove session from sessionlist close websocket link clear member var

func (*WSSession) OnReceive

func (wss *WSSession) OnReceive(iData IDataPacket) bool

OnReceive handle something when the session receive message analysis the message

func (*WSSession) OnStart

func (wss *WSSession) OnStart(sSessionID string)

OnStart handles something when the session start

func (*WSSession) Send

func (wss *WSSession) Send(iData IDataPacket)

Send sends data packet to web client

func (*WSSession) SessionTimeoverCheck

func (wss *WSSession) SessionTimeoverCheck() bool

SessionTimeoverCheck check timeout if unreceive vaild message from web cliet

func (*WSSession) SetTopicEnable

func (wss *WSSession) SetTopicEnable(sTopic string, bEnable bool)

SetTopicEnable set sepcial topic enable flag

func (*WSSession) UpdateActiveTime

func (wss *WSSession) UpdateActiveTime()

UpdateActiveTime updates active time of this session.

type WatchLogPacket

type WatchLogPacket struct {
	Action    string `json:"action"`
	Api       string `json:"api"`
	UserId    string `json:"user_id"`
	ServiceId string `json:"service_id"`
	VersionId string `json:"version_id"`
	Operation string `json:"operation"`
	Id        string `json:"id"`
}

WatchLogPacket is the type for watch_log packet.

type WorkerPushLogPacket

type WorkerPushLogPacket struct {
	Action string `json:"action"`
	Topic  string `json:"topic"`
	Log    string `json:"log"`
}

WorkerPushLogPacket is the type for worker_push_log packet.

Jump to

Keyboard shortcuts

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