summaryrefslogtreecommitdiff
path: root/src/web/app/common/scripts/server-stream-manager.ts
blob: 54333c8cf5d54afb0abff019ef21c953d9609832 (plain)
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
import Connection from './server-stream';
import * as uuid from 'uuid';

export default class ServerStreamManager {
	private connection = null;

	/**
	 * コネクションを必要としているユーザー
	 */
	private users = [];

	public getConnection() {
		if (this.connection == null) {
			this.connection = new Connection();
		}

		return this.connection;
	}

	public use() {
		// ユーザーID生成
		const userId = uuid();

		this.users.push(userId);

		return userId;
	}

	public dispose(userId) {
		this.users = this.users.filter(id => id != userId);

		// 誰もコネクションの利用者がいなくなったら
		if (this.users.length == 0) {
			// コネクションを切断する
			this.connection.close();
			this.connection = null;
		}
	}
}