forked from calvertyang/opentok-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.go
98 lines (78 loc) · 2.35 KB
/
signal.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package opentok
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// SignalData defines the type and data of signal
type SignalData struct {
// The type of the signal.
// This is a string value that clients can filter on when listening for signals
Type string `json:"type"`
// The data of the signal
Data string `json:"data"`
}
// SendSessionSignal send signals to all participants in an active OpenTok session.
func (ot *OpenTok) SendSessionSignal(sessionID string, data *SignalData) error {
if sessionID == "" {
return fmt.Errorf("Signal cannot be sent without a session ID")
}
jsonStr, _ := json.Marshal(data)
//Create jwt token
jwt, err := ot.jwtToken(projectToken)
if err != nil {
return err
}
endpoint := ot.apiHost + projectURL + "/" + ot.apiKey + "/session/" + sessionID + "/signal"
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-OPENTOK-AUTH", jwt)
req.Header.Add("User-Agent", SDKName+"/"+SDKVersion)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 204 {
return fmt.Errorf("Tokbox returns error code: %v", res.StatusCode)
}
return nil
}
// SendConnectionSignal send signals to a specific client in an active OpenTok session
func (ot *OpenTok) SendConnectionSignal(sessionID, connectionID string, data *SignalData) error {
if sessionID == "" {
return fmt.Errorf("Signal cannot be sent without a session ID")
}
if connectionID == "" {
return fmt.Errorf("Signal cannot be sent without a connection ID")
}
jsonStr, _ := json.Marshal(data)
//Create jwt token
jwt, err := ot.jwtToken(projectToken)
if err != nil {
return err
}
endpoint := ot.apiHost + projectURL + "/" + ot.apiKey + "/session/" + sessionID + "/connection/" + connectionID + "/signal"
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("X-OPENTOK-AUTH", jwt)
req.Header.Add("User-Agent", SDKName+"/"+SDKVersion)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 204 {
return fmt.Errorf("Tokbox returns error code: %v", res.StatusCode)
}
return nil
}