Skip to content

Instantly share code, notes, and snippets.

@kino6052
Last active August 1, 2020 01:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kino6052/4e720807c647847d904bb2d132f9bbe9 to your computer and use it in GitHub Desktop.
Save kino6052/4e720807c647847d904bb2d132f9bbe9 to your computer and use it in GitHub Desktop.
export class Client {
private id = generateId(4, 4);
// Channels
private dataChannels: { [id: string]: RTCDataChannel } = {};
private streams: { [id: string]: MediaStream[] } = {};
// Agents
private BroadcastingAgent = new BroadcastingAgent(
this.id,
CommunicationSubject
);
private RTCMessagingAgent = new RTCMessagingAgent(this.BroadcastingAgent);
private ConnectionManager = new ConnectionManager(this.RTCMessagingAgent);
// Subjects
private OnDataChannelMessageSubject = new Subject<[string, string]>();
private OnDataChannelSubject = new Subject<[string, RTCDataChannel]>();
private OnStreamSubject = new Subject<[string, MediaStream]>();
constructor() {
this.OnDataChannelMessageSubject.subscribe(
this.onDataChannelMessageSubjectHandler
);
this.ConnectionManager.OnConnectionCreatedSubject.subscribe(
this.onConnectionCreatedHandler
);
this.ConnectionManager.OnStreamSubject.subscribe(this.onStreamHandler);
this.BroadcastingAgent.sendGreeting();
}
// Connection
onConnectionCreatedHandler = (message: [string, RTCPeerConnection]) => {
// ...
};
// Stream
addStreamToConnection = (connection: RTCPeerConnection) => {
// ...
};
addStream = (id: string, stream: MediaStream) => {
// ...
};
onStreamHandler = (stream: MediaStream) => {
// ...
};
onTrackHandler = (id: string) => (ev: RTCTrackEvent) => {
// ...
};
// Data Channel
sendDataToChannel = (id: string, message: string) => {
// ...
};
onDataChannelHandler = (id: string) => (ev: RTCDataChannelEvent) => {
// ...
};
onDataChannelMessageHandler = (id: string) => (ev: MessageEvent) => {
// ...
};
onDataChannelMessageSubjectHandler = (message: [string, string]) => {
// ...
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment