-
Notifications
You must be signed in to change notification settings - Fork 12
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
Bob-FU
wants to merge
8
commits into
silane:master
Choose a base branch
from
Bob-FU:json_chat_20210328
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b6c8cbb
JSONWebSocket仕様の新ニコ実況に対応
Bob-FU 49dbde5
コメントなどの微修正
Bob-FU 4141d5c
ニコ生側の不具合で稀に必須項目のvposが抜けてるデータが流れてくる可能性があるので念の為JSONキー確認する。
Bob-FU edcee91
RequestHeaderに必要なヘッダを追加
Bob-FU 74426de
万が一Websocket接続中断した場合、数秒空いたからリトライ
Bob-FU a877c9d
RequestHeaderにSubProtocolの情報を追加
Bob-FU 57d8d46
Keepaliveコマンド送信処理の実装
Bob-FU beb72a5
ニコ生と通信する際に送信するUA情報を変更
Bob-FU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
TVTComment/Model/NiconicoUtils/NiconicoCommentJsonParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System.Collections.Generic; | ||
|
||
namespace TVTComment.Model.NiconicoUtils | ||
{ | ||
class NiconicoCommentJsonParser | ||
{ | ||
private bool socketFormat; | ||
private Queue<NiconicoCommentXmlTag> chats = new Queue<NiconicoCommentXmlTag>(); | ||
private string buffer; | ||
|
||
/// <summary> | ||
/// <see cref="NiconicoCommentJsonParser"/>を初期化する | ||
/// </summary> | ||
/// <param name="socketFormat">ソケットを使うリアルタイムのデータ形式ならtrue 過去ログなどのデータ形式ならfalse</param> | ||
public NiconicoCommentJsonParser(bool socketFormat) | ||
{ | ||
this.socketFormat = socketFormat; | ||
} | ||
|
||
public void Push(string str) | ||
{ | ||
if (socketFormat) | ||
{ | ||
// 一旦、コメント関連データのみ解析する | ||
if (str.StartsWith("{\"chat")) | ||
{ | ||
chats.Enqueue(getChatJSONTag(str)); | ||
} | ||
} | ||
else | ||
{ | ||
// サポートしない, | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// 解析結果を返す <see cref="socketFormat"/>がfalseなら<see cref="ChatNiconicoCommentXmlTag"/>しか返さない | ||
/// </summary> | ||
/// <returns>解析結果の<see cref="NiconicoCommentXmlTag"/></returns> | ||
public NiconicoCommentXmlTag Pop() | ||
{ | ||
return chats.Dequeue(); | ||
} | ||
|
||
/// <summary> | ||
/// <see cref="Pop"/>で読みだすデータがあるか | ||
/// </summary> | ||
public bool DataAvailable() | ||
{ | ||
return chats.Count > 0; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
buffer = string.Empty; | ||
chats.Clear(); | ||
} | ||
|
||
private static ChatNiconicoCommentXmlTag getChatJSONTag(string str) { | ||
JObject jsonObj = JObject.Parse(str); | ||
|
||
int vpos = jsonObj["chat"]["vpos"] == null ? 0 : int.Parse(jsonObj["chat"]["vpos"].ToString()); //ニコ生側の不具合で稀に必須項目のvposが抜けてるデータが流れてくる可能性があるので念の為JSONキー確認する。 | ||
long date = long.Parse(jsonObj["chat"]["date"].ToString()); | ||
int dateUsec = jsonObj["chat"]["date_usec"] == null ? 0 : int.Parse(jsonObj["chat"]["date_usec"].ToString()); | ||
string mail = jsonObj["chat"]["mail"] == null ? "" : jsonObj["chat"]["mail"].ToString(); | ||
string userId = jsonObj["chat"]["user_id"].ToString(); | ||
int premium = jsonObj["chat"]["premium"] == null ? 0 : int.Parse(jsonObj["chat"]["premium"].ToString()); | ||
int anonymity = jsonObj["chat"]["anonymity"] == null ? 0 : int.Parse(jsonObj["chat"]["anonymity"].ToString()); | ||
int abone = jsonObj["chat"]["abone"] == null ? 0 : int.Parse(jsonObj["chat"]["abone"].ToString()); | ||
string content = (string)jsonObj["chat"]["content"]; | ||
int no = int.Parse(jsonObj["chat"]["no"].ToString()); | ||
|
||
return new ChatNiconicoCommentXmlTag( | ||
content, 0, no, vpos, date, dateUsec, mail, userId, premium, anonymity, abone | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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」を指定した方が良さそうです。