Skip to content

Commit

Permalink
feat(wti)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenShennn committed Mar 7, 2022
1 parent badc197 commit 63d95ed
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
cmd
.idea
.idea
go.mod
go.sum
6 changes: 3 additions & 3 deletions plugins/wti/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (

type Group struct {
rw *sync.RWMutex
set map[string]*client.Cli
set map[string]client.Clienter
createTime int64
}

func NewGroup()*Group {
return &Group{
rw: &sync.RWMutex{},
set: map[string]*client.Cli{},
set: map[string]client.Clienter{},
createTime: time.Now().Unix(),
}
}
Expand All @@ -36,7 +36,7 @@ func (g *Group) broadCast(content []byte){
}

// 添加cli
func (g *Group) addCli(clis ... *client.Cli){
func (g *Group) addCli(clis ...client.Clienter){
g.rw.Lock()
defer g.rw.Unlock()
for _,v := range clis{
Expand Down
23 changes: 17 additions & 6 deletions plugins/wti/interface.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// WTI 全称为 websocket Target interface ,就是长连接标记接口,目前系统提供的解决方案为
// WTI 全称为 websocket Target interface ,就是长连接标记接口,目前系统提供的解决方案为二维码map进行存储
package wti

import (
Expand All @@ -10,21 +10,24 @@ import (
// WebSocket Target Interface
type WTI interface {
// 给用户打上标签
SetTAG(cli *client.Cli, tag ...string)
SetTAG(cli client.Clienter, tags ...string)

// 删除用户的标签
DelTAG(cli client.Clienter, tags ...string)

// 如果用户下线将会通知调用这个方法
Update(token ...string)

// 广播到包含tag 对象
// 广播到包含标签对象
BroadCast(content []byte, tag ...string)

// 广播所有内容
BroadCastByTarget(targetAndContent map[string][]byte)

// 获取某个用户的所有的tag
// 获取某个用户的所有的标签
GetClienterTAGs(token string) []string

// 获取到TAG 的创建时间,系统会判断这个tag创建时间和当前人数来确认是否需要删除这个tag
// 获取到标签的创建时间
GetTAGCreateTime(tag string) int64

// 获取到所有tag的用户分布
Expand Down Expand Up @@ -60,14 +63,22 @@ var (
ERRNotSupportWTI = errors.New("im/plugins/wti: you should call the SetSupport func")
)

func SetTAG(cli *client.Cli, tag ...string) error {
func SetTAG(cli client.Clienter, tag ...string) error {
if isSupportWTI.Load() == false {
return ERRNotSupportWTI
}
factory.SetTAG(cli, tag...)
return nil
}

func DelTAG(cli client.Clienter, tag ...string) error {
if isSupportWTI.Load() == false {
return ERRNotSupportWTI
}
factory.DelTAG(cli, tag...)
return nil
}

func Update(token ...string) error {
if isSupportWTI.Load() == false {
return ERRNotSupportWTI
Expand Down
17 changes: 16 additions & 1 deletion plugins/wti/wti.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newwti() WTI {


// 给用户设置标签
func (t *tg) SetTAG(cli *client.Cli, tags ...string) {
func (t *tg) SetTAG(cli client.Clienter, tags ...string) {
if len(tags)== 0 {
return
}
Expand All @@ -38,6 +38,21 @@ func (t *tg) SetTAG(cli *client.Cli, tags ...string) {
}


// 删除用户的标签
func (t *tg) DelTAG(cli client.Clienter, tags ...string){
if len(tags) == 0 {return }
t.rw.Lock()
defer t.rw.Unlock()
for _,tag := range tags {
if group,ok := t.mp[tag];!ok{
continue
}else {
group.rmCli(cli.Token())
}
}
}


// 给某一个标签的群体进行广播
func (t *tg) BroadCast(content []byte,tags ...string) {
if len(tags)== 0 {
Expand Down
61 changes: 56 additions & 5 deletions plugins/wti/wti_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package wti
import (
"fmt"
"github.com/mongofs/im/client"

"net/http"
"testing"
)

type MockClient struct {}
func NewClient()client.Clienter {
return &MockClient{}
type MockClient struct {
token string
}
func NewClient(token string )client.Clienter {
return &MockClient{token: token}
}

func (m MockClient) Send(bytes []byte, i ...int64) error {
Expand All @@ -26,11 +29,59 @@ func (m MockClient) LastHeartBeat() int64 {
panic("implement me")
}
func (m MockClient) Token() string {
panic("implement me")
return m.token
}
func (m MockClient) Request() *http.Request {
panic("implement me")
}
func (m MockClient) SetMessageType(i int) {
panic("implement me")
}

func (m MockClient) SetProtocol(i int) {
panic("implement me")
}

func TestTg_DelTAG(t *testing.T) {
SetSupport()
tests := []struct{
token string
tag []string
}{
{
token: "1234",
tag: []string{"v1","v2","v3"},
},
{
token: "12345",
tag: []string{"v1","v2"},
},
}
// 先设置全局变量
for _,v := range tests{
SetTAG(NewClient(v.token),v.tag...) // 将每个client 进行设置tag
}
dis,err := Distribute()
if err != nil {
t.Fatal(err)
}
for k,v:=range dis{
fmt.Println(k,v)
}

// 删除对应的tag
for _,v := range tests{
DelTAG(NewClient(v.token),v.tag[0])
}
dis,err = Distribute()
if err != nil {
t.Fatal(err)
}
for k,v:=range dis{
fmt.Println("after delete tag zero",k,v)
}
}




Expand All @@ -52,7 +103,7 @@ func TestTg_SetTAG(t *testing.T) {

for _,v := range tests{
for i :=0 ;i< v.number;i++ {
// SetTAG(NewClient(),v.tag) ,todo 待优化
SetTAG(NewClient("1234"),v.tag) //,todo 待优化
}
}
}
Expand Down

0 comments on commit 63d95ed

Please sign in to comment.