-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathAppP2pNotify.cs
201 lines (185 loc) · 6.53 KB
/
AppP2pNotify.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DigitalPlatform.MessageClient;
namespace DigitalPlatform.LibraryServer
{
/// <summary>
/// 和点对点通知有关的功能
/// 点对点通讯用到了 dp2mserver 服务器
/// </summary>
public partial class LibraryApplication
{
internal P2PConnection _connection = null;
internal string _mserverUrl = "";
internal string _mserverUserName = "";
internal string _mserverPassword = "";
#if NO
public class OpenConnectionResult : NormalResult
{
public P2PConnection Connection { get; set; }
}
// 获得一个连接
public async Task<OpenConnectionResult> OpenConnectionAsync(
string url,
string userName,
string password)
{
// P2PConnection connection = null;
if (connection != null)
{
// 尝试连接一次
if (connection.IsDisconnected == true)
{
await EnsureConnectMessageServerAsync(
connection,
userNameAndUrl);
}
return new OpenConnectionResult
{
Value = 0,
Connection = connection
};
}
if (_connection == null)
_connection = new P2PConnection();
else
{
_connection.CloseConnection();
_connection = new P2PConnection();
}
var result = await _connection.ConnectAsync(url,
userName,
password,
"");
if (result.Value == -1)
return new OpenConnectionResult
{
Value = -1,
ErrorInfo = result.ErrorInfo,
ErrorCode = result.ErrorCode
};
return new OpenConnectionResult
{
Value = 0,
Connection = _connection
};
}
#endif
public void CloseMessageConnection()
{
if (_connection != null)
{
_connection.CloseConnection();
_connection.AddMessage -= _connection_AddMessage;
_connection = null;
}
}
// 确保连接到消息服务器
public async Task<NormalResult> EnsureConnectMessageServerAsync(
int timeout,
CancellationToken token)
{
if (string.IsNullOrEmpty(_mserverUrl)
|| string.IsNullOrEmpty(_mserverUserName))
{
/*
if (_connection != null)
{
_connection.CloseConnection();
_connection.AddMessage -= _connection_AddMessage;
_connection = null;
}
*/
CloseMessageConnection();
return new NormalResult
{
Value = -1,
ErrorInfo = "点对点消息功能尚未启用",
ErrorCode = "notEnabled"
};
}
if (_connection == null)
{
_connection = new P2PConnection();
_connection.AddMessage += _connection_AddMessage;
}
if (_connection.IsDisconnected)
{
return await _connection.ConnectAsync(_mserverUrl,
_mserverUserName,
_mserverPassword,
"",
timeout,
token);
}
return new NormalResult();
}
private void _connection_AddMessage(object sender, AddMessageEventArgs e)
{
_ = Task.Run(async () =>
{
try
{
if (e.Action == "create")
{
foreach (var message in e.Records)
{
// 响应 dp2ssl 发来的 hello 消息
// "gn:_62637a12-1965-4876-af3a-fc1d3009af8a"
if (message.groups[0] == $"gn:_dp2library_{this.UID}"
&& message.data == "hello, dp2library")
{
var message_sender = message.creator;
var send_result = await _connection.SetMessageAsyncLite(new SetMessageRequest {
Action = "create",
Records = new List<MessageRecord> {
new MessageRecord{
groups = new string [] { message.groups[0] },
subjects = new string [] { "hello" },
data = $"hello, {message_sender}",
expireTime = DateTime.Now + TimeSpan.FromMinutes(5) // 5 分钟以后消息自动失效
}
},
Style = "dontNotifyMe",
});
}
}
}
}
catch (Exception ex)
{
this.WriteErrorLog($"_connection_AddMessage() 出现异常: {ExceptionUtil.GetDebugText(ex)}");
}
});
}
public bool MessageServerConnected
{
get
{
return _connection != null;
}
}
public Task<SetMessageResult> SendMessageAsync(string[] groups,
string subject,
string content)
{
if (_connection == null)
return Task.FromResult<SetMessageResult>(new SetMessageResult());
SetMessageRequest request = new SetMessageRequest("create",
"dontNotifyMe",
new List<MessageRecord> {
new MessageRecord {
groups= groups,
subjects = new string [] { subject },
data = content,
expireTime = DateTime.Now + TimeSpan.FromMinutes(5) // 5 分钟以后消息自动失效
}
});
return _connection.SetMessageAsyncLite(request);
}
}
}