ws

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Copyright 2019-2020 Axetroy. All rights reserved. MIT license.

Index

Constants

This section is empty.

Variables

View Source
var (
	UserPoll   = NewPool()
	WaiterPoll = NewPool()
)
View Source
var MatcherPool = NewMatcher()

Functions

This section is empty.

Types

type Client

type Client struct {
	sync.RWMutex

	UUID string // Socket 连接的唯一标识符
	// contains filtered or unexported fields
}

func NewClient

func NewClient(conn *websocket.Conn) *Client

func (*Client) Close

func (c *Client) Close() error

关闭连接

func (*Client) GetProfile

func (c *Client) GetProfile() *schema.ProfilePublic

func (*Client) UpdateProfile

func (c *Client) UpdateProfile(profile schema.ProfilePublic)

func (*Client) WriteError

func (c *Client) WriteError(err error, data Message) error

func (*Client) WriteJSON

func (c *Client) WriteJSON(data Message) error

向客户端写数据

type Matcher

type Matcher struct {
	sync.RWMutex
	Broadcast chan bool // 调度器,当收到通知时,就安排客服接待排队的用户
	// contains filtered or unexported fields
}

func NewMatcher

func NewMatcher() *Matcher

func (*Matcher) AddWaiter

func (c *Matcher) AddWaiter(waiterSocketUUID string)

添加客服

func (*Matcher) GetIdleWaiter

func (c *Matcher) GetIdleWaiter() *string

获取当前最空闲的客服

func (*Matcher) GetMatcher

func (c *Matcher) GetMatcher() map[string][]string

func (*Matcher) GetMyUsers

func (c *Matcher) GetMyUsers(waiterSocketUUID string) []string

获取这个客服当前服务的用户

func (*Matcher) GetMyWaiter

func (c *Matcher) GetMyWaiter(userSocketUUID string) *string

获取当前接待我的客服

func (*Matcher) GetPendingLength

func (c *Matcher) GetPendingLength() int

func (*Matcher) Join

func (c *Matcher) Join(userSocketUUID string, prepend ...bool) *string

用户加入匹配池 返回接待的客服 UUID 如果返回空,那么说明没有找到合适的客服,加入等待队列 第二个参数

func (*Matcher) Leave

func (c *Matcher) Leave(userSocketUUID string)

用户离开匹配池

func (*Matcher) RemoveWaiter

func (c *Matcher) RemoveWaiter(waiterSocketUUID string)

移除客服

func (*Matcher) ShiftPending

func (c *Matcher) ShiftPending() *string

type Message

type Message struct {
	From    string      `json:"from,omitempty"`                // 从谁发出来的
	To      string      `json:"to,omitempty"`                  // 要发送的目标 ID,只有客服才需要带 target 字段,指明发送给谁
	Type    string      `json:"type" valid:"required~请输入会话类型"` // 会话类型
	Payload interface{} `json:"payload,omitempty"`             // 本次消息的数据
}

来回传输的消息体

type Pool

type Pool struct {
	Broadcast chan Message // 广播频道
	// contains filtered or unexported fields
}

func NewPool

func NewPool() *Pool

func (*Pool) Add

func (c *Pool) Add(client *Client)

添加一个连接

func (*Pool) Get

func (c *Pool) Get(UUID string) *Client

获取连接

func (*Pool) Length

func (c *Pool) Length() int

获取连接长度

func (*Pool) Remove

func (c *Pool) Remove(UUID string)

删除连接

type TypeRequestUser

type TypeRequestUser typeForWriteMessage // 用户发出的消息类型
const (
	TypeRequestUserAuth        TypeRequestUser = "auth"         // 认证帐号
	TypeRequestUserConnect     TypeRequestUser = "connect"      // 请求连接一个客服
	TypeRequestUserDisconnect  TypeRequestUser = "disconnect"   // 请求和客服断开连接
	TypeRequestUserMessageText TypeRequestUser = "message_text" // 发送文本消息
)

用户可以发出的消息类型

type TypeRequestWaiter

type TypeRequestWaiter typeForWriteMessage // 客服发出的消息类型
const (
	TypeRequestWaiterAuth        TypeRequestWaiter = "auth"         // 身份认证
	TypeRequestWaiterReady       TypeRequestWaiter = "ready"        // 客服已准备就绪,可以开始接收客人
	TypeRequestWaiterMessageText TypeRequestWaiter = "message_text" // 客服发出文本消息
	TypeRequestWaiterDisconnect  TypeRequestWaiter = "disconnect"   // 请求断开连接
)

客服发出的消息类型

type TypeResponseUser

type TypeResponseUser typeForWriteMessage // 输出给用户的消息类型
const (
	TypeResponseUserAuthSuccess    TypeResponseUser = "auth_success"    // 初始化,告诉用户当前的链接 ID
	TypeResponseUserNotConnect     TypeResponseUser = "not_connect"     // 尚未连接
	TypeResponseUserConnectSuccess TypeResponseUser = "connect_success" // 连接成功,现在可以开始对话
	TypeResponseUserDisconnected   TypeResponseUser = "disconnected"    // 客服与用户断开连接
	TypeResponseUserConnectQueue   TypeResponseUser = "connect_queue"   // 正在排队,请等待
	TypeResponseUserMessageText    TypeResponseUser = "message_text"    // 用户收到文本消息
	TypeResponseUserError          TypeResponseUser = "error"           // 用户收到一个错误
)

用户收到的类型

type TypeResponseWaiter

type TypeResponseWaiter typeForWriteMessage // 输出给客服的消息类型
const (
	TypeResponseWaiterMessageText   TypeResponseWaiter = "message_text"   // 客服收到文本消息
	TypeResponseWaiterNewConnection TypeResponseWaiter = "new_connection" // 有新连接
	TypeResponseWaiterDisconnected  TypeResponseWaiter = "disconnected"   // 有新连接断开
	TypeResponseWaiterError         TypeResponseWaiter = "error"          // 有新连接断开
)

客服收到的消息

Jump to

Keyboard shortcuts

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