2024-11-29 09:03:54 +08:00

76 lines
2.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
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地址");
Console.WriteLine("直接回车默认使用127.0.0.1");
string ip = Console.ReadLine();
if (string.IsNullOrWhiteSpace(ip))
{
ip = "127.0.0.1";
}
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 消息
var entity = MessagePackSerializer.Deserialize<Dictionary<string, object>>(message, MessagePack.Resolvers.ContractlessStandardResolver.Options);
try
{
Console.WriteLine("解析到的键值对:");
foreach (var kvp in entity)
{
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}
}
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; }
}
}