-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from Executor-Cheng/dev/1.0.6
更新至1.0.6
- Loading branch information
Showing
3 changed files
with
67 additions
and
10 deletions.
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
61 changes: 61 additions & 0 deletions
61
src/BililiveNotification/Clients/AnonymousDanmakuClient.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,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); | ||
} | ||
} | ||
} |
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