-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebsockets.go
45 lines (39 loc) · 1.13 KB
/
websockets.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
package main
import (
"fmt"
"log"
"time"
"golang.org/x/net/websocket"
)
const (
websocketPath = "websocket"
)
type SocketEvent struct {
Data HookMsg `json:"data"`
Signature string `json:"signature,omitempty"`
}
// json that is sent back to the websocket client open connection
type SocketResponse struct {
Url string `json:"url"`
Secret string `json:"secret,omitempty"`
}
// wsServe gemerates a private webhook endpoint for each incoming websocket
// The websocket is kept open so incoming webhook data can be proxied to it
func wsServe(c *websocket.Conn) {
defer c.Close()
id := NewUid()
secret := []byte(NewUid())
setSession(id, Session{c, secret})
defer deleteSession(id)
// send private webhook endpoint to client
data := SocketResponse{
Url: fmt.Sprintf("%s://%s:%d/%s/%s", scheme, config.Host, config.Port, webhooksPath, id),
Secret: string(secret),
}
log.Printf("Incoming websocket from %s, sending: %+v\n", c.Request().RemoteAddr, data)
websocket.JSON.Send(c, data)
// read forever on websocket to keep it open
var msg []byte
for _, err := c.Read(msg); err == nil; time.Sleep(1 * time.Second) {
}
}