-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
62 lines (50 loc) · 1.22 KB
/
client.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package osc
import (
"fmt"
"net"
)
// Client enables you to send OSC packets. It sends OSC messages and bundles to
// the given IP address and port.
type Client struct {
IP string
Port int
laddr *net.UDPAddr
}
// NewClient creates a new OSC client. The Client is used to send OSC
// messages and OSC bundles over an UDP network connection. The `ip` argument
// specifies the IP address and `port` defines the target port where the
// messages and bundles will be send to.
func NewClient(ip string, port int) *Client {
return &Client{
IP: ip,
Port: port,
laddr: nil,
}
}
// SetLocalAddr sets the local address.
func (c *Client) SetLocalAddr(ip string, port int) error {
laddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ip, port))
if err != nil {
return err
}
c.laddr = laddr
return nil
}
// Send sends an OSC Bundle or an OSC Message.
func (c *Client) Send(packet Packet) error {
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", c.IP, c.Port))
if err != nil {
return err
}
conn, err := net.DialUDP("udp", c.laddr, addr)
if err != nil {
return err
}
defer conn.Close()
data, err := packet.MarshalBinary()
if err != nil {
return err
}
_, err = conn.Write(data)
return err
}