// Listener recieved private void BiliLiveListener_JsonRecieved(Json.Value json) { Log += json.ToString() + "\r\n"; if (debugWindow != null) { debugWindow.AppendLog(json.ToString()); } BiliLiveJsonParser.Item item = BiliLiveJsonParser.Parse(json); AppendItem(item); AppendHistory(item); }
public async Task <bool> AddRunePageAsync(Json.Value pageJson) { if (!await PrepareAsync()) { throw new NoClientException(); } HttpWebRequest request = Auth.CreateRequest("/lol-perks/v1/pages"); request.Method = "POST"; byte[] data = Encoding.UTF8.GetBytes(pageJson.ToString()); Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); newStream.Close(); try { HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); if (response.StatusCode == HttpStatusCode.OK) { return(true); } return(false); } catch (WebException ex) { return(false); } }
public static IItem Parse(Json.Value json) { Console.WriteLine(json.ToString()); try { string[] cmd = ((string)json["cmd"]).Split(':'); switch (cmd[0]) { case "DANMU_MSG": return(new Danmaku(json)); case "SUPER_CHAT_MESSAGE": return(new SuperChat(json)); case "SEND_GIFT": return(new Gift(json)); case "COMBO_SEND": return(new ComboSend(json)); case "WELCOME": return(new Welcome(json)); case "WELCOME_GUARD": return(new WelcomeGuard(json)); case "GUARD_BUY": return(new GuardBuy(json)); case "INTERACT_WORD": return(new InteractWord(json)); case "ROOM_BLOCK_MSG": return(new RoomBlock(json)); case "LIVE": case "PREPARING": case "SPECIAL_GIFT": case "USER_TOAST_MSG": case "GUARD_MSG": case "GUARD_LOTTERY_START": case "ENTRY_EFFECT": case "SYS_MSG": case "ROOM_RANK": case "TV_START": case "NOTICE_MSG": case "SYS_GIFT": case "ROOM_REAL_TIME_MESSAGE_UPDATE": return(new Raw((Cmds)Enum.Parse(typeof(Cmds), json["cmd"]), json)); default: return(new Raw(Cmds.UNKNOW, json)); } } catch (Exception ex) { Console.WriteLine(ex); return(null); } }
private DanmakuServer GetDanmakuServer(long roomId) { roomId = GetRealRoomId(roomId); if (roomId < 0) { return(null); } try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://api.live.bilibili.com/room/v1/Danmu/getConf?room_id=" + roomId); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Json.Value json = Json.Parser.Parse(response.GetResponseStream()); if (json["code"] != 0) { Console.Error.WriteLine("Error occurs when resolving dm servers"); Console.Error.WriteLine(json.ToString()); return(null); } DanmakuServer danmakuServer = new DanmakuServer(); danmakuServer.RoomId = roomId; danmakuServer.Server = json["data"]["host_server_list"][0]["host"]; danmakuServer.Port = json["data"]["host_server_list"][0]["port"]; danmakuServer.WsPort = json["data"]["host_server_list"][0]["ws_port"]; danmakuServer.WssPort = json["data"]["host_server_list"][0]["wss_port"]; danmakuServer.Token = json["data"]["token"]; return(danmakuServer); } catch (WebException) { ConnectionFailed?.Invoke("直播间信息获取失败"); return(null); } }
static void Main(string[] args) { // Value Json.Value jsonValue; Console.WriteLine(" [JsonString]"); jsonValue = "abc"; Console.WriteLine(jsonValue == "abc"); Console.WriteLine(jsonValue != "def"); Console.WriteLine("abc" == jsonValue); Console.WriteLine("def" != jsonValue); Console.WriteLine(); Console.WriteLine(" [JsonBoolean]"); jsonValue = true; Console.WriteLine(jsonValue == true); Console.WriteLine(jsonValue != false); Console.WriteLine(true == jsonValue); Console.WriteLine(false != jsonValue); Console.WriteLine(); Console.WriteLine(" [JsonNull]"); jsonValue = new Json.Value.Null(); Console.WriteLine(jsonValue == null); Console.WriteLine(null == jsonValue); Console.WriteLine(); Console.WriteLine(" [JsonNumber]"); jsonValue = 123; Console.WriteLine(jsonValue == 123); Console.WriteLine(jsonValue == 123.0); Console.WriteLine(jsonValue != 456); Console.WriteLine(jsonValue != 456.0); Console.WriteLine(123 == jsonValue); Console.WriteLine(123.0 == jsonValue); Console.WriteLine(456 != jsonValue); Console.WriteLine(456.0 != jsonValue); Console.WriteLine(); Console.WriteLine(" [JsonArray]"); jsonValue = new List <Json.Value>(new Json.Value[] { 123, 456, 789 }); foreach (int i in (List <Json.Value>)jsonValue) { Console.Write(i + " "); } Console.WriteLine(); jsonValue = new List <Json.Value>(new Json.Value[] { "abc", "def", "ghi" }); foreach (string i in (List <Json.Value>)jsonValue) { Console.Write(i + " "); } Console.WriteLine(); jsonValue = new List <Json.Value>(new Json.Value[] { "str", true, 001 }); Console.Write((string)jsonValue[0] + " "); Console.Write((bool)jsonValue[1] + " "); Console.Write((int)jsonValue[2] + " "); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(" [JsonObject]"); Dictionary <string, Json.Value> dic = new Dictionary <string, Json.Value>(); dic.Add("a", "str"); dic.Add("b", true); dic.Add("c", 001); jsonValue = dic; Console.Write((string)jsonValue["a"] + " "); Console.Write((bool)jsonValue["b"] + " "); Console.Write((int)jsonValue["c"] + " "); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(" [JsonArrayMethods]"); jsonValue = new Json.Value.Array(); jsonValue.Add("abc"); jsonValue.Add(true); jsonValue.Add(123); jsonValue.Add(456); Console.WriteLine(jsonValue.ToString()); jsonValue.Remove(2); jsonValue.Insert(0, "def"); Console.WriteLine(jsonValue.ToString()); Console.WriteLine(jsonValue.Count); Console.WriteLine((string)jsonValue[1]); Console.WriteLine(); Console.WriteLine(" [JsonObjectMethods]"); jsonValue = new Json.Value.Object(); jsonValue.Add("a", 123); jsonValue.Add("b", 456); jsonValue.Add("c", 789); Console.WriteLine(jsonValue.ToString()); jsonValue.Remove("b"); Console.WriteLine(jsonValue.ToString()); Console.WriteLine(jsonValue.Count); Console.WriteLine(jsonValue.Contains("a")); Console.WriteLine((double)jsonValue["a"]); Console.WriteLine(); jsonValue = null; if (!File.Exists("Test.json")) { Console.Write(" ([Test.json] not found, press Enter to quit)"); Console.ReadLine(); return; } using (FileStream stream = new FileStream("Test.json", FileMode.Open)) { Console.WriteLine("File Size: {0} MB", (double)stream.Length / 1024 / 1024); Console.WriteLine(); Stopwatch stopwatch = new Stopwatch(); while (true) { stream.Position = 0; // Parse Console.Write(" (Ready, press Enter to parse)"); Console.ReadLine(); Console.WriteLine(); Console.WriteLine(" [Parse]"); stopwatch.Reset(); stopwatch.Start(); Json.Value value = Json.Parser.Parse(stream); stopwatch.Stop(); Console.Write("Elapsed time: {0} ms", stopwatch.ElapsedMilliseconds); // Generate Console.Write(" (Ready, press Enter to generate)"); Console.ReadLine(); Console.WriteLine(); Console.WriteLine(" [Generate]"); stopwatch.Reset(); stopwatch.Start(); string json = value.ToString(); stopwatch.Stop(); Console.Write("Elapsed time: {0} ms", stopwatch.ElapsedMilliseconds); json = null; value = null; GC.Collect(); } } }