From 3ad37a481012b8445faa2e3a9a104a35ff9fc4ee Mon Sep 17 00:00:00 2001 From: Geno Date: Tue, 24 Nov 2020 23:29:53 +0100 Subject: [PATCH] a other socket (webrtc-sfu is just for ingress?) --- main.go | 79 +++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 10 deletions(-) diff --git a/main.go b/main.go index 2de8191..2a189a9 100644 --- a/main.go +++ b/main.go @@ -2,35 +2,41 @@ package main import ( "fmt" - "log" "net/http" "net/url" "os" + "time" + + "github.com/bdlm/log" "github.com/PuerkitoBio/goquery" + // "github.com/gorilla/websocket" + "dev.sum7.eu/genofire/golang-lib/worker" + "golang.org/x/net/websocket" ) -func main() { - bbbName := "bbbBot" - bbbURL, _ := url.Parse(os.Args[1]) +type Message struct { + ID string `json:"id"` +} +func fetchRoomURL(bbbURL *url.URL, bbbName string) (string, *url.URL, error) { res, err := http.Get(bbbURL.String()) if err != nil { - log.Fatal(err) + return "", nil, err } defer res.Body.Close() if res.StatusCode != 200 { - log.Fatalf("status code error: %d %s", res.StatusCode, res.Status) + return "", nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status) } doc, err := goquery.NewDocumentFromReader(res.Body) if err != nil { - log.Fatal(err) + return "", nil, err } form := doc.Find("form") authToken, ok := form.Find("input[name='authenticity_token']").Attr("value") if !ok { - log.Fatalln("'authenticity_token' not found") + return "", nil, fmt.Errorf("'authenticity_token' not found") } res.Body.Close() // ---- @@ -40,10 +46,63 @@ func main() { }) //defer res.Body.Close() if res.StatusCode != 200 { - log.Fatalf("status code error: %d %s", res.StatusCode, res.Status) + return "", nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status) } - log.Printf("%v", res.Request.Header.Get("Referer")) + token := res.Request.URL.Query().Get("sessionToken") + u, err := url.Parse(res.Request.Header.Get("Referer")) + return token, u, err +} +func main() { + log.SetLevel(log.DebugLevel) + bbbName := "bbbBot" + bbbURL, _ := url.Parse(os.Args[1]) + + token, u, err := fetchRoomURL(bbbURL, bbbName) + if err != nil { + log.Fatal(err) + } + log.WithField("url", u).Debug("join url") + + ws, err := websocket.Dial(fmt.Sprintf("wss://%s/sockjs/...", u.Host), "", u.String()) + //ws, err := websocket.Dial(fmt.Sprintf("wss://%s/bbb-webrtc-sfu?sessionToken=%s", u.Host, token), "", u.String()) + if err != nil { + log.Fatal(err) + } + log.Info("websocket established") + + wPing := worker.NewWorker(time.Duration(15)*time.Second, func() { + if err := websocket.JSON.Send(ws, Message{ID: "ping"}); err != nil { + log.Fatal(err) + } + log.WithField("id", "ping").Debug("sended") + }) + wPing.Start() + + if err := websocket.JSON.Send(ws, map[string]interface{}{ + "id": "start", + "type": "send", + "role": "", + "internalMeetingId": u.Query().Get("meetingID"), + "voiceBridge": "", + "callerName": u.Query().Get("userID"), + "sdpOffer": "", + "vh": "", + "vw": "", + "userName": bbbName, + }); err != nil { + log.Fatal(err) + } + log.WithField("id", "start").Debug("sended") + + for { + + var msgJSON interface{} + if err = websocket.JSON.Receive(ws, &msgJSON); err != nil { + log.Fatal(err) + } + log.WithField("msg", msgJSON).Debug("received") + } /* not needed ... websocket doc, err = goquery.NewDocumentFromReader(res.Body) if err != nil {