[ADDITION] tried to fix the async methods.

This commit is contained in:
Dogukan
2020-10-22 16:11:19 +02:00
parent 5b5d66c41b
commit 3b667b3f0c
4 changed files with 39 additions and 14 deletions

View File

@@ -48,6 +48,9 @@ namespace Client
private void OnReadComplete(IAsyncResult ar)
{
if (ar == null || (!ar.IsCompleted) || (!this.stream.CanRead) || !this.tcpClient.Client.Connected)
return;
int amountReceived = stream.EndRead(ar);
if (totalBufferReceived + amountReceived > 1024)
@@ -140,6 +143,9 @@ namespace Client
case JSONConvert.RANDOMWORD:
//Flag byte for receiving the random word.
int lobbyId = JSONConvert.GetLobbyID(payload);
if(data.Lobby?.ID == lobbyId)
ViewModels.ViewModelGame.HandleRandomWord(JSONConvert.GetRandomWord(payload));
break;
default:

View File

@@ -9,6 +9,8 @@ using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using static SharedClientServer.JSONConvert;
namespace Server.Models
@@ -181,13 +183,15 @@ namespace Server.Models
int id = JSONConvert.GetLobbyID(payload);
ServerCommunication.INSTANCE.JoinLobby(this.User, id);
sendMessage(JSONConvert.ConstructLobbyJoinSuccessMessage());
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
serverCom.SendToLobby(serverCom.GetLobbyForUser(User), JSONConvert.GetMessageToSend(RANDOMWORD, new
Task.Run(SendLobbyData);
serverCom.sendToAll(JSONConvert.GetMessageToSend(JSONConvert.RANDOMWORD, new
{
id = serverCom.GetLobbyForUser(User).ID,
word = JSONConvert.SendRandomWord("WordsForGame.json")
}));
ServerCommunication.INSTANCE.sendToAll(JSONConvert.ConstructLobbyListMessage(ServerCommunication.INSTANCE.lobbies.ToArray()));
break;
case LobbyIdentifier.LEAVE:
id = JSONConvert.GetLobbyID(payload);
@@ -198,6 +202,20 @@ namespace Server.Models
}
}
private async void SendLobbyData()
{
string result = await WaitForData();
if(result == "bruh momento")
{
}
}
private async Task<string> WaitForData()
{
await Task.Delay(1000);
return "bruh momento";
}
/// <summary>
/// sends a message to the tcp client
/// </summary>

View File

@@ -90,7 +90,7 @@ namespace Server.Models
/// send a message to all tcp clients in the list
/// </summary>
/// <param name="message">the message to send</param>
public void sendToAll(byte[] message)
public async void sendToAll(byte[] message)
{
foreach (ServerClient sc in serverClients)
{

View File

@@ -184,6 +184,7 @@ namespace SharedClientServer
int index = random.Next(0, 24);
Debug.WriteLine($"[SERVERCLIENT] Sending random words {words}");
return words.words[index];
}