ws

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

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

Go to latest
Published: Feb 27, 2020 License: MIT Imports: 8 Imported by: 0

README

C1000k WebSocket Server

demo: https://github.com/lwl1989/ws

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorResponse = DefaultResponse{
	Code: "200",
	Msg:  "操作失败",
}
View Source
var SuccessResponse = DefaultResponse{
	Code: "200",
	Msg:  "操作成功",
}

Functions

func Response

func Response(res http.ResponseWriter, response IResponse)

Types

type Connection

type Connection struct {
	*websocket.Conn

	UniqueKey string

	CLog   ILog
	Config IConfig
	// contains filtered or unexported fields
}

func (*Connection) Close

func (wsc *Connection) Close()

close and offline

func (*Connection) GetRoom

func (wsc *Connection) GetRoom() string

func (*Connection) GetUniqueKey

func (wsc *Connection) GetUniqueKey() string

func (*Connection) Send

func (wsc *Connection) Send(b []byte)

func (*Connection) SetRoom

func (wsc *Connection) SetRoom(room string)

func (*Connection) WriteBytes

func (wsc *Connection) WriteBytes(message []byte) error

func (*Connection) WriteString

func (wsc *Connection) WriteString(message string) error

type DefaultResponse

type DefaultResponse struct {
	Code string      `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (DefaultResponse) Error

func (res DefaultResponse) Error() string

func (DefaultResponse) GetCode

func (res DefaultResponse) GetCode() int

func (DefaultResponse) GetData

func (res DefaultResponse) GetData() interface{}

func (DefaultResponse) ToJsonBytes

func (res DefaultResponse) ToJsonBytes() []byte

func (DefaultResponse) ToJsonString

func (res DefaultResponse) ToJsonString() string

type IConfig

type IConfig interface {
	GetWriteWaitTime() (d time.Duration)
	GetReadWaitTime() (d time.Duration)
	GetConnectionWaitTime() (d time.Duration)
	GetMaxMessageSize() int64
	GetPongWaitTime() (d time.Duration)
	GetReadBufferSize() int
	GetWriteBufferSize() int
}

type IError

type IError interface {
	Error() string
	ToJsonBytes() []byte
	GetCode() int
}

type ILog

type ILog interface {
	Println(v ...interface{})
}

type IMessage

type IMessage interface {
	GetMessage() (bs []byte, length int64, err error)
	GetRoom() string
}

type IResponse

type IResponse interface {
	IError
	GetData() interface{}
}

type Protocol

type Protocol struct {

	//all connections, It's mapping O(1)
	ConnectionsMap map[string]*sync.Map

	//send and received message
	Msg chan IMessage

	//log
	PLog ILog

	//upgrade check logic
	CheckOrigin func(r *http.Request) bool
	//upgrade error handler
	UpErrorHandler func(res http.ResponseWriter, r *http.Request, status int, reason error)

	//config
	Config IConfig
	// contains filtered or unexported fields
}
var Wsp *Protocol

func (*Protocol) All

func (w *Protocol) All(room string) []*Connection

read lock

func (*Protocol) GetNumber

func (w *Protocol) GetNumber() uint64

func (*Protocol) OffLine

func (w *Protocol) OffLine(conn *Connection)

write lock

func (*Protocol) Online

func (w *Protocol) Online(conn *Connection)

conn connection,write lock

func (*Protocol) Run

func (w *Protocol) Run()

run 1 catch client in/out 2 catch message

func (*Protocol) Send

func (w *Protocol) Send(msg IMessage)

func (*Protocol) ServeHTTP

func (w *Protocol) ServeHTTP(rw http.ResponseWriter, r *http.Request)

router upgrade /ws/{room} router http /room/xxx/cancel router http /room/xxx/register

type RoomMsg

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

default IMessage

func (*RoomMsg) GetMessage

func (rm *RoomMsg) GetMessage() (bs []byte, length int64, err error)

func (*RoomMsg) GetMsg

func (rm *RoomMsg) GetMsg() []byte

func (*RoomMsg) GetRoom

func (rm *RoomMsg) GetRoom() string

func (*RoomMsg) SetMsg

func (rm *RoomMsg) SetMsg(m []byte)

func (*RoomMsg) SetRoom

func (rm *RoomMsg) SetRoom(r string)

Jump to

Keyboard shortcuts

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