Skip to content

Commit

Permalink
Merge pull request #1 from Sagleft/update-ws
Browse files Browse the repository at this point in the history
Update ws
  • Loading branch information
Sagleft authored May 28, 2022
2 parents 354c52b + d359129 commit da5ad81
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 43 deletions.
7 changes: 0 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,6 @@ func (c *UtopiaClient) CreateUUSDVoucher(amount float64) (string, error) {
return c.createCoinVoucher(amount, "UUSD")
}

type SetWsStateTask struct {
Enabled bool `json:"enabled"`
Port int `json:"port"`
EnableSSL bool `json:"enablessl"`
Notifications string `json:"notifications"` // example: "contact, wallet" example2: "all"
}

// SetWebSocketState - set WSS Notification state
func (c *UtopiaClient) SetWebSocketState(task SetWsStateTask) error {
params := map[string]interface{}{
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/Sagleft/utopialib-go
go 1.16

require (
github.com/rgamba/evtwebsocket v0.0.0-20181029234908-48b8cd9f8616 // indirect
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23 // indirect
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/rgamba/evtwebsocket v0.0.0-20181029234908-48b8cd9f8616 h1:CLOrtba3mh42XMLPr+g89SxpQ7PcdsCQEICO655IsxI=
github.com/rgamba/evtwebsocket v0.0.0-20181029234908-48b8cd9f8616/go.mod h1:fpClUynpFcOmPLy6rXe0pEIoDamYH5s5I0SrbDbF4gk=
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d h1:5T+fbRuQbpi+WZtB2yfuu59r00F6T2HV/zGYrwX8nvE=
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d/go.mod h1:L5EJe2k8GwpBoGXDRLAEs58R239jpZuE7NNEtW+T7oo=
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23 h1:yjnkNJTpQPCx10KF1jypuIhAVc6EYn2M9lJgwSTHQYs=
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23/go.mod h1:h00QywbM5Le22ESUiI8Yz2/9TVGD8eAz/cAk55Kcz/E=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df h1:SCh+kVSG+MN/pU/l0/3ehkc3i9T+G6AQS6jEoxM4ddY=
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df/go.mod h1:ikoZVciJt+u3It4kEk89OVqIKHPU9BjrgkHSr++TA2w=
5 changes: 5 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ func (c *UtopiaClient) getBaseURLWithoutProtocol() string {
return c.Host + ":" + strconv.Itoa(c.Port) + "/api/1.0/"
}

// get ws API url
func (c *UtopiaClient) getWsURL() string {
return "ws://" + c.Host + ":" + strconv.Itoa(c.WsPort) + "/UtopiaWSS?token=" + c.Token
}

func (c *UtopiaClient) apiQuery(methodName string, params map[string]interface{}) (map[string]interface{}, error) {
var responseMap map[string]interface{}
var query = Query{
Expand Down
22 changes: 22 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,25 @@ type WsEvent struct {
Type string `json:"type"`
Data map[string]interface{} `json:"data"`
}

type SetWsStateTask struct {
Enabled bool `json:"enabled"`
Port int `json:"port"`
EnableSSL bool `json:"enablessl"`
Notifications string `json:"notifications"` // example: "contact, wallet" example2: "all"
}

type WsEventsCallback func(ws WsEvent)

type WsErrorCallback func(err error)

type WsSubscribeTask struct {
// required
OnConnected func() // required
Callback WsEventsCallback // required
ErrCallback WsErrorCallback // required
Port int

// optional
DisablePing bool
}
61 changes: 25 additions & 36 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package utopiago
import (
"encoding/json"
"errors"
"os"
"os/signal"
"reflect"

"github.com/sacOO7/gowebsocket"
"github.com/rgamba/evtwebsocket"
)

// GetString - get string field from ws event.
Expand Down Expand Up @@ -90,18 +88,9 @@ func (ws *WsEvent) GetFloat(field string) (float64, error) {
return val, nil
}

type WsEventsCallback func(ws WsEvent)

type WsErrorCallback func(err error)

type WsSubscribeTask struct {
Callback WsEventsCallback // required
ErrCallback WsErrorCallback // required
}

func newWsEvent(jsonRaw string) (WsEvent, error) {
func newWsEvent(jsonRaw []byte) (WsEvent, error) {
event := WsEvent{}
err := json.Unmarshal([]byte(jsonRaw), &event)
err := json.Unmarshal(jsonRaw, &event)
if err != nil {
return event, errors.New("failed to decode event json: " + err.Error())
}
Expand All @@ -111,31 +100,31 @@ func newWsEvent(jsonRaw string) (WsEvent, error) {
// WsSubscribe - connect to websocket & recive messages.
// NOTE: it's blocking method
func (c *UtopiaClient) WsSubscribe(task WsSubscribeTask) error {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)

// create ws
socket := gowebsocket.New("ws://" + c.getBaseURLWithoutProtocol())

// setup callbacks
socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
event, err := newWsEvent(message)
if err != nil {
conn := evtwebsocket.Conn{
// Fires when the connection is established
OnConnected: func(w *evtwebsocket.Conn) {
task.OnConnected()
},
// Fires when a new message arrives from the server
OnMessage: func(msg []byte, w *evtwebsocket.Conn) {
event, err := newWsEvent(msg)
if err != nil {
task.ErrCallback(err)
} else {
task.Callback(event)
}
},
// Fires when an error occurs and connection is closed
OnError: func(err error) {
task.ErrCallback(err)
} else {
task.Callback(event)
}
},
}

socket.OnDisconnected = func(err error, socket gowebsocket.Socket) {
task.ErrCallback(err)
if !task.DisablePing {
conn.PingIntervalSecs = 5 // ping interval in seconds
conn.PingMsg = []byte("PING") // ping message to send
}

// connect
socket.Connect()

// wait for close
<-interrupt
socket.Close()
return nil
// open connection
return conn.Dial(c.getWsURL(), "")
}

0 comments on commit da5ad81

Please sign in to comment.