-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathnatsclient.go
34 lines (29 loc) · 971 Bytes
/
natsclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package front
import (
"context"
"fmt"
"github.com/maxence-charriere/go-app/v10/pkg/app"
"github.com/nats-io/nats.go"
"net"
"nhooyr.io/websocket"
"time"
)
var _ nats.CustomDialer = (*WasmNatsConnectionWrapper)(nil) // Verify the implementation
func (cw WasmNatsConnectionWrapper) Dial(network, address string) (net.Conn, error) {
// we actually do not care about the adress given here
app.Logf("Got Request for Network: %q / Address: %q", network, address)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
app.Logf("Dialing Address: ws://%s", address)
c, _, err := websocket.Dial(ctx, "ws://"+address, nil)
if err != nil {
app.Logf("websocket.Dial failed %#v", err)
return nil, fmt.Errorf("websocket.Dial failed %w", err)
}
cw.ws = c
nconn := websocket.NetConn(context.Background(), c, websocket.MessageBinary)
return nconn, nil
}
func (cw WasmNatsConnectionWrapper) SkipTLSHandshake() bool {
return true
}