2025-03-10 18:15:27 +08:00
|
|
|
|
using MessagePack;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
using System;
|
2024-11-29 09:03:54 +08:00
|
|
|
|
using System.Collections.Generic;
|
2024-11-26 13:45:28 +08:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Threading.Channels;
|
|
|
|
|
using static System.Runtime.InteropServices.JavaScript.JSType;
|
|
|
|
|
|
|
|
|
|
class Subscriber
|
|
|
|
|
{
|
|
|
|
|
static void Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("请输入redis的ip地址:");
|
2024-11-29 09:03:54 +08:00
|
|
|
|
Console.WriteLine("直接回车默认使用127.0.0.1");
|
|
|
|
|
|
2024-11-26 13:45:28 +08:00
|
|
|
|
string ip = Console.ReadLine();
|
2024-11-29 09:03:54 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(ip))
|
|
|
|
|
{
|
|
|
|
|
ip = "127.0.0.1";
|
|
|
|
|
}
|
2024-11-26 13:45:28 +08:00
|
|
|
|
var connectStr = $"{ip}:36379,defaultDatabase=0,password=yunda123";
|
|
|
|
|
var configurationOptions = new ConfigurationOptions
|
|
|
|
|
{
|
|
|
|
|
EndPoints = { $"{ip}:36379" },
|
|
|
|
|
ConnectTimeout = 20000,
|
|
|
|
|
AsyncTimeout = 20000,
|
|
|
|
|
SyncTimeout = 20000,
|
|
|
|
|
DefaultDatabase = 0,
|
|
|
|
|
Password = "yunda123",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var conmper = ConnectionMultiplexer.Connect(configurationOptions);
|
|
|
|
|
var confOption = ConfigurationOptions.Parse(connectStr);
|
|
|
|
|
var server = conmper.GetServer(confOption.EndPoints[0]);
|
|
|
|
|
var sub = conmper.GetSubscriber();
|
|
|
|
|
Console.WriteLine("请输入redis的channel地址:");
|
|
|
|
|
string channel = Console.ReadLine();
|
|
|
|
|
|
|
|
|
|
long receivedCount = 0;
|
|
|
|
|
DateTime startTime = DateTime.Now;
|
|
|
|
|
|
|
|
|
|
sub.Subscribe(channel, (channel, message) =>
|
|
|
|
|
{
|
|
|
|
|
receivedCount++;
|
|
|
|
|
|
|
|
|
|
// 解析 JSON 消息
|
2024-11-29 09:03:54 +08:00
|
|
|
|
var entity = MessagePackSerializer.Deserialize<Dictionary<string, object>>(message, MessagePack.Resolvers.ContractlessStandardResolver.Options);
|
2024-12-02 14:52:59 +08:00
|
|
|
|
ProcessDictionary(entity);
|
|
|
|
|
|
2024-11-26 13:45:28 +08:00
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Console.WriteLine("按 Enter 键退出程序...");
|
|
|
|
|
Console.ReadLine();
|
|
|
|
|
|
|
|
|
|
conmper.Dispose();
|
|
|
|
|
}
|
2024-12-02 14:52:59 +08:00
|
|
|
|
static void ProcessDictionary(Dictionary<string, object> dictionary)
|
|
|
|
|
{
|
|
|
|
|
foreach (var kvp in dictionary)
|
|
|
|
|
{
|
|
|
|
|
Console.Write($"{kvp.Key}: ");
|
|
|
|
|
if (kvp.Value is byte[] nestedMessagePack)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// 进一步解析嵌套的 MessagePack 数据
|
|
|
|
|
var nestedDict = MessagePackSerializer.Deserialize<Dictionary<string, object>>(nestedMessagePack, MessagePack.Resolvers.ContractlessStandardResolver.Options);
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
ProcessDictionary(nestedDict); // 递归解析
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("(嵌套内容无法解析)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(kvp.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-26 13:45:28 +08:00
|
|
|
|
public class Message
|
|
|
|
|
{
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
public int index { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|