2018-10-07 11:06:17 +09:00
|
|
|
import autobind from 'autobind-decorator';
|
|
|
|
import { EventEmitter } from 'eventemitter3';
|
2018-10-08 01:16:32 +09:00
|
|
|
import ReconnectingWebsocket from 'reconnecting-websocket';
|
2020-11-01 11:39:38 +09:00
|
|
|
import { debug, wsUrl } from '@/config';
|
2020-10-09 14:20:34 +09:00
|
|
|
import { query as urlQuery } from '../../prelude/url';
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Misskey stream connection
|
|
|
|
*/
|
|
|
|
export default class Stream extends EventEmitter {
|
|
|
|
private stream: ReconnectingWebsocket;
|
2020-10-17 20:12:00 +09:00
|
|
|
public state: 'initializing' | 'reconnecting' | 'connected' = 'initializing';
|
2018-10-10 20:59:10 +09:00
|
|
|
private sharedConnectionPools: Pool[] = [];
|
2018-10-07 11:06:17 +09:00
|
|
|
private sharedConnections: SharedConnection[] = [];
|
|
|
|
private nonSharedConnections: NonSharedConnection[] = [];
|
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
@autobind
|
|
|
|
public init(user): void {
|
2020-10-09 14:20:34 +09:00
|
|
|
const query = urlQuery({
|
|
|
|
i: user?.token,
|
|
|
|
_t: Date.now(),
|
|
|
|
});
|
|
|
|
|
|
|
|
this.stream = new ReconnectingWebsocket(`${wsUrl}?${query}`, '', { minReconnectionDelay: 1 }); // https://github.com/pladaria/reconnecting-websocket/issues/91
|
2018-10-07 11:06:17 +09:00
|
|
|
this.stream.addEventListener('open', this.onOpen);
|
|
|
|
this.stream.addEventListener('close', this.onClose);
|
|
|
|
this.stream.addEventListener('message', this.onMessage);
|
|
|
|
}
|
|
|
|
|
2018-10-10 22:13:32 +09:00
|
|
|
@autobind
|
2020-11-01 11:39:38 +09:00
|
|
|
public useSharedConnection(channel: string, name?: string): SharedConnection {
|
2018-10-10 20:59:10 +09:00
|
|
|
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
|
2018-10-07 11:06:17 +09:00
|
|
|
|
2018-10-10 20:59:10 +09:00
|
|
|
if (pool == null) {
|
|
|
|
pool = new Pool(this, channel);
|
|
|
|
this.sharedConnectionPools.push(pool);
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
2018-10-10 20:59:10 +09:00
|
|
|
|
2020-11-01 11:39:38 +09:00
|
|
|
const connection = new SharedConnection(this, channel, pool, name);
|
2018-10-10 20:59:10 +09:00
|
|
|
this.sharedConnections.push(connection);
|
|
|
|
return connection;
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public removeSharedConnection(connection: SharedConnection) {
|
2018-10-10 20:59:10 +09:00
|
|
|
this.sharedConnections = this.sharedConnections.filter(c => c !== connection);
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
2018-10-11 22:17:27 +09:00
|
|
|
@autobind
|
|
|
|
public removeSharedConnectionPool(pool: Pool) {
|
|
|
|
this.sharedConnectionPools = this.sharedConnectionPools.filter(p => p !== pool);
|
|
|
|
}
|
|
|
|
|
2018-10-10 22:13:32 +09:00
|
|
|
@autobind
|
|
|
|
public connectToChannel(channel: string, params?: any): NonSharedConnection {
|
2018-10-07 11:06:17 +09:00
|
|
|
const connection = new NonSharedConnection(this, channel, params);
|
|
|
|
this.nonSharedConnections.push(connection);
|
|
|
|
return connection;
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public disconnectToChannel(connection: NonSharedConnection) {
|
2018-10-10 20:59:10 +09:00
|
|
|
this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection);
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback of when open connection
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onOpen() {
|
2020-04-04 08:46:54 +09:00
|
|
|
const isReconnect = this.state === 'reconnecting';
|
2018-10-07 11:06:17 +09:00
|
|
|
|
|
|
|
this.state = 'connected';
|
|
|
|
this.emit('_connected_');
|
|
|
|
|
|
|
|
// チャンネル再接続
|
|
|
|
if (isReconnect) {
|
2018-12-09 13:09:31 +09:00
|
|
|
for (const p of this.sharedConnectionPools)
|
2018-10-11 22:17:27 +09:00
|
|
|
p.connect();
|
2018-12-09 13:09:31 +09:00
|
|
|
for (const c of this.nonSharedConnections)
|
2018-10-07 11:06:17 +09:00
|
|
|
c.connect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback of when close connection
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onClose() {
|
2020-04-04 08:46:54 +09:00
|
|
|
if (this.state === 'connected') {
|
2018-10-11 22:35:34 +09:00
|
|
|
this.state = 'reconnecting';
|
|
|
|
this.emit('_disconnected_');
|
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback of when received a message from connection
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
private onMessage(message) {
|
|
|
|
const { type, body } = JSON.parse(message.data);
|
|
|
|
|
2020-04-04 08:46:54 +09:00
|
|
|
if (type === 'channel') {
|
2018-10-07 11:06:17 +09:00
|
|
|
const id = body.id;
|
2018-10-10 20:59:10 +09:00
|
|
|
|
|
|
|
let connections: Connection[];
|
|
|
|
|
|
|
|
connections = this.sharedConnections.filter(c => c.id === id);
|
|
|
|
|
|
|
|
if (connections.length === 0) {
|
|
|
|
connections = [this.nonSharedConnections.find(c => c.id === id)];
|
|
|
|
}
|
|
|
|
|
2018-12-11 20:36:55 +09:00
|
|
|
for (const c of connections.filter(c => c != null)) {
|
2020-07-27 23:25:37 +09:00
|
|
|
c.emit(body.type, Object.freeze(body.body));
|
2020-11-01 11:39:38 +09:00
|
|
|
if (debug) c.inCount++;
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
} else {
|
2020-07-27 23:25:37 +09:00
|
|
|
this.emit(type, Object.freeze(body));
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message to connection
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
public send(typeOrPayload, payload?) {
|
|
|
|
const data = payload === undefined ? typeOrPayload : {
|
|
|
|
type: typeOrPayload,
|
|
|
|
body: payload
|
|
|
|
};
|
|
|
|
|
|
|
|
this.stream.send(JSON.stringify(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close this connection
|
|
|
|
*/
|
|
|
|
@autobind
|
|
|
|
public close() {
|
|
|
|
this.stream.removeEventListener('open', this.onOpen);
|
|
|
|
this.stream.removeEventListener('message', this.onMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:39:38 +09:00
|
|
|
let idCounter = 0;
|
|
|
|
|
2018-10-10 20:59:10 +09:00
|
|
|
class Pool {
|
2018-10-07 11:06:17 +09:00
|
|
|
public channel: string;
|
|
|
|
public id: string;
|
|
|
|
protected stream: Stream;
|
2018-10-11 21:25:55 +09:00
|
|
|
public users = 0;
|
2018-10-10 20:59:10 +09:00
|
|
|
private disposeTimerId: any;
|
|
|
|
private isConnected = false;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
2018-10-10 20:59:10 +09:00
|
|
|
constructor(stream: Stream, channel: string) {
|
2018-10-07 11:06:17 +09:00
|
|
|
this.channel = channel;
|
2018-10-10 20:59:10 +09:00
|
|
|
this.stream = stream;
|
|
|
|
|
2020-11-01 11:39:38 +09:00
|
|
|
this.id = (++idCounter).toString();
|
2018-10-11 22:17:27 +09:00
|
|
|
|
|
|
|
this.stream.on('_disconnected_', this.onStreamDisconnected);
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
private onStreamDisconnected() {
|
|
|
|
this.isConnected = false;
|
2018-10-10 20:59:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public inc() {
|
|
|
|
if (this.users === 0 && !this.isConnected) {
|
|
|
|
this.connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.users++;
|
|
|
|
|
|
|
|
// タイマー解除
|
|
|
|
if (this.disposeTimerId) {
|
|
|
|
clearTimeout(this.disposeTimerId);
|
|
|
|
this.disposeTimerId = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public dec() {
|
|
|
|
this.users--;
|
|
|
|
|
|
|
|
// そのコネクションの利用者が誰もいなくなったら
|
|
|
|
if (this.users === 0) {
|
|
|
|
// また直ぐに再利用される可能性があるので、一定時間待ち、
|
|
|
|
// 新たな利用者が現れなければコネクションを切断する
|
|
|
|
this.disposeTimerId = setTimeout(() => {
|
|
|
|
this.disconnect();
|
|
|
|
}, 3000);
|
|
|
|
}
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public connect() {
|
2018-10-11 22:17:27 +09:00
|
|
|
if (this.isConnected) return;
|
2018-10-10 20:59:10 +09:00
|
|
|
this.isConnected = true;
|
2018-10-07 11:06:17 +09:00
|
|
|
this.stream.send('connect', {
|
|
|
|
channel: this.channel,
|
2018-10-10 20:59:10 +09:00
|
|
|
id: this.id
|
2018-10-07 11:06:17 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
2018-10-10 20:59:10 +09:00
|
|
|
private disconnect() {
|
2018-10-11 22:17:27 +09:00
|
|
|
this.stream.off('_disconnected_', this.onStreamDisconnected);
|
2018-10-10 20:59:10 +09:00
|
|
|
this.stream.send('disconnect', { id: this.id });
|
2018-10-11 22:17:27 +09:00
|
|
|
this.stream.removeSharedConnectionPool(this);
|
2018-10-10 20:59:10 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class Connection extends EventEmitter {
|
|
|
|
public channel: string;
|
|
|
|
protected stream: Stream;
|
|
|
|
public abstract id: string;
|
|
|
|
|
2020-11-01 11:39:38 +09:00
|
|
|
public name?: string; // for debug
|
|
|
|
public inCount: number = 0; // for debug
|
|
|
|
public outCount: number = 0; // for debug
|
|
|
|
|
|
|
|
constructor(stream: Stream, channel: string, name?: string) {
|
2018-10-10 20:59:10 +09:00
|
|
|
super();
|
|
|
|
|
|
|
|
this.stream = stream;
|
|
|
|
this.channel = channel;
|
2020-11-01 11:39:38 +09:00
|
|
|
this.name = name;
|
2018-10-10 20:59:10 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public send(id: string, typeOrPayload, payload?) {
|
2018-10-09 01:50:49 +09:00
|
|
|
const type = payload === undefined ? typeOrPayload.type : typeOrPayload;
|
|
|
|
const body = payload === undefined ? typeOrPayload.body : payload;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
2018-10-10 03:24:09 +09:00
|
|
|
this.stream.send('ch', {
|
2018-10-10 20:59:10 +09:00
|
|
|
id: id,
|
2018-10-09 01:50:49 +09:00
|
|
|
type: type,
|
|
|
|
body: body
|
2018-10-07 11:06:17 +09:00
|
|
|
});
|
2020-11-01 11:39:38 +09:00
|
|
|
|
|
|
|
if (debug) this.outCount++;
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
2018-10-09 15:13:06 +09:00
|
|
|
public abstract dispose(): void;
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
class SharedConnection extends Connection {
|
2018-10-10 20:59:10 +09:00
|
|
|
private pool: Pool;
|
2018-10-07 11:06:17 +09:00
|
|
|
|
2018-10-10 20:59:10 +09:00
|
|
|
public get id(): string {
|
|
|
|
return this.pool.id;
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:39:38 +09:00
|
|
|
constructor(stream: Stream, channel: string, pool: Pool, name?: string) {
|
|
|
|
super(stream, channel, name);
|
2018-10-10 20:59:10 +09:00
|
|
|
|
|
|
|
this.pool = pool;
|
|
|
|
this.pool.inc();
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
2018-10-10 20:59:10 +09:00
|
|
|
public send(typeOrPayload, payload?) {
|
|
|
|
super.send(this.pool.id, typeOrPayload, payload);
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public dispose() {
|
2018-10-10 20:59:10 +09:00
|
|
|
this.pool.dec();
|
|
|
|
this.removeAllListeners();
|
|
|
|
this.stream.removeSharedConnection(this);
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NonSharedConnection extends Connection {
|
2018-10-10 20:59:10 +09:00
|
|
|
public id: string;
|
|
|
|
protected params: any;
|
|
|
|
|
2018-10-07 11:06:17 +09:00
|
|
|
constructor(stream: Stream, channel: string, params?: any) {
|
2018-10-10 20:59:10 +09:00
|
|
|
super(stream, channel);
|
|
|
|
|
|
|
|
this.params = params;
|
2020-11-01 11:39:38 +09:00
|
|
|
this.id = (++idCounter).toString();
|
2018-10-10 20:59:10 +09:00
|
|
|
|
|
|
|
this.connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public connect() {
|
|
|
|
this.stream.send('connect', {
|
|
|
|
channel: this.channel,
|
|
|
|
id: this.id,
|
|
|
|
params: this.params
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public send(typeOrPayload, payload?) {
|
|
|
|
super.send(this.id, typeOrPayload, payload);
|
2018-10-07 11:06:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
@autobind
|
|
|
|
public dispose() {
|
|
|
|
this.removeAllListeners();
|
|
|
|
this.stream.send('disconnect', { id: this.id });
|
|
|
|
this.stream.disconnectToChannel(this);
|
|
|
|
}
|
|
|
|
}
|