Skip to content

Commit

Permalink
update client id
Browse files Browse the repository at this point in the history
  • Loading branch information
loeffel-io committed Feb 5, 2021
1 parent bbd0b79 commit 1c4336a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
12 changes: 6 additions & 6 deletions event/basic/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package makeless_go_event_basic

import (
"github.com/gin-contrib/sse"
"github.com/google/uuid"
"github.com/makeless/makeless-go/event"
"sync"
"time"
)

type Event struct {
Expand All @@ -31,15 +31,15 @@ func (event *Event) GetError() chan error {
return event.Error
}

func (event *Event) NewClientId() uint {
return uint(time.Now().Unix())
func (event *Event) NewClientId() string {
return uuid.NewString()
}

func (event *Event) Subscribe(userId uint, clientId uint) {
func (event *Event) Subscribe(userId uint, clientId string) {
event.GetHub().NewClient(userId, clientId)
}

func (event *Event) Unsubscribe(userId uint, clientId uint) {
func (event *Event) Unsubscribe(userId uint, clientId string) {
event.GetHub().DeleteClient(userId, clientId)
}

Expand Down Expand Up @@ -85,7 +85,7 @@ func (event *Event) Broadcast(channel string, id string, data interface{}) error
return err
}

func (event *Event) Listen(userId uint, clientId uint) chan sse.Event {
func (event *Event) Listen(userId uint, clientId string) chan sse.Event {
return event.GetHub().GetClient(userId, clientId)
}

Expand Down
6 changes: 3 additions & 3 deletions event/basic/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (hub *Hub) GetUser(userId uint) *sync.Map {
return nil
}

func (hub *Hub) GetClient(userId uint, clientId uint) chan sse.Event {
func (hub *Hub) GetClient(userId uint, clientId string) chan sse.Event {
var user = hub.GetUser(userId)
var client interface{}

Expand All @@ -40,7 +40,7 @@ func (hub *Hub) GetClient(userId uint, clientId uint) chan sse.Event {
return client.(chan sse.Event)
}

func (hub *Hub) DeleteClient(userId uint, clientId uint) {
func (hub *Hub) DeleteClient(userId uint, clientId string) {
var user = hub.GetUser(userId)
var client = hub.GetClient(userId, clientId)

Expand All @@ -52,7 +52,7 @@ func (hub *Hub) DeleteClient(userId uint, clientId uint) {
user.Delete(clientId)
}

func (hub *Hub) NewClient(userId uint, clientId uint) {
func (hub *Hub) NewClient(userId uint, clientId string) {
var user, _ = hub.GetList().LoadOrStore(userId, new(sync.Map))
user.(*sync.Map).Store(clientId, make(chan sse.Event))
}
8 changes: 4 additions & 4 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import "github.com/gin-contrib/sse"

type Event interface {
Init() error
NewClientId() uint
NewClientId() string
GetHub() Hub
Subscribe(userId uint, clientId uint)
Unsubscribe(userId uint, clientId uint)
Subscribe(userId uint, clientId string)
Unsubscribe(userId uint, clientId string)
Trigger(userId uint, channel string, id string, data interface{}) error
TriggerError(err error)
Broadcast(channel string, id string, data interface{}) error
Listen(userId uint, clientId uint) chan sse.Event
Listen(userId uint, clientId string) chan sse.Event
ListenError() chan error
}
6 changes: 3 additions & 3 deletions event/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type Hub interface {
GetList() *sync.Map
GetUser(userId uint) *sync.Map
GetClient(userId uint, clientId uint) chan sse.Event
NewClient(userId uint, clientId uint)
DeleteClient(userId uint, clientId uint)
GetClient(userId uint, clientId string) chan sse.Event
NewClient(userId uint, clientId string)
DeleteClient(userId uint, clientId string)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/gin-gonic/gin v1.6.3
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/uuid v1.1.5 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/jaytaylor/html2text v0.0.0-20200412013138-3577fbdbcff7 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.5 h1:kxhtnfFVi+rYdOALN0B3k9UT86zVJKfBimRaciULW4I=
github.com/google/uuid v1.1.5/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
Expand Down

0 comments on commit 1c4336a

Please sign in to comment.