-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconnection.ts
187 lines (153 loc) · 4.99 KB
/
connection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//@ts-ignore
import Promise from 'bluebird';
import TransactionManager from './tx/transaction-manager';
import JanusError from './misc/error';
import Transaction from './tx/transaction';
import Websocket from './websocket';
import Session from './session';
import JanusMessage from './misc/message';
import { MediaDevices, WebRTC } from '../plugin/base/shims/definitions';
export interface RTCPeerConnectionOptions {
config?: any;
constraints?: any;
}
export interface ConnectionOptions {
token?: string;
apisecret?: string;
keepalive?: boolean | number;
pc?: RTCPeerConnectionOptions;
iceServers?: RTCIceServer[];
}
class Connection extends TransactionManager {
private readonly id: string;
private readonly address: string;
private readonly sessions: { [key: string]: Session };
private readonly options: ConnectionOptions;
private readonly websocketConnection: Websocket;
private readonly mediaDevices: MediaDevices;
private readonly webRTC: WebRTC;
constructor(id: string, address: string, options: ConnectionOptions, mediaDevices: MediaDevices, webRTC: WebRTC) {
super();
this.id = id;
this.address = address;
this.sessions = {};
this.options = options;
this.websocketConnection = new Websocket();
this.initWebsocketListeners();
this.mediaDevices = mediaDevices;
this.webRTC = webRTC;
}
getId(): string {
return this.id;
}
getAddress(): string {
return this.address;
}
getOptions(): ConnectionOptions {
return this.options;
}
open(): Promise<Connection> {
return this.websocketConnection.open(this.address, 'janus-protocol').return(this);
}
// @ts-ignore
async close(): Promise<boolean> {
if (this.websocketConnection.isOpened()) {
return Promise.map(this.getSessionList(), session => session.cleanup())
.then(() => this.websocketConnection.close())
.then(() => this.emit('close'));
}
return true;
}
isClosed(): boolean {
return this.websocketConnection.isClosed();
}
createSession(): Promise<Session> {
return this.sendSync({ janus: 'create' }, this);
}
hasSession(sessionId: string): boolean {
return !!this.getSession(sessionId);
}
getSession(sessionId: string): Session {
return this.sessions[sessionId];
}
getSessionList(): Session[] {
return Object.keys(this.sessions).map(id => this.sessions[id]);
}
addSession(session: Session) {
this.sessions[session.getId()] = session;
session.once('destroy', () => this.removeSession(session.getId()));
}
removeSession(sessionId: string) {
delete this.sessions[sessionId];
}
send(message: { token: string; apisecret: string; transaction: string }): Promise<void> {
if (this.options.token) {
message.token = this.options.token;
}
if (this.options.apisecret) {
message.apisecret = this.options.apisecret;
}
if (!message.transaction) {
message.transaction = Transaction.generateRandomId();
}
return this.websocketConnection.send(message);
}
// @ts-ignore
async processOutcomeMessage(message: JanusMessage): Promise<JanusMessage> {
//@ts-ignore
if ('create' === message.janus) {
return this.onCreate(message);
}
//@ts-ignore
let sessionId = message['session_id'];
if (sessionId) {
if (this.hasSession(sessionId)) {
return this.getSession(sessionId).processOutcomeMessage(message);
} else {
throw new Error(`Invalid session: [${sessionId}]`);
}
}
return message;
}
processIncomeMessage(msg: JanusMessage): Promise<any> {
this.emit('message', msg);
let sessionId = msg.get('session_id');
if (sessionId && this.hasSession(sessionId)) {
return this.getSession(sessionId).processIncomeMessage(msg);
}
return Promise.try(() => {
if (sessionId && !this.hasSession(sessionId)) {
throw new Error(`Invalid session: [${sessionId}]`);
}
return this.defaultProcessIncomeMessage(msg);
});
}
toString() {
return `[Connection] ${JSON.stringify({ id: this.id, address: this.address })}`;
}
//@ts-ignore
private async onCreate(outMsg: JanusMessage): Promise<JanusMessage> {
this.addTransaction(
//@ts-ignore
new Transaction(outMsg.transaction, (msg: JanusMessage) => {
if ('success' === msg.get('janus')) {
let sessionId = msg.get('data', 'id');
this.addSession(new Session(this, sessionId, this.mediaDevices, this.webRTC));
return this.getSession(sessionId);
} else {
throw new JanusError(msg);
}
})
);
return outMsg;
}
private initWebsocketListeners() {
this.websocketConnection.on('open', () => this.emit('open'));
this.websocketConnection.on('error', () => this.emit('error'));
this.websocketConnection.on('close', () => this.emit('close'));
this.websocketConnection.on('message', msg => {
this.processIncomeMessage(new JanusMessage(msg)).catch(error => this.emit('error', error));
});
}
}
export default Connection;