This commit is contained in:
syuilo 2021-05-23 13:34:36 +09:00
parent ca655c0628
commit 6ae5f76250
4 changed files with 30 additions and 27 deletions

View file

@ -34,30 +34,27 @@ const meta = await cli.request('meta', { detail: true });
import * as Misskey from 'misskey-js';
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useSharedConnection('main');
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
console.log('notification received', notification);
});
```
### チャンネルへの接続(使いまわす場合)
使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続するときは、`useSharedConnection`メソッドを使用します。
### チャンネルへの接続
チャンネルへの接続は`useChannel`メソッドを使用します。
パラメータなし
``` ts
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useSharedConnection('main');
const mainChannel = stream.useChannel('main');
```
このメソッドを用いてチャンネルに接続することで、(同じStreamインスタンスを共有している場合)プログラム上の複数個所から呼び出しても内部的にまとめられます。
### チャンネルへの接続(使いまわし不可の場合)
パラメータを持つチャンネルへの接続は`connectToChannel`メソッドを使用します。
パラメータあり
``` ts
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const messagingChannel = stream.connectToChannel('messaging', {
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
```
@ -68,7 +65,7 @@ const messagingChannel = stream.connectToChannel('messaging', {
``` ts
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useSharedConnection('main');
const mainChannel = stream.useChannel('main');
mainChannel.dispose();
```
@ -80,7 +77,7 @@ mainChannel.dispose();
import * as Misskey from 'misskey-js';
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const mainChannel = stream.useSharedConnection('main');
const mainChannel = stream.useChannel('main');
mainChannel.on('notification', notification => {
console.log('notification received', notification);
});
@ -93,7 +90,7 @@ mainChannel.on('notification', notification => {
import * as Misskey from 'misskey-js';
const stream = new Misskey.Stream('https://misskey.test', { token: 'TOKEN' });
const messagingChannel = stream.connectToChannel('messaging', {
const messagingChannel = stream.useChannel('messaging', {
otherparty: 'xxxxxxxxxx',
});
@ -102,15 +99,6 @@ messagingChannel.send('read', {
});
```
### Reference
#### `useSharedConnection(channel: string): SharedConnection`
使いまわし可能なチャンネル(=パラメータを持たないチャンネル)に接続します。
このメソッドを用いて接続したチャンネル接続は内部的に使いまわされるため、プログラム上の複数の場所から呼び出してもコネクションを無駄に増やさずに済みます。
#### `connectToChannel(channel: string, params?: any): NonSharedConnection`
チャンネルに接続します。返り値はそのチャンネルへのコネクションインスタンスです。
---
<div align="center">