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

JSONWebSocket仕様の新ニコ実況に対応 #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Bob-FU
Copy link

@Bob-FU Bob-FU commented Mar 28, 2021

Newtonsoft.Jsonを導入し、ニコ実況のJSONWebSocketAPIに接続してコメントデータを取得するように改造しました。
※もともとのXMLベースのWebsocketAPIは死んだっぽくってTVTCommentが落ちまくりです。

C#のソースをいじるのは実は1億年ぶりです。実装は凄く雑かもしれないが一応当方の環境では問題なさそうです。
変なところもしあれば勝手に修正してくださいm

Copy link
Contributor

@noriokun4649 noriokun4649 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

気になる点としては、60秒ごとに空リクエストを送っていないこととvposに関することです。
あと、ニコニコ的にはUAで利用状況を把握しているみたいなのでUAは付けた用がよさそうなのと、コメントセッションでは必要じゃないぽいけど、視聴セッションではWebSocketで通信する際に一応サブタイトルを指定するようになっているのでそちらも付けた方が良さそうです。

あとfor (int disconnectedCount = 0; disconnectedCount < 5; ++disconnectedCount)直下に

var random = new Random();
await Task.Delay((disconnectedCount * 5000) + random.Next(0, 101));

見たいな具合に連続した再接続にならないようにした方が良さそうです。

private static ChatNiconicoCommentXmlTag getChatJSONTag(string str) {
JObject jsonObj = JObject.Parse(str);
// {"chat":{"thread":"M.kk-tzPBrneGMsNfDNO1skg","no":45929,"vpos":6496293,"date":1616936565,"date_usec":664450,"mail":"184","user_id":"EvrCRqk2e04B-pYS7q44kVU5HR4","anonymity":1,"content":"結局SBの勝ちかいw"}}
int vpos = int.Parse(jsonObj["chat"]["vpos"].ToString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

極々まれにニコ生のAPI側の不具合なのか必須項目のvposが抜けてるデータが流れてくるので注意が必要かもしれません。

using var tcpClinet = new TcpClient(msUri.Host, msUri.Port);
var socketStream = tcpClinet.GetStream();
using var socketReader = new StreamReader(socketStream, Encoding.UTF8);
ClientWebSocket ws = new ClientWebSocket();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestHeaderにUAを付けるのと、サブプロトコルに「msg.nicovideo.jp#json」を指定した方が良さそうです。

@Bob-FU
Copy link
Author

Bob-FU commented Mar 28, 2021

RequestHeaderにUAとSubProtocolの情報を入れました。
image

60秒1回の空メッセージ送信はちょっと考えておく。

@Bob-FU
Copy link
Author

Bob-FU commented Mar 28, 2021

KeepAliveコマンド送信部分を実装しました。

Copy link
Contributor

@noriokun4649 noriokun4649 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

早速の対応ありがとうございます!
UAは利用者の把握をするのが目的なので、なおした方が良さそうです。

@@ -178,5 +186,8 @@ public void Dispose()

private readonly HttpClient httpClient;
private readonly NiconicoCommentJsonParser parser = new NiconicoCommentJsonParser(true);
private readonly string WEBSOCKET_CLIENT_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UAは
TVTComment/Model/ChatCollectService/TsukumijimaJikkyoApiChatCollectService.cs
の最後あたりにあるようなUAにした方がよいです。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noriokun4649 ニコニコ側が今後、UAでTVTCommentからの通信をブロックするのではないかと少々心配ではありますが、まあ本家のnicojk等はもう長年やっていましたので、おそらく問題ないでしょう。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

むしろUAが付加されてないリクエストやMozilla系のUAわブロックするような変更も過去にあったため、ルールを守った利用方法であれば問題ないと考えています。
何かしら問題があった場合でもニコニコ側からのコンタクトもしやすいですしね。(これが主な目的のようです

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants