Skip to content

Commit

Permalink
Merge pull request #7 from Executor-Cheng/dev/1.0.6
Browse files Browse the repository at this point in the history
更新至1.0.6
  • Loading branch information
Executor-Cheng authored Nov 28, 2023
2 parents 607cf3a + 1a6bb34 commit 4681698
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<UseWindowsForms>true</UseWindowsForms>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>

<Version>1.0.5.0</Version>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<PackageVersion>1.0.5.0</PackageVersion>
<Version>1.0.6.0</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<PackageVersion>1.0.6.0</PackageVersion>
</PropertyGroup>
</Project>
61 changes: 61 additions & 0 deletions src/BililiveNotification/Clients/AnonymousDanmakuClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Diagnostics;
using System.Net.Sockets;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Executorlibs.Bilibili.Protocol.Invokers;
using Executorlibs.Bilibili.Protocol.Models;
using Executorlibs.Bilibili.Protocol.Options;
using Executorlibs.Bilibili.Protocol.Services;
using Microsoft.Extensions.Options;
#if NET5_0_OR_GREATER
using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV3;
#else
using Executorlibs.Shared.Extensions;
using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV2;
#endif

namespace BililiveNotification.Clients
{
public class AnonymousDanmakuClient : TcpDanmakuClient
{
public AnonymousDanmakuClient(IBilibiliMessageHandlerInvoker invoker, IBilibiliMessageSubscriptionResolver resolver, IOptionsSnapshot<DanmakuClientOptions> options, IDanmakuServerProvider credentialProvider) : base(invoker, resolver, options, credentialProvider)
{

}

protected override async Task InternalConnectAsync(CancellationToken token)
{
int roomId = _options.RoomId;
DanmakuServerInfo server = await _credentialProvider.GetDanmakuServerInfoAsync(token);
Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
int sendTimeout = (socket.ReceiveTimeout = (int)_options.HeartbeatInterval.TotalMilliseconds + 10000);
socket.SendTimeout = sendTimeout;
token.Register(socket.Dispose);
DanmakuServerHostInfo danmakuServerHostInfo = server.Hosts[(int)(Stopwatch.GetTimestamp() % server.Hosts.Length)];
#if NET5_0_OR_GREATER
await socket.ConnectAsync(danmakuServerHostInfo.Host, danmakuServerHostInfo.Port, token);
#else
await socket.ConnectAsync(danmakuServerHostInfo.Host, danmakuServerHostInfo.Port);
#endif
await socket.SendAsync(CreateNewJoinRoomPayload(roomId, 0, server.Token), SocketFlags.None, token);
_Socket = socket;
}

private byte[] CreateNewJoinRoomPayload(int roomId, long userId, string token)
{
byte[] body = JsonSerializer.SerializeToUtf8Bytes(new
{
uid = userId,
roomid = roomId,
protover = Version,
buvid = $"{Guid.NewGuid()}{new Random().Next(10000, 100000)}infoc",
platform = "web",
type = 2,
key = token
});
return CreatePayload(7, body);
}
}
}
8 changes: 2 additions & 6 deletions src/BililiveNotification/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BililiveNotification.Clients;
using BililiveNotification.Configs;
using Executorlibs.Bilibili.Protocol.Builders;
using Executorlibs.Bilibili.Protocol.Invokers;
Expand All @@ -15,11 +16,6 @@
using System.Windows.Forms;
using Button = System.Windows.Controls.Button;
using MessageBox = System.Windows.MessageBox;
#if NET5_0_OR_GREATER
using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV3;
#else
using TcpDanmakuClient = Executorlibs.Bilibili.Protocol.Clients.TcpDanmakuClientV2;
#endif

namespace BililiveNotification
{
Expand Down Expand Up @@ -53,7 +49,7 @@ public MainWindow()
.AddParser<LiveStartParser>()
.AddParser<LiveEndParser>()
.AddInvoker<BilibiliMessageHandlerInvoker>()
.AddClient<TcpDanmakuClient>()
.AddClient<AnonymousDanmakuClient>()
.AddHandler(services => services.GetRequiredService<RoomMonitor>())
.Services
.AddSingleton<AddRoomWindow>()
Expand Down

0 comments on commit 4681698

Please sign in to comment.