mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
handle sessions on connected / disconnected.
This commit is contained in:
parent
ccd1598b54
commit
2adb93bc8e
@ -8,6 +8,8 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
oldTypes "github.com/demodesk/neko/internal/http/legacy/types"
|
||||||
|
|
||||||
"github.com/demodesk/neko/internal/api"
|
"github.com/demodesk/neko/internal/api"
|
||||||
"github.com/demodesk/neko/pkg/types"
|
"github.com/demodesk/neko/pkg/types"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@ -20,14 +22,17 @@ type session struct {
|
|||||||
profile types.MemberProfile
|
profile types.MemberProfile
|
||||||
client *http.Client
|
client *http.Client
|
||||||
|
|
||||||
|
sessions map[string]*oldTypes.Member
|
||||||
|
|
||||||
connClient *websocket.Conn
|
connClient *websocket.Conn
|
||||||
connBackend *websocket.Conn
|
connBackend *websocket.Conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSession(url string) *session {
|
func newSession(url string) *session {
|
||||||
return &session{
|
return &session{
|
||||||
url: url,
|
url: url,
|
||||||
client: http.DefaultClient,
|
client: http.DefaultClient,
|
||||||
|
sessions: make(map[string]*oldTypes.Member),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,12 +109,17 @@ func (s *session) wsToClient(msg []byte, sendMsg func([]byte) error) error {
|
|||||||
//
|
//
|
||||||
|
|
||||||
membersList := []*oldTypes.Member{}
|
membersList := []*oldTypes.Member{}
|
||||||
|
s.sessions = map[string]*oldTypes.Member{}
|
||||||
for id, session := range request.Sessions {
|
for id, session := range request.Sessions {
|
||||||
|
if !session.State.IsConnected {
|
||||||
|
continue
|
||||||
|
}
|
||||||
member, err := sessionDataToMember(id, session)
|
member, err := sessionDataToMember(id, session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
membersList = append(membersList, member)
|
membersList = append(membersList, member)
|
||||||
|
s.sessions[id] = member
|
||||||
}
|
}
|
||||||
|
|
||||||
err = send(&oldMessage.MembersList{
|
err = send(&oldMessage.MembersList{
|
||||||
@ -242,7 +247,6 @@ func (s *session) wsToClient(msg []byte, sendMsg func([]byte) error) error {
|
|||||||
|
|
||||||
// Member Events
|
// Member Events
|
||||||
|
|
||||||
// TODO: This is on Created but old API wants OnConnected.
|
|
||||||
case event.SESSION_CREATED:
|
case event.SESSION_CREATED:
|
||||||
request := &message.SessionData{}
|
request := &message.SessionData{}
|
||||||
err := json.Unmarshal(data.Payload, request)
|
err := json.Unmarshal(data.Payload, request)
|
||||||
@ -255,12 +259,11 @@ func (s *session) wsToClient(msg []byte, sendMsg func([]byte) error) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return send(&oldMessage.Member{
|
// only save session - will be notified on connect
|
||||||
Event: oldEvent.MEMBER_CONNECTED,
|
s.sessions[request.ID] = member
|
||||||
Member: member,
|
|
||||||
})
|
return nil
|
||||||
|
|
||||||
// TODO: This is on Deleted but old API wants OnDisconnected.
|
|
||||||
case event.SESSION_DELETED:
|
case event.SESSION_DELETED:
|
||||||
request := &message.SessionID{}
|
request := &message.SessionID{}
|
||||||
err := json.Unmarshal(data.Payload, request)
|
err := json.Unmarshal(data.Payload, request)
|
||||||
@ -268,23 +271,51 @@ func (s *session) wsToClient(msg []byte, sendMsg func([]byte) error) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// only continue if session is in the list - should have been already removed
|
||||||
|
if _, ok := s.sessions[request.ID]; !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
delete(s.sessions, request.ID)
|
||||||
|
|
||||||
return send(&oldMessage.MemberDisconnected{
|
return send(&oldMessage.MemberDisconnected{
|
||||||
Event: oldEvent.MEMBER_DISCONNECTED,
|
Event: oldEvent.MEMBER_DISCONNECTED,
|
||||||
ID: request.ID,
|
ID: request.ID,
|
||||||
})
|
})
|
||||||
|
|
||||||
// TODO: This would need some context to know it message has been sent already/to get session data on connect.
|
case event.SESSION_STATE:
|
||||||
//case event.SESSION_STATE:
|
request := &message.SessionState{}
|
||||||
// request := &message.SessionState{}
|
err := json.Unmarshal(data.Payload, request)
|
||||||
// err := json.Unmarshal(data.Payload, request)
|
if err != nil {
|
||||||
// if err != nil {
|
return err
|
||||||
// return err
|
}
|
||||||
// }
|
|
||||||
// if request.IsConnected {
|
member, ok := s.sessions[request.ID]
|
||||||
// // oldEvent.MEMBER_CONNECTED if not sent already
|
if !ok {
|
||||||
// } else {
|
return nil
|
||||||
// // oldEvent.MEMBER_DISCONNECTED if nor sent already
|
}
|
||||||
// }
|
|
||||||
|
if request.IsConnected && member != nil {
|
||||||
|
s.sessions[request.ID] = nil
|
||||||
|
|
||||||
|
// oldEvent.MEMBER_CONNECTED if not sent already
|
||||||
|
return send(&oldMessage.Member{
|
||||||
|
Event: oldEvent.MEMBER_CONNECTED,
|
||||||
|
Member: member,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !request.IsConnected {
|
||||||
|
delete(s.sessions, request.ID)
|
||||||
|
|
||||||
|
// oldEvent.MEMBER_DISCONNECTED if nor sent already
|
||||||
|
return send(&oldMessage.MemberDisconnected{
|
||||||
|
Event: oldEvent.MEMBER_DISCONNECTED,
|
||||||
|
ID: request.ID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
// Signal Events
|
// Signal Events
|
||||||
case event.SIGNAL_OFFER:
|
case event.SIGNAL_OFFER:
|
||||||
|
Loading…
Reference in New Issue
Block a user