-
Notifications
You must be signed in to change notification settings - Fork 1
Add websocket #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,58 @@ | ||
| package sseClients | ||
| package clients | ||
|
|
||
| import ( | ||
| "sync" | ||
|
|
||
| uuid "github.com/satori/go.uuid" | ||
| ) | ||
|
|
||
| type SseCLients struct { | ||
| // clients map[*SseClient]bool | ||
| type Clients struct { | ||
| // clients map[*Client]bool | ||
| clients sync.Map | ||
| } | ||
|
|
||
| type SseClientData map[string]interface{} | ||
| type ClientData map[string]interface{} | ||
| type ClientType uint8 | ||
|
|
||
| type SseClient struct { | ||
| Id string | ||
| Data SseClientData | ||
| const ( | ||
| SseClient ClientType = 1 | ||
| WsClient ClientType = 2 | ||
| ) | ||
|
|
||
| func (ct ClientType) IsValid() bool { | ||
| switch ct { | ||
| case SseClient, WsClient: | ||
| return true | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| type Client struct { | ||
| Id string | ||
| Data ClientData | ||
| clientType ClientType | ||
| } | ||
|
|
||
| func (m *SseCLients) RegisterNewCLient() *SseClient { | ||
| func (m *Clients) RegisterNewClient(clientType ClientType) *Client { | ||
|
|
||
| var err error | ||
| u1 := uuid.Must(uuid.NewV4(), err) | ||
|
|
||
| cl := SseClient{Id: u1.String(), Data: nil} | ||
| if !clientType.IsValid() { | ||
| panic("client type not handled") | ||
| } | ||
|
|
||
| cl := Client{Id: u1.String(), Data: nil, clientType: clientType} | ||
|
|
||
| return &cl | ||
| } | ||
|
|
||
| // func (m *SseCLients) GetClientsList() map[*SseClient]bool { | ||
| // func (m *Clients) GetClientsList() map[*Client]bool { | ||
|
|
||
| // return m.clients | ||
| // } | ||
|
|
||
| func (m *SseCLients) GetRegisteredClients() int { | ||
| func (m *Clients) GetRegisteredClients() int { | ||
|
|
||
| cnt := 0 | ||
| m.clients.Range(func(key, value interface{}) bool { | ||
|
|
@@ -45,12 +64,16 @@ func (m *SseCLients) GetRegisteredClients() int { | |
|
|
||
| } | ||
|
|
||
| func (m *SseCLients) RegisterNewCLientWithData(data SseClientData) *SseClient { | ||
| func (m *Clients) RegisterNewClientWithData(clientType ClientType, data ClientData) *Client { | ||
|
|
||
| var err error | ||
| u1 := uuid.Must(uuid.NewV4(), err) | ||
|
|
||
| cl := SseClient{Id: u1.String(), Data: data} | ||
| if !clientType.IsValid() { | ||
| panic("client type not handled") | ||
| } | ||
|
|
||
| cl := Client{Id: u1.String(), Data: data, clientType: clientType} | ||
|
|
||
| //func (m *Map) Store(key, value interface{}) | ||
| // Key and value are interface types, which can store data of any type | ||
|
|
@@ -59,14 +82,14 @@ func (m *SseCLients) RegisterNewCLientWithData(data SseClientData) *SseClient { | |
| return &cl | ||
| } | ||
|
|
||
| func New() *SseCLients { | ||
| func New() *Clients { | ||
|
|
||
| manager := new(SseCLients) | ||
| manager := new(Clients) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can avoid the new c := Clients{} return &c |
||
|
|
||
| return manager | ||
| } | ||
|
|
||
| func (manager *SseCLients) SetDisconnected(conn *SseClient) { | ||
| func (manager *Clients) SetDisconnected(conn *Client) { | ||
| // func (m *Map) Load(key interface{}) (value interface{}, ok bool) | ||
| // Key: key, value: if the data exists, the corresponding value will be returned; if not, nil will be returned. ok: indicates whether the value is found | ||
| // Remember to process data assertions | ||
|
|
@@ -77,13 +100,13 @@ func (manager *SseCLients) SetDisconnected(conn *SseClient) { | |
|
|
||
| } | ||
|
|
||
| func (manager *SseCLients) RemoveCLient(conn *SseClient) { | ||
| func (manager *Clients) RemoveClient(conn *Client) { | ||
|
|
||
| manager.clients.Delete(conn) | ||
|
|
||
| } | ||
|
|
||
| func (m *SseCLients) PurgeDisconnected() { | ||
| func (m *Clients) PurgeDisconnected() { | ||
|
|
||
| //func (m *Map) Range(f func(key, value interface{}) bool) | ||
| // The callback function used by the user to process data. The parameters of the callback function are key, value and the return value is bool | ||
|
|
@@ -97,11 +120,11 @@ func (m *SseCLients) PurgeDisconnected() { | |
|
|
||
| } | ||
|
|
||
| func (m *SseCLients) FuncIterationForClients(fn func(client *SseClient, isConnected bool) bool) bool { | ||
| func (m *Clients) FuncIterationForClients(fn func(client *Client, isConnected bool) bool) bool { | ||
|
|
||
| m.clients.Range(func(key, value interface{}) bool { | ||
| if value == true { | ||
| if ok := fn(key.(*SseClient), value.(bool)); ok == false { | ||
| if ok := fn(key.(*Client), value.(bool)); ok == false { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add in the main the flags to setup this features.