76 lines
2.2 KiB
C#
Raw Normal View History

2024-11-26 13:45:28 +08:00
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 MessagePack;
using StackExchange.Redis;
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-11-26 13:45:28 +08:00
try
{
2024-11-29 09:03:54 +08:00
Console.WriteLine("解析到的键值对:");
foreach (var kvp in entity)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
2024-11-26 13:45:28 +08:00
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
});
Console.WriteLine("按 Enter 键退出程序...");
Console.ReadLine();
conmper.Dispose();
}
public class Message
{
public string name { get; set; }
public int index { get; set; }
}
}