Properly close share sessions

This commit is contained in:
Jannis Mattheis
2020-10-09 13:46:37 +00:00
parent a02e885370
commit 7efeccad52
5 changed files with 57 additions and 1 deletions
+4
View File
@@ -77,8 +77,10 @@ export type ClientICECandidate = Typed<P2PMessage<RTCIceCandidate>, 'clientice'>
export type HostOffer = Typed<P2PMessage<RTCSessionDescriptionInit>, 'hostoffer'>;
export type ClientAnswer = Typed<P2PMessage<RTCSessionDescriptionInit>, 'clientanswer'>;
export type StartSharing = Typed<{}, 'share'>;
export type StopShare = Typed<{}, 'stopshare'>;
export type RoomCreate = Typed<RoomConfiguration, 'create'>;
export type JoinRoom = Typed<JoinConfiguration, 'join'>;
export type EndShare = Typed<string, 'endshare'>;
export type IncomingMessage =
| Room
@@ -88,6 +90,7 @@ export type IncomingMessage =
| HostICECandidate
| ClientICECandidate
| HostOffer
| EndShare
| ClientAnswer;
export type OutgoingMessage =
@@ -97,5 +100,6 @@ export type OutgoingMessage =
| HostICECandidate
| ClientICECandidate
| HostOffer
| StopShare
| ClientAnswer
| StartSharing;
+14 -1
View File
@@ -242,6 +242,19 @@ export const useRoom = (): UseRoom => {
case 'hostice':
client.current[event.payload.sid]?.addIceCandidate(event.payload.value);
return;
case 'endshare':
client.current[event.payload]?.close();
host.current[event.payload]?.close();
setState((current) =>
current
? {
...current,
clientStreams: current.clientStreams.filter(
({id}) => id !== event.payload
),
}
: current
);
}
};
ws.onclose = (event) => {
@@ -285,7 +298,7 @@ export const useRoom = (): UseRoom => {
host.current = {};
stream.current?.getTracks().forEach((track) => track.stop());
stream.current = undefined;
// todo notify server
conn.current?.send(JSON.stringify({type: 'stopshare'}));
setState((current) => (current ? {...current, hostStream: undefined} : current));
};
+22
View File
@@ -1,5 +1,10 @@
package ws
import (
"bytes"
"github.com/screego/server/ws/outgoing"
)
type Disconnected struct {
}
@@ -24,6 +29,23 @@ func (e *Disconnected) Execute(rooms *Rooms, current ClientInfo) error {
current.Close <- CloseDone
delete(room.Users, current.ID)
for id, session := range room.Sessions {
if bytes.Equal(session.Client.Bytes(), current.ID.Bytes()) {
host, ok := room.Users[session.Host]
if ok {
host.Write <- outgoing.EndShare(id)
}
delete(room.Sessions, id)
}
if bytes.Equal(session.Host.Bytes(), current.ID.Bytes()) {
client, ok := room.Users[session.Client]
if ok {
client.Write <- outgoing.EndShare(id)
}
delete(room.Sessions, id)
}
}
if user.Owner && room.CloseOnOwnerLeave {
for _, member := range room.Users {
member.Close <- CloseOwnerLeft
+11
View File
@@ -1,7 +1,9 @@
package ws
import (
"bytes"
"fmt"
"github.com/screego/server/ws/outgoing"
)
func init() {
@@ -24,6 +26,15 @@ func (e *StopShare) Execute(rooms *Rooms, current ClientInfo) error {
}
room.Users[current.ID].Sharing = false
for id, session := range room.Sessions {
if bytes.Equal(session.Host.Bytes(), current.ID.Bytes()) {
client, ok := room.Users[session.Client]
if ok {
client.Write <- outgoing.EndShare(id)
}
delete(room.Sessions, id)
}
}
room.notifyInfoChanged()
return nil
+6
View File
@@ -83,6 +83,12 @@ func (HostOffer) Type() string {
return "hostoffer"
}
type EndShare xid.ID
func (EndShare) Type() string {
return "endshare"
}
type ConnectionMode string
const (