-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.cs
183 lines (144 loc) · 6.17 KB
/
Client.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
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using LeanCloud;
using LeanCloud.Storage;
using LeanCloud.Realtime;
namespace ChatStack
{
public class Client
{
public bool Beep = true;
LCIMClient UserClient;
LCIMConversation Chatroom;
Dictionary<string, string> UsernameIndex = new Dictionary<string, string>();
// TODO: Detect if the user is typing.
// Queue<LCIMTextMessage> MessageBuffer = new Queue<LCIMTextMessage>();
public static void InitApp(string appId, string appKey, string url = null)
{
// Initialize the app with the LeanCloud credentials.
LCApplication.Initialize(appId, appKey, url);
}
public static async Task<LCUser> LoginUserAsync(string username, string password)
{
// Check if the username is an email address.
if (new System.ComponentModel.DataAnnotations.EmailAddressAttribute().IsValid(username))
{
return await LCUser.LoginByEmail(username, password);
}
return await LCUser.Login(username, password);
}
public async Task LoginClientAsync(string username, string password)
{
// Log in to client using LCUser.
UserClient = new LCIMClient(await LoginUserAsync(username, password));
await InitClientAsync();
}
public async Task InitClientAsync()
{
// Initiate the client with the server.
await UserClient.Open();
}
public async Task GetChatroomAsync()
{
// Query the ChatStack chatroom.
var conversationQuery = UserClient.GetQuery()
.WhereEqualTo("name", "ChatStack")
.WhereEqualTo("tr", true);
var queryResult = await conversationQuery.Find();
Chatroom = queryResult[0];
}
public async Task StartChatAsync()
{
// Join the chat.
await Chatroom.Join();
// TODO: Fetch previous messages.
// Print the prompt and the number of online users in the chatroom.
Console.WriteLine("You are now in the chat. Type /exit or /quit to exit.");
Console.WriteLine(await Chatroom.GetMembersCount() + " people online.");
// Listen for messages.
UserClient.OnMessage = async (_, message) =>
{
// If the message received is text, assign it to textMessage.
if (message is LCIMTextMessage textMessage)
{
if (!UsernameIndex.ContainsKey(textMessage.FromClientId))
{
// Query and save the sender's username if it's not in the index.
LCQuery<LCUser> senderQuery = LCUser.GetQuery();
UsernameIndex[textMessage.FromClientId] = (await senderQuery.Get(textMessage.FromClientId)).Username;
}
// TODO: Detect if the user is typing.
// // Peek the input stream without reading it.
// if ((new StreamReader(Console.OpenStandardInput()).Peek() == -1))
// {
// // If the user hasn't typed anything, print the message.
// PrintMessage(textMessage);
// // Print the prompt again.
// Console.Write("> ");
// }
// else
// {
// // If the user is typing, save the message to the buffer.
// MessageBuffer.Enqueue(textMessage);
// }
PrintMessage(textMessage);
}
};
// Ask for message input constantly.
await AskForMessageInputAsync();
}
async Task AskForMessageInputAsync()
{
while (true)
{
// Print prompt and get input.
Console.Write("> ");
string input = await Task.Run(() => Console.ReadLine().Trim());
// TODO: Detect if the user is typing.
// Clear message buffer.
// while (MessageBuffer.Count > 0)
// PrintMessage(MessageBuffer.Dequeue());
// Check the input for exit commands.
if (input == "/exit" || input == "/quit") break;
// Send the message if the input is not empty.
if (!string.IsNullOrWhiteSpace(input))
{
_ = await Chatroom.Send(new LCIMTextMessage(input));
}
}
}
void PrintMessage(LCIMTextMessage message)
{
// Play a beep sound if beep is enabled.
if (Beep)
Console.Beep();
// TODO: Detect if the user is typing.
// Set cursor position to the beginning of the line.
// Console.SetCursorPosition(0, Console.CursorTop);
Console.WriteLine();
// Print the message sender's username and the message content.
Console.WriteLine($"* {UsernameIndex[message.FromClientId]}: {message.Text}");
// Print the prompt again.
Console.Write("> ");
}
// public static void DebugLogger(LCLogLevel level, string info)
// {
// switch (level)
// {
// case LCLogLevel.Debug:
// Console.WriteLine("[DEBUG] " + info);
// break;
// case LCLogLevel.Warn:
// Console.WriteLine("[WARNING] " + info);
// break;
// case LCLogLevel.Error:
// Console.WriteLine("[ERROR] " + info);
// break;
// default:
// Console.WriteLine(info);
// break;
// }
// }
}
}