private void ReceiveMessageLoop() { // lock (shit_lock) // //ReceiveMessageLoop 似乎好像大概會同時運行兩個的bug, 但是不修了, 鎖上算了 // { try { var stableBuffer = new byte[Client.ReceiveBufferSize]; while (this.Connected) { NetStream.ReadB(stableBuffer, 0, 4); var packetlength = BitConverter.ToInt32(stableBuffer, 0); packetlength = IPAddress.NetworkToHostOrder(packetlength); if (packetlength < 16) { throw new NotSupportedException("协议失败: (L:" + packetlength + ")"); } NetStream.ReadB(stableBuffer, 0, 2);//magic NetStream.ReadB(stableBuffer, 0, 2);//protocol_version NetStream.ReadB(stableBuffer, 0, 4); var typeId = BitConverter.ToInt32(stableBuffer, 0); typeId = IPAddress.NetworkToHostOrder(typeId); Console.WriteLine(typeId); NetStream.ReadB(stableBuffer, 0, 4);//magic, params? var playloadlength = packetlength - 16; if (playloadlength == 0) { continue;//没有内容了 } typeId = typeId - 1;//和反编译的代码对应 var buffer = new byte[playloadlength]; NetStream.ReadB(buffer, 0, playloadlength); switch (typeId) { case 0: case 1: case 2: { var viewer = BitConverter.ToUInt32(buffer.Take(4).Reverse().ToArray(), 0); //观众人数 Console.WriteLine(viewer); if (ReceivedRoomCount != null) { ReceivedRoomCount(this, new ReceivedRoomCountArgs() { UserCount = viewer }); } break; } case 3: case 4://playerCommand { var json = Encoding.UTF8.GetString(buffer, 0, playloadlength); if (debuglog) { Console.WriteLine(json); } try { DanmakuModel dama = new DanmakuModel(json, 2); if (ReceivedDanmaku != null) { ReceivedDanmaku(this, new ReceivedDanmakuArgs() { Danmaku = dama }); } } catch (Exception) { // ignored } break; } case 5://newScrollMessage { break; } case 7: { break; } case 16: { break; } default: { break; } // } } } catch (NotSupportedException ex) { this.Error = ex; _disconnect(); } catch (Exception ex) { this.Error = ex; _disconnect(); } // } }
/// <summary> /// 添加直播滚动弹幕 /// </summary> /// <param name="text">参数</param> /// <param name="own">是否自己发送的</param> /// <param name="color">颜色</param> public void AddLiveDanmu(string text, bool own, Color?color) { if (color == null) { color = Colors.White; } var m = new DanmakuModel() { text = text, color = color.Value, location = DanmakuLocation.Roll, size = 25 }; Grid grid = null; switch (borderStyle) { case DanmakuBorderStyle.Default: grid = CreateControlBorder(m); break; case DanmakuBorderStyle.NoBorder: grid = CreateControlNoBorder(m); break; case DanmakuBorderStyle.Shadow: grid = CreateControlShadow(m); break; default: break; } if (own) { grid.BorderBrush = new SolidColorBrush(color.Value); grid.BorderThickness = new Thickness(1); } var r = ComputeRollRow(); if (r == -1) { return; } Grid.SetRow(grid, r); grid.HorizontalAlignment = HorizontalAlignment.Left; grid.VerticalAlignment = VerticalAlignment.Center; grid_Roll.Children.Add(grid); grid_Roll.UpdateLayout(); TranslateTransform moveTransform = new TranslateTransform(); moveTransform.X = gv.ActualWidth; grid.RenderTransform = moveTransform; //创建动画 Duration duration = new Duration(TimeSpan.FromSeconds(speed)); DoubleAnimation myDoubleAnimationX = new DoubleAnimation(); myDoubleAnimationX.Duration = duration; //创建故事版 Storyboard moveStoryboard = new Storyboard(); moveStoryboard.Duration = duration; myDoubleAnimationX.To = -(grid.ActualWidth);//到达 moveStoryboard.Children.Add(myDoubleAnimationX); Storyboard.SetTarget(myDoubleAnimationX, moveTransform); //故事版加入动画 Storyboard.SetTargetProperty(myDoubleAnimationX, "X"); rollStoryList.Add(moveStoryboard); moveStoryboard.Completed += new EventHandler <object>((senders, obj) => { grid_Roll.Children.Remove(grid); rollStoryList.Remove(moveStoryboard); }); moveStoryboard.Begin(); }