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
11 changes: 11 additions & 0 deletions TVTComment/Model/NiconicoUtils/NicoLiveCommentReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,17 @@ public async IAsyncEnumerable<NiconicoCommentXmlTag> Receive(string liveId, [Enu
}
// WebSocketAPIに接続
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」を指定した方が良さそうです。

// UAヘッダ追加
ws.Options.SetRequestHeader("User-Agent", WEBSOCKET_CLIENT_UA);
// Sec-WebSocket-Protocolヘッダ追加
ws.Options.SetRequestHeader("Sec-WebSocket-Protocol", WEBSOCKET_PROTOCOL);
// Sec-WebSocket-Versionヘッダ追加
ws.Options.SetRequestHeader("Sec-WebSocket-Extensions", WEBSOCKET_EXTENSIONS);

var uri = new Uri(msUriStr);
await ws.ConnectAsync(uri, cancellationToken);
var buffer = new byte[1024];

// threadId情報を送信
string body = "[{\"ping\":{\"content\":\"rs:0\"}},{\"ping\":{\"content\":\"ps:0\"}},{\"thread\":{\"thread\":\"" + threadId + "\",\"version\":\"20061206\",\"user_id\":\"guest\",\"res_from\":-10,\"with_global\":1,\"scores\":1,\"nicoru\":0}},{\"ping\":{\"content\":\"pf:0\"}},{\"ping\":{\"content\":\"rf:0\"}}]";
byte[] bodyEncoded = Encoding.UTF8.GetBytes(body);
Expand Down Expand Up @@ -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わブロックするような変更も過去にあったため、ルールを守った利用方法であれば問題ないと考えています。
何かしら問題があった場合でもニコニコ側からのコンタクトもしやすいですしね。(これが主な目的のようです

private readonly string WEBSOCKET_PROTOCOL = "msg.nicovideo.jp#json";
private readonly string WEBSOCKET_EXTENSIONS = "permessage-deflate; client_max_window_bits";
}
}