websockethttp

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 14, 2022 License: MulanPSL-2.0 Imports: 11 Imported by: 0

README

websockethttp-go

golang support for websockethttp

websockethttp

Use
go get gitee.com/vesmr/websockethttp-go
Server [server_test.go]
    server := ServerNew("test-server")
    server.ShowPongLogs(true)
    server.SetAuthorizationFunc(func(request *http.Request) (string, bool) {
        name := request.URL.Query().Get("name")
        return name, len(name) > 2
    })
	
    // 注册一个Process等待客户端访问
    server.RegisterProcessFunc("ServerTest", func(context *ServerContext) {
        log.Printf("收到请求信息: %v", context.Request)
        // 设置响应数据
        context.Response.Code = 0
        context.Response.Message = "ok"
        context.Response.Body = "你好,这是服务器!"
    })
	
    // 监听连接成功事件
    server.AddConnOpenEventFunc(func(channel *ClientConnChannel) {
        log.Printf("收到连接: %v", channel.ConnName)
		
        // 给客户端推送消息
        channel.SendMessage("ClientTest", nil, "Hi client, conn ok !", func(context *ServerContext) {
            log.Printf("收到响应信息: %v", context.Response.Body)
        })
    })
    _ = server.LauncherServer("/websockethttp", 8080)
Client [client_test.go]
    client, err := ClientNew("ws://127.0.0.1:8080/websockethttp?name=test", "test-client")
    if err != nil {
        log.Printf("连接错误: %v", err)
        return
    }
    client.ShowPongLogs(true)
    
    // 注册一个Process等待服务器推送
    client.RegisterProcessFunc("ClientTest", func(context *ClientContext) {
        log.Printf("收到请求信息: %v", context.Request)
        // 设置响应数据
        context.Response.Code = 0
        context.Response.Message = "ok"
        context.Response.Body = "你好,这是客户端!"
    })

    // 发送消息到服务器
    client.SendMessage("ServerTest", nil, "Hi, server !", func(context *ClientContext) {
        log.Printf("收到响应信息: %v", context.Response.Body)
    })
DEMO

[test-websockethttp-server]

Documentation

Index

Constants

View Source
const (
	PongProcessName       = "Health"
	PongProcessTime       = 4500
	PongProcessSuccess    = 0
	RequestTimeoutCode    = -500
	RequestTimeoutMsg     = "timeout"
	RequestDefaultTimeout = int64(60 * 1000)
)

Variables

This section is empty.

Functions

func NewMessageId added in v1.0.1

func NewMessageId() string

NewMessageId 创建一个消息ID

Types

type Client added in v1.0.1

type Client struct {
	Conn *websocket.Conn // 连接实例
	Name string          // 连接名称
	Url  string          // 连接路径
	// contains filtered or unexported fields
}

func ClientNew added in v1.0.4

func ClientNew(url, clientName string) (*Client, error)

ClientNew 创建连接

func (*Client) AddClientRequestFilterFunc added in v1.0.4

func (client *Client) AddClientRequestFilterFunc(filter func(context *ClientContext) bool)

AddClientRequestFilterFunc 添加Request过滤器函数(仅针对客户端对服务端的请求有效)

func (*Client) AddClientResponseFilterFunc added in v1.0.4

func (client *Client) AddClientResponseFilterFunc(filter func(context *ClientContext) bool)

AddClientResponseFilterFunc 添加Response过滤器函数(仅针对服务端对客户端的响应有效)

func (*Client) AddServerRequestFilterFunc added in v1.0.4

func (client *Client) AddServerRequestFilterFunc(filter func(context *ClientContext) bool)

AddServerRequestFilterFunc 添加Request过滤器函数(仅针对服务端对客户端的请求有效)

func (*Client) AddServerResponseFilterFunc added in v1.0.4

func (client *Client) AddServerResponseFilterFunc(filter func(context *ClientContext) bool)

AddServerResponseFilterFunc 添加Response过滤器函数(仅针对客户端对服务端的响应有效)

func (*Client) CloseConnection added in v1.0.4

func (client *Client) CloseConnection()

CloseConnection 关闭连接

func (*Client) IsActiveStatus added in v1.0.4

func (client *Client) IsActiveStatus() bool

IsActiveStatus 客户端是否活跃(即使当前连接是断开的但是系统会自动重连)

func (*Client) RegisterProcessFunc added in v1.0.4

func (client *Client) RegisterProcessFunc(processFuncName string, processFunc func(context *ClientContext))

RegisterProcessFunc 注册请求处理器(所有的业务应该在Process中处理)

func (*Client) SendMessage added in v1.0.4

func (client *Client) SendMessage(process string, header map[string]string, body string, callback func(c *ClientContext))

