Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ストリーミングAPIを実装する #2

Open
EbiseLutica opened this issue Sep 22, 2020 · 0 comments
Open

ストリーミングAPIを実装する #2

EbiseLutica opened this issue Sep 22, 2020 · 0 comments
Labels

Comments

@EbiseLutica
Copy link
Owner

EbiseLutica commented Sep 22, 2020

Misskeyのストリーミングを購読する機能を実装する

基本

現在調整中のため、たびたびAPIが変わる可能性がある

// Misskey mi;

var cid = Guid.NewGuid().ToString();
var st = mi.OpenStreaming();

// 受信
st.Received += (StreamingResponse res) =>
{
  if (res.Type == "channel" && res.Raw["body"] is Dictionary<string, object> body && body["id"] == cid && body["note"] is Dictionary<string, object> note)
  {
    Console.WriteLine(note["text"] ?? "null");
  }
};

// 購読
await st.SendAsync("connect", new
{
  id = cid,
  channel = "hybridTimeline",
});

// WSを切断するときは次のように
st.Dispose();

拡張

いくつかのシナリオで使いやすいようヘルパーイベントがある

var st = mi.OpenStreaming(); // StreamingContext

// チャンネル接続
var main = st.ConnectChannel("main"); // ChannelStreamingContext
var tl = st.ConnectChannel("timeline"); // ChannelStreamingContext

// チャンネル接続解除
main.Dispose();
tl.Dispose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant