-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRootMain.cs
238 lines (200 loc) · 6.73 KB
/
RootMain.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
using NapCatSprcit.MessagesService;
using NapCatSprcit.MessagesService.MessagesProcess;
using NapCatSprcit.WebSocketConnection;
namespace NapCatSprcit
{
public class RootMain
{
public static bool isOpen = true;
private static List<string> messagesList = new List<string>();
static void Main(string[] args)
{
//if (args.Count() > 0)
//{
// PublicProperty.WebSocketURI = args[0];
// PublicProperty.HttpURI = args[1];
//}
//else
//{
// do
// {
// #region 测试放开
// Console.WriteLine("请输入WebSocketURI");
// string? w = Console.ReadLine();
// if (w == null)
// {
// continue;
// }
// PublicProperty.WebSocketURI = w;
// if (PublicProperty.WebSocketURI == null)
// continue;
// Console.WriteLine("请输入HttpURI");
// string? r = Console.ReadLine();
// if (r == null)
// {
// continue;
// }
// PublicProperty.HttpURI = r;
// #endregion
// } while (PublicProperty.HttpURI == "" || PublicProperty.WebSocketURI == "");
//}
PublicProperty.Connection = new Connection();
PublicProperty.Connection.InitionWebSocket().GetAwaiter().GetResult();
PublicProperty.Messages = new Messages();
//接收消息
Task.Run(async () =>
{
Console.WriteLine("RootMain : 开始接收消息");
while (true)
{
IAsyncEnumerable<string> strs = PublicProperty.Messages.ReceiveMes();
await foreach (string str in strs)
{
messagesList.Add(str);
}
}
});
//判断结束
Task.Run(async () =>
{
while (true)
{
string? e = Console.ReadLine();
if (e != null && (e == "quit" || e == "exit"))
{
await PublicProperty.Connection.CloseWebSocket();
Console.WriteLine("bye -=-");
Environment.Exit(0);
}
}
});
Task.Run(async () =>
{
while(true)
{
if(messagesList.Count > 0)
{
await ProcessClass.MessagesProcess(messagesList);
}
}
});
#region 基础消息发送
/*
Task.Run(async () =>
{
int isExit = 0;
while (true)
{
await Task.Delay(1000);
string content = await messages.SendPostMessagesAsync("2375948110", "Hello");
if(!string.IsNullOrEmpty(content))
{
Console.WriteLine("发送结果:");
Console.WriteLine(content + "\n");
}
isExit++;
if (isExit > 3)
return;
}
});
*/
#endregion
while (true)
{
//if (messagesList.Count > 0)
//{
// Console.WriteLine(messagesList[0] + "\n");
// messagesList.RemoveAt(0);
//}
}
}
}
}
#region 其实也是正确的
/*
using System.Net;
using System.Net.WebSockets;
using System.Text;
using System.Text.Json.Nodes;
namespace NapCatSprcit
{
internal class Program
{
static void Main(string[] args)
{
Conn();
while (true)
{ }
}
public static async void Conn()
{
using (ClientWebSocket webSocket = new ClientWebSocket())
{
//accessToken
//webSocket.Options.SetRequestHeader("Authorization", "Bearer nantingttt");
Uri uri = new Uri("ws://122.11.29.203:3001");
CancellationToken cts = new CancellationToken();
//链接服务
await webSocket.ConnectAsync(uri, cts);
Console.WriteLine("等待");
Console.WriteLine("开启");
byte[] str = new byte[byte.Parse("2")];
var receiveData = new ArraySegment<byte>(str);
var stb = new StringBuilder();
while (true)
{
webSocket.ReceiveAsync(receiveData, cts);
await Task.Delay(22);
stb.Append(Encoding.ASCII.GetString(receiveData.ToArray(), receiveData.Offset, receiveData.Count));
if(stb.ToString() is not "") Console.WriteLine(stb.ToString() + "\n\n");
stb.Clear();
}
await webSocket.CloseAsync(webSocket.CloseStatus.Value, "wu", cts);
}
}
}
}
*/
#endregion
#region 正确的
//Task.Run(async () =>
//{
// using (var clink = new ClientWebSocket())
// {
// var ct = new CancellationToken();
// do
// {
// await clink.ConnectAsync(new Uri("ws://122.11.29.203:3001"), ct);
// Console.WriteLine("握手");
// } while (clink.State != WebSocketState.Open);
// Console.WriteLine("握手完成");
// Console.WriteLine(clink.State);
// byte[] bytes = new byte[1024 * 10];
// var ars = new ArraySegment<byte>(bytes);
// while (true)
// {
// if(isOpen == false)
// {
// await clink.CloseAsync(clink.CloseStatus.Value,"hand",ct);
// Console.WriteLine("已经关闭");
// }
// WebSocketReceiveResult result = await clink.ReceiveAsync(ars, ct);
// if (result.EndOfMessage)
// {
// Console.WriteLine(result.MessageType.ToString());
// string jsonStr = Encoding.UTF8.GetString(bytes, ars.Offset, ars.Count);
// //JsonNode jsonObject = JsonObject.Parse(jsonStr);
// Console.WriteLine(jsonStr);
// }
// }
// }
//});
//while (true)
//{
// string? r = Console.ReadLine();
// if (r != null && r == "quit")
// {
// isOpen = false;
// }
//}
#endregion