From 7efbe6725610e3799c7a4186985c7a22e0cc3834 Mon Sep 17 00:00:00 2001 From: "mohammadreza.modares" Date: Tue, 26 Oct 2021 12:12:43 +0330 Subject: [PATCH 1/2] Add claim method to gateway The claim method is used for recreating a session so that it has not expired yet. --- janus.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/janus.go b/janus.go index 09d4cc0..ca75f27 100644 --- a/janus.go +++ b/janus.go @@ -5,6 +5,7 @@ package janus import ( "bytes" "encoding/json" + "errors" "fmt" "log" "os" @@ -294,6 +295,52 @@ func (gateway *Gateway) Create() (*Session, error) { return session, nil } +// Claim sends a claim request to the Gateway. +// On success, the Session will be recreated and error will be nil. +func (gateway *Gateway) Claim(sessionID uint64, handleIDs []uint64) (*Session, error) { + if gateway.Sessions[sessionID] != nil { + return nil, errors.New("the session is exist already") + } + + req, ch := newRequest("claim") + req["session_id"] = sessionID + gateway.send(req, ch) + + msg := <-ch + var success *SuccessMsg + switch msg := msg.(type) { + case *SuccessMsg: + success = msg + case *ErrorMsg: + return nil, msg + } + + // Create new session + session := new(Session) + session.gateway = gateway + session.ID = success.Data.ID + session.Handles = make(map[uint64]*Handle) + session.Events = make(chan interface{}, 2) + + // Store this session + gateway.Lock() + gateway.Sessions[session.ID] = session + gateway.Unlock() + + for _, hid := range handleIDs { + handle := &Handle{ + ID: hid, + Events: make(chan interface{}), + session: session, + } + gateway.Lock() + session.Handles[handle.ID] = handle + gateway.Unlock() + } + + return session, nil +} + // Session represents a session instance on the Janus Gateway. type Session struct { // ID is the session_id of this session From 2ac2b5da42c01fca333c1565ffdc39caefa35efe Mon Sep 17 00:00:00 2001 From: "mohammadreza.modares" Date: Wed, 3 Nov 2021 12:00:01 +0330 Subject: [PATCH 2/2] Use proper field for setting session id in claim method --- janus.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/janus.go b/janus.go index ca75f27..1fe2c42 100644 --- a/janus.go +++ b/janus.go @@ -318,7 +318,7 @@ func (gateway *Gateway) Claim(sessionID uint64, handleIDs []uint64) (*Session, e // Create new session session := new(Session) session.gateway = gateway - session.ID = success.Data.ID + session.ID = success.Session session.Handles = make(map[uint64]*Handle) session.Events = make(chan interface{}, 2)