func (*Client) SendMessageAsync added in v1.0.4

func (client *Client) SendMessageAsync(process string, header map[string]string, body string) (*ClientContext, error)

func (*Client) SetDefaultProcessFunc added in v1.0.4

func (client *Client) SetDefaultProcessFunc(fun func(context *ClientContext))

SetDefaultProcessFunc 默认Process函数

func (*Client) SetPongTime added in v1.0.4

func (client *Client) SetPongTime(pongTime int64)

SetPongTime 设置心跳间隔(毫秒)

func (*Client) SetReadBeforeFunc added in v1.0.4

func (client *Client) SetReadBeforeFunc(fun func(data []byte) []byte)

SetReadBeforeFunc 消息接收之前处理函数

func (*Client) SetRequestCommonHeader added in v1.0.4

func (client *Client) SetRequestCommonHeader(header map[string]string)

func (*Client) SetRequestTimeout added in v1.0.4

func (client *Client) SetRequestTimeout(timeout int64)

func (*Client) SetResponseCommonHeader added in v1.0.4

func (client *Client) SetResponseCommonHeader(header map[string]string)

func (*Client) SetWriteBeforeFunc added in v1.0.4

func (client *Client) SetWriteBeforeFunc(fun func(data []byte) []byte)

SetWriteBeforeFunc 消息传送之前处理函数

func (*Client) ShowPongLogs added in v1.0.4

func (client *Client) ShowPongLogs(isShowPongLogs bool)

ShowPongLogs 显示心跳日志

type ClientConnChannel added in v1.0.4

type ClientConnChannel struct {
	ConnName   string          // 客户端名称
	PongTime   int64           // 心跳最新时间
	ConnSocket *websocket.Conn // 连接对象
	BindServer *Server         // 绑定的服务
}

ClientConnChannel 封装一个客户端连接信息

func (*ClientConnChannel) CloseConnection added in v1.0.4

func (channel *ClientConnChannel) CloseConnection(code int, message string)

CloseConnection 关闭渠道(连接)

func (*ClientConnChannel) SendMessage added in v1.0.4

func (channel *ClientConnChannel) SendMessage(process string, header map[string]string, body string, callback func(context *ServerContext))

SendMessage 发送消息(异步)

func (*ClientConnChannel) SendMessageSync added in v1.0.4

func (channel *ClientConnChannel) SendMessageSync(process string, header map[string]string, body string) (*ServerContext, error)

SendMessageSync 发送消息(同步)

type ClientContext added in v1.0.4

type ClientContext struct {
	Client   *Client         `json:"client"`
	Request  *SocketRequest  `json:"request"`
	Response *SocketResponse `json:"response"`
	Extra    interface{}     `json:"extra"`
	Error    error           `json:"error"`
}

ClientContext 封装请求上下文信息

type ClientRequestCallback added in v1.0.4

type ClientRequestCallback struct {
	Context    *ClientContext           `json:"context"`
	Callback   func(ctx *ClientContext) `json:"callback"`
	CreateTime int64                    `json:"create_time"`
}

ClientRequestCallback 封装request异步回调的函数信息

type Server added in v1.0.1

type Server struct {
	Name string // 服务名
	// contains filtered or unexported fields
}

func ServerNew added in v1.0.4

func ServerNew(serverName string) *Server

ServerNew 新建一个服务实例

func (*Server) AddClientRequestFilterFunc added in v1.0.1

func (server *Server) AddClientRequestFilterFunc(filter func(context *ServerContext) bool)

AddClientRequestFilterFunc 添加Request过滤器函数(仅针对客户端对服务端的请求有效)

func (*Server) AddClientResponseFilterFunc added in v1.0.1

func (server *Server) AddClientResponseFilterFunc(filter func(context *ServerContext) bool)

AddClientResponseFilterFunc 添加Response过滤器函数(仅针对服务端对客户端的响应有效)

func (*Server) AddConnCloseEventFunc added in v1.0.4

func (server *Server) AddConnCloseEventFunc(fun func(channel *ClientConnChannel))

AddConnCloseEventFunc 设置连接监听

func (*Server) AddConnOpenEventFunc added in v1.0.4

func (server *Server) AddConnOpenEventFunc(fun func(channel *ClientConnChannel))

AddConnOpenEventFunc 设置连接监听

func (*Server) AddServerRequestFilterFunc added in v1.0.1

func (server *Server) AddServerRequestFilterFunc(filter func(context *ServerContext) bool)

AddServerRequestFilterFunc 添加Request过滤器函数(仅针对服务端对客户端的请求有效)

func (*Server) AddServerResponseFilterFunc added in v1.0.1

