Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
244 additions
and
369 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package client | ||
|
||
import ( | ||
"github.com/danclive/nson-go" | ||
"github.com/danclive/queen-go/conn" | ||
) | ||
|
||
type SendMessage struct { | ||
Ch string | ||
Body nson.Message | ||
Id *nson.MessageId | ||
Label []string | ||
To []nson.MessageId | ||
Call bool | ||
} | ||
|
||
func NewSendMessage(ch string, body nson.Message) *SendMessage { | ||
return &SendMessage{ | ||
Ch: ch, | ||
Body: body, | ||
} | ||
} | ||
|
||
func (s *SendMessage) WithId(id nson.MessageId) *SendMessage { | ||
s.Id = &id | ||
return s | ||
} | ||
|
||
func (s *SendMessage) WithLabel(label []string) *SendMessage { | ||
s.Label = label | ||
return s | ||
} | ||
|
||
func (s *SendMessage) WithTo(to []nson.MessageId) *SendMessage { | ||
s.To = to | ||
return s | ||
} | ||
|
||
func (s *SendMessage) WithCall(call bool) *SendMessage { | ||
s.Call = call | ||
return s | ||
} | ||
|
||
func (s *SendMessage) Build() nson.Message { | ||
msg := s.Body | ||
|
||
msg.Insert(conn.CHAN, nson.String(s.Ch)) | ||
|
||
if s.Label != nil && len(s.Label) > 0 { | ||
array := make(nson.Array, 0) | ||
for _, v := range s.Label { | ||
array = append(array, nson.String(v)) | ||
} | ||
msg.Insert(conn.LABEL, nson.Array(array)) | ||
} | ||
|
||
if s.To != nil && len(s.To) > 0 { | ||
array := make(nson.Array, 0) | ||
for _, v := range s.To { | ||
array = append(array, v) | ||
} | ||
msg.Insert(conn.TO, nson.Array(array)) | ||
} | ||
|
||
if s.Call { | ||
msg.Insert(conn.SHARE, nson.Bool(true)) | ||
} | ||
|
||
return msg | ||
} | ||
|
||
type RecvMessage struct { | ||
Ch string | ||
Body nson.Message | ||
} | ||
|
||
func (r *RecvMessage) CallId() (nson.MessageId, bool) { | ||
if id, err := r.Body.GetMessageId(CALL_ID); err == nil { | ||
return id, true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
func (r *RecvMessage) FromId() (nson.MessageId, bool) { | ||
if id, err := r.Body.GetMessageId(conn.FROM); err == nil { | ||
return id, true | ||
} | ||
|
||
return nil, false | ||
} | ||
|
||
func (r *RecvMessage) Back() (*SendMessage, bool) { | ||
callId, ok := r.CallId() | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
fromId, ok := r.FromId() | ||
if !ok { | ||
return nil, false | ||
} | ||
|
||
return &SendMessage{ | ||
Ch: r.Ch, | ||
Body: nson.Message{ | ||
CALL_ID: callId, | ||
conn.CODE: nson.I32(0), | ||
}, | ||
To: []nson.MessageId{fromId}, | ||
}, true | ||
} |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module github.com/danclive/queen-go | ||
|
||
go 1.15 | ||
|
||
require ( | ||
github.com/danclive/nson-go v0.3.0 | ||
github.com/stretchr/testify v1.6.1 | ||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a | ||
) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
github.com/danclive/nson-go v0.3.0 h1:skVgeJrbJsQk3HpVOAtZ71QMi+VQuhttgUVsDDMBDAE= | ||
github.com/danclive/nson-go v0.3.0/go.mod h1:L1kLvlMsYMooindbfOBMV33U0S0chCfPyC5g2dimKAQ= | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM= | ||
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |