ws

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 12, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TextMessage denotes a text data message. The text message payload is
	// interpreted as UTF-8 encoded text data.
	//text message表示文本数据消息。文本消息负载是
	//解释为UTF-8编码文本数据。
	TextMessage = 1

	// BinaryMessage denotes a binary data message.
	//BinaryMessage表示二进制数据消息。
	BinaryMessage = 2

	// CloseMessage denotes a close control message. The optional message
	// payload contains a numeric code and text. Use the FormatCloseMessage
	// function to format a close message payload.
	//CloseMessage表示关闭控制消息。可选消息
	//有效负载包含数字代码和文本。使用FormatCloseMessage
	//函数设置关闭消息有效负载的格式。
	CloseMessage = 8

	// PingMessage denotes a ping control message. The optional message payload
	// is UTF-8 encoded text.
	//PingMessage表示ping控制消息。可选消息负载
	//是UTF-8编码文本。
	PingMessage = 9

	// PongMessage denotes a pong control message. The optional message payload
	// is UTF-8 encoded text.
	//PongMessage表示pong控制消息。可选消息负载
	//是UTF-8编码文本。
	PongMessage = 10
)

ws消息类型常量 内容来自 github.com/gorilla/websocket/Conn.go

Variables

This section is empty.

Functions

func BroadcastMessage

func BroadcastMessage(data []byte) (success uint, error uint)

广播消息,推送给所有客户端

func Handshake

func Handshake(
	resp http.ResponseWriter, req *http.Request, onOpen func(conn Connection),
	onMessage func(conn Connection, msg Message), onClose func(conn Connection))

*

  • 封装ws握手,维护连接池
  • @param resp http.ResponseWriter
  • @param req http.Request
  • @param onOpen OpenEvent 新连接事件
  • @param onMessage MessageEvent 收到消息事件
  • @param onClose CloseEvent 连接断开事件

func SendWsMessage

func SendWsMessage(fd uint32, data []byte) error

根据fd查到连接发送消息

Types

type Client

type Client struct {
	Conn  []*Connection
	Count int //连接数量
	// contains filtered or unexported fields
}

Websocket连接池

var Clients Client

func (*Client) GetPoolIndex

func (c *Client) GetPoolIndex() uint32

获取连接池自增Id并加一

type Connection

type Connection struct {
	Fd       uint32
	WsSocket *websocket.Conn // 底层websocket
	// contains filtered or unexported fields
}

Websocket连接

func FindClient

func FindClient(fd uint32) Connection

根据fd取得ws连接

func (*Connection) Close

func (wsConn *Connection) Close() error

关闭ws连接

func (*Connection) Send

func (wsConn *Connection) Send(msgType int, data []byte) error

ws连接发送消息

type Message

type Message struct {
	Type int
	Data []byte
}

客户端读写消息

Jump to

Keyboard shortcuts

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