go_websocket

package module
v0.0.0-...-8ecaae2 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 9 Imported by: 2

README

demo启动方式

go run _examples/main.go

浏览器打开http://127.0.0.1:8088

使用方式

说明
  • 基于github.com/gorilla/websocket
导入包
go get github.com/lixiang4u/go-websocket
实例化对象
import (
	go_websocket "github.com/lixiang4u/go-websocket"
)

var ws = go_websocket.NewWebsocketManager()
注册响应事件
  • 内置事件返回true才会执行自定义事件,否则直接执行自定义事件
ws.On(eventName string, f eventHandler)
  • 需要客户端请求数据格式为protocol对象的json字面量
type EventProtocol struct {
	Event    string      `json:"event"`
	Data     interface{} `json:"data"`
}
  • 内置事件如下(部分事件不直接对外暴露):
"connect",
"sendToUid",
"listGroup",
"listGroupClient",
"joinGroup",
"leaveGroup",
"close",
"ping",
"bindUid",
"sendToClient",
"sendToGroup"
运行
ws.Handler(w http.ResponseWriter, r *http.Request, responseHeader http.Header)
广播聊天截图

markdown

Documentation

Index

Constants

View Source
const (
	EventHelp    = iota
	EventConnect = iota
	EventClose   = iota

	EventPing    = iota
	EventBindUid = iota

	EventSendToClient = iota
	EventSendToUid    = iota
	EventSendToGroup  = iota
	EventBroadcast    = iota

	EventJoinGroup       = iota
	EventLeaveGroup      = iota
	EventListGroup       = iota
	EventListGroupClient = iota
)

Variables

This section is empty.

Functions

func ToJson

func ToJson(v interface{}) string

func UUID

func UUID(length ...int) string

Types

type ConnectionContext

type ConnectionContext struct {
	Conn  *websocket.Conn
	Group map[string]bool
	Uid   string
}

type ConnectionMutex

type ConnectionMutex struct {
	Conn  map[string]*ConnectionContext // [ClientId => CONNECTION_DATA]
	Uid   map[string]map[string]bool    // [Uid => [ClientId => bool]]
	Group map[string]map[string]bool    // [GroupName => [ClientId => bool]]
	// contains filtered or unexported fields
}

func (*ConnectionMutex) GetGroupClientIds

func (x *ConnectionMutex) GetGroupClientIds(groupName string) []string

func (*ConnectionMutex) GetUidClientId

func (x *ConnectionMutex) GetUidClientId(uid string) []string

func (*ConnectionMutex) JoinGroup

func (x *ConnectionMutex) JoinGroup(clientId, groupName string)

func (*ConnectionMutex) LeaveGroup

func (x *ConnectionMutex) LeaveGroup(clientId, groupName string)

func (*ConnectionMutex) ListGroup

func (x *ConnectionMutex) ListGroup() []string

func (*ConnectionMutex) ListGroupClientIds

func (x *ConnectionMutex) ListGroupClientIds(groupName string) []string

func (*ConnectionMutex) LoadConn

func (x *ConnectionMutex) LoadConn(clientId string) *websocket.Conn

func (*ConnectionMutex) LoadConnContext

func (x *ConnectionMutex) LoadConnContext(clientId string) *ConnectionContext

func (*ConnectionMutex) Remove

func (x *ConnectionMutex) Remove(clientId string)

func (*ConnectionMutex) SetUid

func (x *ConnectionMutex) SetUid(clientId, uid string)

func (*ConnectionMutex) Store

func (x *ConnectionMutex) Store(clientId string, ws *websocket.Conn)

type Event

type Event int

func (Event) String

func (x Event) String() string

type EventHandler

type EventHandler func(clientId string, ws *websocket.Conn, messageType int, data EventProtocol) bool

EventHandler 事件响应格式

type EventProtocol

type EventProtocol struct {
	ClientId string      `json:"client_id"`
	Event    string      `json:"event"`
	Data     interface{} `json:"data"`
}

EventProtocol ws数据交互格式,基于json,event字段必选

type EventProtocolConnect

type EventProtocolConnect struct {
	ClientId string `json:"client_id"`
	Event    string `json:"event"`
	Data     struct {
		ClientId string `json:"client_id"`
	} `json:"data"`
}

type H

type H map[string]interface{}

type WebsocketManager

type WebsocketManager struct {
	Conn   *ConnectionMutex
	Config struct {
		Debug bool
	}
	// contains filtered or unexported fields
}

func NewWebsocketManager

func NewWebsocketManager(debug ...bool) *WebsocketManager

func (*WebsocketManager) Handler

func (x *WebsocketManager) Handler(w http.ResponseWriter, r *http.Request, responseHeader http.Header)

Handler 开始处理websocket请求

func (*WebsocketManager) Log

func (x *WebsocketManager) Log(format string, v ...interface{})

func (*WebsocketManager) LogForce

func (x *WebsocketManager) LogForce(format string, v ...interface{})

func (*WebsocketManager) On

func (x *WebsocketManager) On(eventName string, f EventHandler) bool

On 注册事件

func (*WebsocketManager) Send

func (x *WebsocketManager) Send(clientId string, messageType int, data []byte) bool

Send 对外接口,用于发送ws消息到指定clientId

func (*WebsocketManager) ToBytes

func (x *WebsocketManager) ToBytes(v interface{}) []byte

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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