allow sending

This commit is contained in:
Zack Scholl 2019-04-29 19:19:48 -07:00
parent 9223fc79e9
commit 662bce58a3
1 changed files with 19 additions and 0 deletions

View File

@ -5,8 +5,10 @@ import (
"github.com/schollz/croc/v6/src/compress"
"github.com/schollz/croc/v6/src/crypt"
"github.com/schollz/croc/v6/src/comm"
)
// Message is the possible payload for messaging
type Message struct {
Type string `json:"t,omitempty"`
Message string `json:"m,omitempty"`
@ -14,6 +16,22 @@ type Message struct {
Num int `json:"n,omitempty"`
}
func (m Message) String() string {
b, _ := json.Marshal(m)
return string(b)
}
// Send will send out
func Send(c *comm.Comm, m Message) (err error) {
mSend, err := Encode(m)
if err != nil {
return
}
_, err = c.Write(mSend)
return
}
// Encode will convert to bytes
func Encode(m Message, e ...crypt.Encryption) (b []byte, err error) {
b, err = json.Marshal(m)
if err != nil {
@ -27,6 +45,7 @@ func Encode(m Message, e ...crypt.Encryption) (b []byte, err error) {
return
}
// Decode will convert from bytes
func Decode(b []byte, e ...crypt.Encryption) (m Message, err error) {
if len(e) > 0 {
b, err = e[0].Decrypt(b)