65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
![]() |
using System;
|
|||
|
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地址:");
|
|||
|
string ip = Console.ReadLine();
|
|||
|
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 消息
|
|||
|
dynamic entity = MessagePackSerializer.Deserialize<dynamic>(message, MessagePack.Resolvers.ContractlessStandardResolver.Options);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
Console.WriteLine("收到消息");
|
|||
|
}
|
|||
|
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; }
|
|||
|
}
|
|||
|
}
|