Kaynağa Gözat

通讯连接测试

样式分支
pry 2 yıl önce
ebeveyn
işleme
88bce7dd75
2 değiştirilmiş dosya ile 23 ekleme ve 13 silme
  1. +1
    -1
      BPASmart.Server/CommunicationServer.cs
  2. +22
    -12
      Communication/RedisHelper.cs

+ 1
- 1
BPASmart.Server/CommunicationServer.cs Dosyayı Görüntüle

@@ -11,7 +11,7 @@ namespace BPASmart.Server
public void Init()
{
BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog("通讯模块初始化");
RedisHelper.GetInstance.Connect();
RedisHelper.GetInstance.ConnectAsync();
Json<CommunicationPar>.Data.CommunicationDevices.ToList()?.ForEach(item =>
{
ThreadManage.GetInstance().Start(new Action(() =>


+ 22
- 12
Communication/RedisHelper.cs Dosyayı Görüntüle

@@ -1,4 +1,5 @@
using ServiceStack.Redis;
using System.Diagnostics;

namespace Communication
{
@@ -10,19 +11,28 @@ namespace Communication
private RedisHelper() { }
RedisClient client;

public void Connect()
public async Task<bool> ConnectAsync()
{
if (client == null)
{
client = new RedisClient("124.222.238.75", 16000, "123456", 1);
Thread.Sleep(1000);
//while (!client.HasConnected)
//{
// client = new RedisClient("124.222.238.75", 16000, "123456");
// Thread.Sleep(1000);
//}
}
if (client.HasConnected) BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog("Redis 连接成功");
return await Task.Factory.StartNew(new Func<bool>(() =>
{
if (client == null)
{
BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog("开始连接 Redis");
client = new RedisClient("124.222.238.75", 16000, "123456", 1);
client.ConnectTimeout = 5000;
Stopwatch sw = new Stopwatch();
sw.Start();
while (!client.IsSocketConnected())
{
if (sw.ElapsedMilliseconds >= client.ConnectTimeout) break;
Thread.Sleep(1000);
}
string status = client.IsSocketConnected() ? "成功" : "失败";
BPASmartClient.Message.MessageLog.GetInstance.ShowDebugLog($"Redis 连接{status}");
}
return client.IsSocketConnected();
}));

}

/// <summary>


Yükleniyor…
İptal
Kaydet