func (server *Server) AddServerResponseFilterFunc(filter func(context *ServerContext) bool)

AddServerResponseFilterFunc 添加Response过滤器函数(仅针对客户端对服务端的响应有效)

func (*Server) GetAllClientChannel added in v1.0.4

func (server *Server) GetAllClientChannel() map[string]*ClientConnChannel

GetAllClientChannel 获取全部连接

func (*Server) GetClientChannel added in v1.0.4

func (server *Server) GetClientChannel(name string) (*ClientConnChannel, error)

GetClientChannel 获取连接(name 为 authorizationFunc 的返回值)

func (*Server) LauncherServer added in v1.0.4

func (server *Server) LauncherServer(path string, port int) error

LauncherServer 启动服务

func (*Server) RegisterProcessFunc added in v1.0.4

func (server *Server) RegisterProcessFunc(processFuncName string, processFunc func(context *ServerContext))

RegisterProcessFunc 注册请求处理器(所有的业务应该在Process中处理)

func (*Server) SetAuthorizationFunc added in v1.0.1

func (server *Server) SetAuthorizationFunc(fun func(request *http.Request) (string, bool))

SetAuthorizationFunc 设置授权处理函数,返回: 连接名称/认证结果

func (*Server) SetDefaultProcessFunc added in v1.0.4

func (server *Server) SetDefaultProcessFunc(fun func(context *ServerContext))

SetDefaultProcessFunc 默认Process函数

func (*Server) SetReadBeforeFunc added in v1.0.4

func (server *Server) SetReadBeforeFunc(fun func(data []byte) []byte)

SetReadBeforeFunc 设置 websocket.Upgrader

func (*Server) SetRequestCommonHeader added in v1.0.1

func (server *Server) SetRequestCommonHeader(header map[string]string)

SetRequestCommonHeader 设置 Common Request Header

func (*Server) SetRequestTimeout added in v1.0.4

func (server *Server) SetRequestTimeout(timeout int64)

SetRequestTimeout 设置超时

func (*Server) SetResponseCommonHeader added in v1.0.4

func (server *Server) SetResponseCommonHeader(header map[string]string)

SetResponseCommonHeader 设置 Common Request Header

func (*Server) SetWebsocketUpgrader added in v1.0.4

func (server *Server) SetWebsocketUpgrader(upgrader *websocket.Upgrader)

SetWebsocketUpgrader 消息接收之前处理函数

func (*Server) SetWriteBeforeFunc added in v1.0.4

func (server *Server) SetWriteBeforeFunc(fun func(data []byte) []byte)

SetWriteBeforeFunc 消息传送之前处理函数

func (*Server) ShowPongLogs added in v1.0.4

func (server *Server) ShowPongLogs(isShowPongLogs bool)

ShowPongLogs 显示心跳日志

func (*Server) UpgradeHttp added in v1.0.4

func (server *Server) UpgradeHttp(writer http.ResponseWriter, request *http.Request)

UpgradeHttp 将http转为socket(兼容使用gin等第三方web框架)

type ServerContext added in v1.0.4

type ServerContext struct {
	Channel  *ClientConnChannel `json:"channel"`
	Request  *SocketRequest     `json:"request"`
	Response *SocketResponse    `json:"response"`
	Error    error              `json:"error"`
}

ServerContext 封装请求上下文信息

type ServerRequestCallback added in v1.0.4

type ServerRequestCallback struct {
	Context    *ServerContext           `json:"context"`
	Callback   func(ctx *ServerContext) `json:"callback"`
	CreateTime int64                    `json:"create_time"`
}

ServerRequestCallback 封装request异步回调的函数信息

type SocketRequest

type SocketRequest struct {
	Uid     string            `json:"U"` // 消息唯一ID
	Process string            `json:"P"` // 处理器
	Header  map[string]string `json:"H"` // 数据头
	Body    string            `json:"B"` // 数据体(需要进行编码后传输)
}

SocketRequest {"U":"","P":"","H":{},"B":""}

func RequestNew added in v1.0.4

func RequestNew(process string, header map[string]string, body string) *SocketRequest

RequestNew 新建一个请求体

type SocketResponse

type SocketResponse struct {
	Uid     string            `json:"U"` // 消息唯一ID
	Code    int               `json:"C"` // 业务状态
	Message string            `json:"M"` // 业务说明
	Header  map[string]string `json:"H"` // 数据头
	Body    string            `json:"B"` // 数据体(需要进行编码后传输)
}

SocketResponse {"U":"","C":"","M":"","H":{},"B":""}

func ResponseNew added in v1.0.4

func ResponseNew(code int, message string, header map[string]string, body string) *SocketResponse

ResponseNew 新建一个响应体

Jump to

Keyboard shortcuts

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