同じ接続を使いまわすように
This commit is contained in:
parent
f24d86bf93
commit
98dca7b7ac
6 changed files with 60 additions and 13 deletions
|
|
@ -1,9 +1,13 @@
|
|||
import * as riot from 'riot';
|
||||
|
||||
import activateMe from './i';
|
||||
import activateApi from './api';
|
||||
import activateStream from './stream';
|
||||
|
||||
export default (me, stream) => {
|
||||
export default (me, stream, serverStreamManager) => {
|
||||
activateMe(me);
|
||||
activateApi(me);
|
||||
activateStream(stream);
|
||||
|
||||
riot.mixin('stream', { stream });
|
||||
|
||||
riot.mixin('server-stream', { serverStream: serverStreamManager });
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
import * as riot from 'riot';
|
||||
|
||||
export default stream => {
|
||||
riot.mixin('stream', { stream });
|
||||
};
|
||||
39
src/web/app/common/scripts/server-stream-manager.ts
Normal file
39
src/web/app/common/scripts/server-stream-manager.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import Connection from './server-stream';
|
||||
import 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue