Skip to content

Commit

Permalink
More on websocket.
Browse files Browse the repository at this point in the history
Settings now works.
  • Loading branch information
SlejmUr committed Dec 26, 2024
1 parent 0c89531 commit d57ada0
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 118 deletions.
63 changes: 63 additions & 0 deletions csharp/Client/ChatWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
#if CLIENT || GAME

using EIV_Common.Coroutines;
using EIV_JsonLib.Lobby;
using System.Buffers;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text.Json;
using System.Text;
using System.Threading;
using System;

namespace ExtractIntoVoid.Client;

public class ChatWebSocket
{
public Queue<ChatMessage> MessageQueue = new();
ClientWebSocket webSocket = new();
private static ChatWebSocket instance;
public static ChatWebSocket Instance
{
get
{
if (instance == null)
instance = new();
return instance;
}
}

public void Connect(string ip_port, string ticket)
{
webSocket.Options.SetRequestHeader("authorization", ticket);
webSocket.ConnectAsync(new($"ws://{ip_port}/EIV_Lobby/Socket/Chat"), CancellationToken.None);
CoroutineWorkerNode.CallPeriodically(TimeSpan.FromSeconds(1), Receive, CoroutineType.Process, "ChatWebSocket");
}

public void Send(ChatMessage clientSocketMessage)
{
if (webSocket.State != WebSocketState.Open)
return;
webSocket.SendAsync(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(clientSocketMessage)), WebSocketMessageType.Binary, false, CancellationToken.None);
}

public ChatMessage SendAndReceive(ChatMessage clientSocketMessage)
{
Send(clientSocketMessage);
Receive();
if (MessageQueue.TryDequeue(out var message))
return message;
return null;
}

public void Receive()
{
byte[] bytes = ArrayPool<byte>.Shared.Rent(2048);
var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result;
if (res.MessageType == WebSocketMessageType.Close)
return;
MessageQueue.Enqueue(JsonSerializer.Deserialize<ChatMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count)));
ArrayPool<byte>.Shared.Return(bytes);
}

public void Quit()
{
CoroutineWorkerNode.KillCoroutineTagInstance("ChatWebSocket");
MessageQueue.Clear();
webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
webSocket.Dispose();
}
}
#endif
4 changes: 2 additions & 2 deletions csharp/Client/GameSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace ExtractIntoVoid.Client;
public partial class GameSettings : Resource
{
[Export]
public VisualSettings Visual { get; set; } = new();
public VideoSettings Video { get; set; } = new();
}

public partial class VisualSettings : Resource
public partial class VideoSettings : Resource
{

[Export]
Expand Down
19 changes: 9 additions & 10 deletions csharp/Client/UserWebSocket.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if CLIENT || GAME
using EIV_Common.Coroutines;
using EIV_JsonLib.Lobby;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Net.WebSockets;
Expand All @@ -11,6 +13,7 @@ namespace ExtractIntoVoid.Client;

public class UserWebSocket
{
public Queue<ClientSocketMessage> MessageQueue = new();
ClientWebSocket webSocket = new();
private static UserWebSocket instance;
public static UserWebSocket Instance
Expand All @@ -23,12 +26,11 @@ public static UserWebSocket Instance
}
}

public Queue<ClientSocketMessage> MessageQueue = new();

public void Connect(string ip_port, string guid, string ticket)
{
webSocket.Options.SetRequestHeader("authorization", ticket);
webSocket.ConnectAsync(new($"ws://{ip_port}/Socket/Client/{guid}"), CancellationToken.None);
CoroutineWorkerNode.CallPeriodically(TimeSpan.FromSeconds(1), Receive, CoroutineType.Process, "UserWebSocket");
}

public void Send(ClientSocketMessage clientSocketMessage)
Expand All @@ -41,16 +43,12 @@ public void Send(ClientSocketMessage clientSocketMessage)
public ClientSocketMessage SendAndReceive(ClientSocketMessage clientSocketMessage)
{
Send(clientSocketMessage);
byte[] bytes = ArrayPool<byte>.Shared.Rent(2048);
var res = webSocket.ReceiveAsync(bytes, CancellationToken.None).Result;
if (res.MessageType == WebSocketMessageType.Close)
return null;
ClientSocketMessage resp = JsonSerializer.Deserialize<ClientSocketMessage>(Encoding.UTF8.GetString(bytes, 0, res.Count));
ArrayPool<byte>.Shared.Return(bytes);
return resp;
Receive();
if (MessageQueue.TryDequeue(out var message))
return message;
return null;
}


public void Receive()
{
byte[] bytes = ArrayPool<byte>.Shared.Rent(2048);
Expand All @@ -63,6 +61,7 @@ public void Receive()

public void Quit()
{
CoroutineWorkerNode.KillCoroutineTagInstance("UserWebSocket");
MessageQueue.Clear();
webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, null, CancellationToken.None);
webSocket.Dispose();
Expand Down
3 changes: 2 additions & 1 deletion csharp/Menus/LobbyScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void Connect()

ConnectResponse = JsonSerializer.Deserialize<ConnectResponse>(connectasync.Result.Content.ReadAsStringAsync().Result);
UserWebSocket.Instance.Connect(LobbyAddress, ConnectResponse.Id.ToString(), ConnectResponse.Ticket);
// start chat.
ChatWebSocket.Instance.Connect(LobbyAddress, ConnectResponse.Ticket);
}


Expand Down Expand Up @@ -73,6 +73,7 @@ public void CheckMap()

public void Quit()
{
ChatWebSocket.Instance.Quit();
UserWebSocket.Instance.Quit();
CoroutineWorkerNode.KillCoroutineTagInstance("CheckMap");
Client.Dispose();
Expand Down
3 changes: 2 additions & 1 deletion csharp/Menus/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public partial class MainMenu : Control
{
public override void _Ready()
{
GameManager.Instance.UIManager.LoadScreenStop();
this.GetWindow().Unresizable = true;
GameManager.Instance.UIManager.LoadScreenStop();
GetNode<Label>("VersionLabel").Text = BuildDefined.FullVersion;
}

Expand Down
Loading

0 comments on commit d57ada0

Please sign in to comment.