Skip to content

Commit b22d45b

Browse files
committed
change ws method
1 parent 354c52b commit b22d45b

File tree

5 files changed

+53
-10
lines changed

5 files changed

+53
-10
lines changed

client.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,6 @@ func (c *UtopiaClient) CreateUUSDVoucher(amount float64) (string, error) {
105105
return c.createCoinVoucher(amount, "UUSD")
106106
}
107107

108-
type SetWsStateTask struct {
109-
Enabled bool `json:"enabled"`
110-
Port int `json:"port"`
111-
EnableSSL bool `json:"enablessl"`
112-
Notifications string `json:"notifications"` // example: "contact, wallet" example2: "all"
113-
}
114-
115108
// SetWebSocketState - set WSS Notification state
116109
func (c *UtopiaClient) SetWebSocketState(task SetWsStateTask) error {
117110
params := map[string]interface{}{

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/Sagleft/utopialib-go
33
go 1.16
44

55
require (
6+
github.com/rgamba/evtwebsocket v0.0.0-20181029234908-48b8cd9f8616 // indirect
67
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23 // indirect
8+
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
79
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df
810
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
22
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
3+
github.com/rgamba/evtwebsocket v0.0.0-20181029234908-48b8cd9f8616 h1:CLOrtba3mh42XMLPr+g89SxpQ7PcdsCQEICO655IsxI=
4+
github.com/rgamba/evtwebsocket v0.0.0-20181029234908-48b8cd9f8616/go.mod h1:fpClUynpFcOmPLy6rXe0pEIoDamYH5s5I0SrbDbF4gk=
35
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d h1:5T+fbRuQbpi+WZtB2yfuu59r00F6T2HV/zGYrwX8nvE=
46
github.com/sacOO7/go-logger v0.0.0-20180719173527-9ac9add5a50d/go.mod h1:L5EJe2k8GwpBoGXDRLAEs58R239jpZuE7NNEtW+T7oo=
57
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23 h1:yjnkNJTpQPCx10KF1jypuIhAVc6EYn2M9lJgwSTHQYs=
68
github.com/sacOO7/gowebsocket v0.0.0-20210515122958-9396f1a71e23/go.mod h1:h00QywbM5Le22ESUiI8Yz2/9TVGD8eAz/cAk55Kcz/E=
9+
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
10+
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
11+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
13+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
14+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
15+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
716
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df h1:SCh+kVSG+MN/pU/l0/3ehkc3i9T+G6AQS6jEoxM4ddY=
817
gopkg.in/grignaak/tribool.v1 v1.0.0-20150312065122-d6bb19d816df/go.mod h1:ikoZVciJt+u3It4kEk89OVqIKHPU9BjrgkHSr++TA2w=

structs.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ type WsEvent struct {
2121
Type string `json:"type"`
2222
Data map[string]interface{} `json:"data"`
2323
}
24+
25+
type SetWsStateTask struct {
26+
Enabled bool `json:"enabled"`
27+
Port int `json:"port"`
28+
EnableSSL bool `json:"enablessl"`
29+
Notifications string `json:"notifications"` // example: "contact, wallet" example2: "all"
30+
}

ws.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package utopiago
33
import (
44
"encoding/json"
55
"errors"
6+
"fmt"
7+
"log"
68
"os"
7-
"os/signal"
89
"reflect"
910

10-
"github.com/sacOO7/gowebsocket"
11+
"github.com/rgamba/evtwebsocket"
1112
)
1213

1314
// GetString - get string field from ws event.
@@ -110,7 +111,7 @@ func newWsEvent(jsonRaw string) (WsEvent, error) {
110111

111112
// WsSubscribe - connect to websocket & recive messages.
112113
// NOTE: it's blocking method
113-
func (c *UtopiaClient) WsSubscribe(task WsSubscribeTask) error {
114+
/*func (c *UtopiaClient) WsSubscribe(task WsSubscribeTask) error {
114115
interrupt := make(chan os.Signal, 1)
115116
signal.Notify(interrupt, os.Interrupt)
116117
@@ -137,5 +138,36 @@ func (c *UtopiaClient) WsSubscribe(task WsSubscribeTask) error {
137138
// wait for close
138139
<-interrupt
139140
socket.Close()
141+
return nil
142+
}*/
143+
144+
// WsSubscribe - connect to websocket & recive messages.
145+
// NOTE: it's blocking method
146+
func (c *UtopiaClient) WsSubscribe(task WsSubscribeTask) error {
147+
conn := evtwebsocket.Conn{
148+
// Fires when the connection is established
149+
OnConnected: func(w *evtwebsocket.Conn) {
150+
fmt.Println("Connected!")
151+
},
152+
// Fires when a new message arrives from the server
153+
OnMessage: func(msg []byte, w *evtwebsocket.Conn) {
154+
fmt.Printf("New message: %s\n", msg)
155+
},
156+
// Fires when an error occurs and connection is closed
157+
OnError: func(err error) {
158+
fmt.Printf("Error: %s\n", err.Error())
159+
os.Exit(1)
160+
},
161+
// Ping interval in secs (optional)
162+
PingIntervalSecs: 5,
163+
// Ping message to send (optional)
164+
PingMsg: []byte("PING"),
165+
}
166+
167+
err := conn.Dial(c.getBaseURL(), "")
168+
if err != nil {
169+
log.Fatal(err)
170+
}
171+
140172
return nil
141173
}

0 commit comments

Comments
 (0)