aboutsummaryrefslogtreecommitdiff
path: root/go/utils/ws_wasm.go
blob: 562f67918e9371b1a3dd64c8b1e29168130ba9f8 (plain)
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
package utils

import (
	"net"
	"encoding/json"
	"context"
	"github.com/coder/websocket"
)

func WsDial(addr string) (c net.Conn, err error) {
	ctx := context.Background()
	ws, _, err := websocket.Dial(ctx, addr, nil)
	if err != nil {
		return nil, err
	}
	c = websocket.NetConn(ctx, ws, websocket.MessageText)
	return
}

func WsJsonSend(c net.Conn, o any) (err error) {
	enc := json.NewEncoder(c)
	err = enc.Encode(o)
	return
}

func WsJsonRecv(c net.Conn, o any) (err error) {
	dec := json.NewDecoder(c)
	err = dec.Decode(o)
	return
}