implemented receive endpoint via websocket connection

This commit is contained in:
Bernhard B
2021-09-13 22:09:33 +02:00
parent d8a5ddfc98
commit 760883bdca
5 changed files with 133 additions and 38 deletions

View File

@@ -297,6 +297,10 @@ func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpD
}
}
func (s *SignalClient) GetSignalCliMode() SignalCliMode {
return s.signalCliMode
}
func (s *SignalClient) Init() error {
if s.signalCliMode == JsonRpc {
s.jsonRpc2ClientConfig = utils.NewJsonRpc2ClientConfig()
@@ -312,6 +316,8 @@ func (s *SignalClient) Init() error {
if err != nil {
return err
}
go s.jsonRpc2Clients[number].ReceiveData() //receive messages in goroutine
}
}
return nil
@@ -542,8 +548,16 @@ func (s *SignalClient) SendV2(number string, message string, recps []string, bas
}
func (s *SignalClient) Receive(number string, timeout int64) (string, error) {
if s.signalCliMode == Native {
return "", errors.New(endpointNotSupportedInJsonRpcMode)
if s.signalCliMode == JsonRpc {
jsonRpc2Client, err := s.getJsonRpc2Client(number)
if err != nil {
return "", err
}
msg := jsonRpc2Client.ReceiveMessage()
if msg.Err.Code != 0 {
return "", errors.New(msg.Err.Message)
}
return string(msg.Params), nil
} else {
command := []string{"--config", s.signalCliConfig, "--output", "json", "-u", number, "receive", "-t", strconv.FormatInt(timeout, 10)}

View File

@@ -9,8 +9,27 @@ import (
"net"
)
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}
type JsonRpc2MessageResponse struct {
Id string `json:"id"`
Result json.RawMessage `json:"result"`
Err Error `json:"error"`
}
type JsonRpc2ReceivedMessage struct {
Method string `json:"method"`
Params json.RawMessage `json:"params"`
Err Error `json:"error"`
}
type JsonRpc2Client struct {
conn net.Conn
conn net.Conn
receivedMessageResponses chan JsonRpc2MessageResponse
receivedMessages chan JsonRpc2ReceivedMessage
}
func NewJsonRpc2Client() *JsonRpc2Client {
@@ -24,6 +43,9 @@ func (r *JsonRpc2Client) Dial(address string) error {
return err
}
r.receivedMessageResponses = make(chan JsonRpc2MessageResponse)
r.receivedMessages = make(chan JsonRpc2ReceivedMessage)
return nil
}
@@ -35,17 +57,6 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error
Params interface{} `json:"params,omitempty"`
}
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}
type Response struct {
Id string `json:"id"`
Result json.RawMessage `json:"result"`
Err Error `json:"error"`
}
u, err := uuid.NewV4()
if err != nil {
return "", err
@@ -68,27 +79,56 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error
return "", err
}
var resp JsonRpc2MessageResponse
for {
resp = <-r.receivedMessageResponses
if resp.Id == u.String() {
break
}
}
if resp.Err.Code != 0 {
return "", errors.New(resp.Err.Message)
}
return string(resp.Result), nil
}
func (r *JsonRpc2Client) ReceiveData() {
connbuf := bufio.NewReader(r.conn)
for {
str, err := connbuf.ReadString('\n')
if err != nil {
return "", err
log.Error("Couldn't read data: ", err.Error())
continue
}
//log.Info("Received data = ", str)
var resp1 JsonRpc2ReceivedMessage
json.Unmarshal([]byte(str), &resp1)
if resp1.Method == "receive" {
select {
case r.receivedMessages <- resp1:
log.Debug("Message sent to golang channel")
default:
log.Debug("Couldn't send message to golang channel, as there's no receiver")
}
continue
}
var resp Response
err = json.Unmarshal([]byte(str), &resp)
var resp2 JsonRpc2MessageResponse
err = json.Unmarshal([]byte(str), &resp2)
if err == nil {
if resp.Id == u.String() {
log.Info("Response1 = ", string(resp.Result))
if resp.Err.Code != 0 {
return "", errors.New(resp.Err.Message)
}
return string(resp.Result), nil
if resp2.Id != "" {
r.receivedMessageResponses <- resp2
}
} else {
log.Info("Response = ", str)
log.Error("Received unparsable message: ", str)
}
}
return "", errors.New("no data")
}
//blocks until message a message is received
func (r *JsonRpc2Client) ReceiveMessage() JsonRpc2ReceivedMessage {
resp := <-r.receivedMessages
return resp
}