-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
43 lines (38 loc) · 1.71 KB
/
Program.cs
File metadata and controls
43 lines (38 loc) · 1.71 KB
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
using System;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CafeteriaApp
{
class CafeteriaClient
{
static async Task Main(string[] args)
{
using (ClientWebSocket clientWebSocket = new ClientWebSocket())
{
await clientWebSocket.ConnectAsync(new Uri("ws://localhost:9090"), CancellationToken.None);
Console.WriteLine("Conectado al servidor de Cafetería.");
// Bucle para recibir mensajes del servidor
_ = Task.Run(async () =>
{
while (true)
{
ArraySegment<byte> receiveBuffer = new ArraySegment<byte>(new byte[1024]);
WebSocketReceiveResult result = await clientWebSocket.ReceiveAsync(receiveBuffer, CancellationToken.None);
string receivedMessage = Encoding.UTF8.GetString(receiveBuffer.Array, 0, result.Count);
Console.WriteLine($"Mensaje del servidor: {receivedMessage}");
}
});
// Bucle para que los estudiantes entren en la cafetería
while (true)
{
Console.WriteLine("Presione 'Enter' para entrar a la cafetería...");
Console.ReadLine();
byte[] enterMessage = Encoding.UTF8.GetBytes("Quiero entrar a la cafetería");
await clientWebSocket.SendAsync(new ArraySegment<byte>(enterMessage), WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
}
}