-
Notifications
You must be signed in to change notification settings - Fork 41
/
melody.go
39 lines (32 loc) · 890 Bytes
/
melody.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
package connector
import (
"net/http"
"github.com/TUM-Dev/gocast/tools/realtime"
"github.com/gabstv/melody"
)
func NewMelodyConnector() *realtime.Connector {
melodyInstance := melody.New()
connector := realtime.NewConnector(
func(writer http.ResponseWriter, request *http.Request, properties map[string]interface{}) error {
return melodyInstance.HandleRequestWithKeys(writer, request, properties)
},
)
melodyInstance.HandleConnect(func(s *melody.Session) {
client := connector.Join(
func(message []byte) error {
return s.Write(message)
},
s.Keys(),
)
s.Set("id", client.Id)
})
melodyInstance.HandleDisconnect(func(s *melody.Session) {
id, _ := s.Get("id")
connector.Leave(id.(string))
})
melodyInstance.HandleMessage(func(s *melody.Session, data []byte) {
id, _ := s.Get("id")
connector.Message(id.(string), data)
})
return connector
}