go_websocket

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

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

Go to latest
Published: Dec 6, 2022 License: MIT Imports: 9 Imported by: 0

README

使用方式

说明
  • 基于github.com/gorilla/websocket
  • 增加了client_id,区分客户端
  • 可以发送消息到指定client_id
导入包
go get github.com/lixiang4u/go-websocket
实例化对象
var ws = go_websocket.WSWrapper{}
注册响应事件
ws.On(eventName string, f eventHandler)
  • 需要客户端请求数据格式为protocol对象的json字面量
type protocol struct {
	Event    string      `json:"event"`
	Data     interface{} `json:"data"`
}
  • 测试返回的数据格式为protocol对象的json字面量
type protocol struct {
	ClientId string      `json:"client_id"`
	Event    string      `json:"event"`
	Data     interface{} `json:"data"`
}
运行
ws.Run(w http.ResponseWriter, r *http.Request, responseHeader http.Header)
广播聊天截图

markdown

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Log2

func Log2(format string, v ...interface{})

func UUID

func UUID(length ...int) string

func WSBroadcast

func WSBroadcast(clientId string, messageType int, data []byte)

func WSConnectionList

func WSConnectionList() map[string]interface{}

func WSendMessage

func WSendMessage(clientId string, messageType int, data []byte) bool

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

Types

type Conf

type Conf struct {
	Debug bool
}

ws的全局配置

type WSWrapper

type WSWrapper struct {
	Config Conf
	// contains filtered or unexported fields
}

当前ws对外包装

func (*WSWrapper) On

func (c *WSWrapper) On(eventName string, f eventHandler) bool

注册事件

func (*WSWrapper) Run

func (c *WSWrapper) Run(w http.ResponseWriter, r *http.Request, responseHeader http.Header)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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