-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_webconnect.go
More file actions
118 lines (102 loc) · 2.86 KB
/
api_webconnect.go
File metadata and controls
118 lines (102 loc) · 2.86 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"math/rand"
"net/http"
"regexp"
"api.arfevrier.fr/v3/signal"
"api.arfevrier.fr/v3/webconnect"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
)
// > Input
type urlChannel struct {
Channel string `uri:"channel" binding:"required"`
}
// newWS godoc
// @Summary Create a new WebSocket connection
// @Schemes
// @Description Create a new WebSocket connection for a given channel
// @Tags webconnect
// @Accept json
// @Produce json
// @Param channel path string true "Channel"
// @Param localdesc query string false "Local description for WebRTC"
// @Success 200 {string} string "WebSocket connection established"
// @Failure 400 {string} string "Invalid input"
// @Router /webconnect/new/{channel} [get]
func (Api API) newWS(c *gin.Context) {
var urlChannel urlChannel
// Get the token from REST url
if err := c.ShouldBindUri(&urlChannel); err != nil {
c.JSON(400, gin.H{"msg": err})
return
}
// Validate input type
r, err := regexp.Compile(`^.*$`)
if err != nil || !r.MatchString(urlChannel.Channel) {
c.JSON(400, gin.H{"msg": "Invalid input"})
return
}
if localDesc, ok := c.GetQuery("localdesc"); ok {
newRtc := webconnect.NewRtc(hub, localDesc)
hub.RegisterRtc <- newRtc
c.JSON(200, gin.H{"websocket": fmt.Sprintf("%d", rand.Intn(1000)), "webrtc": signal.Encode(newRtc.Conn.LocalDescription())})
return
}
c.JSON(200, gin.H{"websocket": "withoutRTC"})
}
// > Var
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
// > Input
type urlId struct {
Id string `uri:"id" binding:"required"`
}
// connectWS godoc
// @Summary Connect to an existing WebSocket
// @Schemes
// @Description Connect to an existing WebSocket by ID
// @Tags webconnect
// @Accept json
// @Produce json
// @Param id path string true "WebSocket ID"
// @Success 200 {string} string "Connection established"
// @Failure 400 {string} string "Invalid input"
// @Router /webconnect/connect/{id} [get]
func (Api API) connectWS(c *gin.Context) {
var urlId urlId
// Get the token from REST url
if err := c.ShouldBindUri(&urlId); err != nil {
c.JSON(400, gin.H{"msg": err})
return
}
// Validate input type
r, err := regexp.Compile(`^.*$`)
if err != nil || !r.MatchString(urlId.Id) {
c.JSON(400, gin.H{"msg": "Invalid input"})
return
}
// --- Start websocket connection upgrade
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
fmt.Println(err)
return
}
defer conn.Close()
// Disconnect if client already connected
for client := range hub.Clients {
fmt.Printf("|> Websocket: Current client ID: %s\n", client.Id)
if client.Id == urlId.Id {
return
}
}
// Add new client to the hub and run
newClient := webconnect.NewClient(urlId.Id, hub, conn)
hub.Register <- newClient
newClient.Run()
hub.Unregister <- newClient
}