C
C#Hath

Issue with websocket within a service

Im having troubles using a websocket service within other parts of my projects, cannot use the Send method properly
H
Hath17d ago
namespace WebsocketServer.Services
{
[Service]
public interface IWebsocketServer
{
void SendMessage(string message);
}
[PluginServiceImplementation(Lifetime = ServiceLifetime.Singleton, Priority = Priority.Lowest)]
public class WebsocketServerService : IWebsocketServer, IDisposable
{
private readonly WebSocketServer m_WebSocketServer;
private readonly WebSocketService m_WebSocketService;

public WebsocketServerService()
{
m_WebSocketServer = new WebSocketServer(27015);
m_WebSocketService = new WebSocketService();
m_WebSocketServer.AddWebSocketService
<WebSocketService>("/playerinfo");
m_WebSocketServer.Start();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
m_WebSocketServer?.Stop();
}
}

public void SendMessage(string message)
{
Console.WriteLine("asd");
m_WebSocketService.Send(message);
}
}
}
namespace WebsocketServer.Services
{
[Service]
public interface IWebsocketServer
{
void SendMessage(string message);
}
[PluginServiceImplementation(Lifetime = ServiceLifetime.Singleton, Priority = Priority.Lowest)]
public class WebsocketServerService : IWebsocketServer, IDisposable
{
private readonly WebSocketServer m_WebSocketServer;
private readonly WebSocketService m_WebSocketService;

public WebsocketServerService()
{
m_WebSocketServer = new WebSocketServer(27015);
m_WebSocketService = new WebSocketService();
m_WebSocketServer.AddWebSocketService
<WebSocketService>("/playerinfo");
m_WebSocketServer.Start();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
m_WebSocketServer?.Stop();
}
}

public void SendMessage(string message)
{
Console.WriteLine("asd");
m_WebSocketService.Send(message);
}
}
}
namespace WebsocketServer.Services
{
public class WebSocketService : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
var receivedMessage = e.Data;
Console.WriteLine($"Received message: {receivedMessage}");
Send(receivedMessage);
}

protected override void OnClose(CloseEventArgs e)
{
Console.WriteLine("WebSocket connection closed");
}

public void Send(string message)
{
SendAsync(message, (completed) =>
{
if (completed)
{
Console.WriteLine($"Sent message: {message}");
}
else
{
Console.WriteLine($"Failed to send message: {message}");
}
});
}
}
}
namespace WebsocketServer.Services
{
public class WebSocketService : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
var receivedMessage = e.Data;
Console.WriteLine($"Received message: {receivedMessage}");
Send(receivedMessage);
}

protected override void OnClose(CloseEventArgs e)
{
Console.WriteLine("WebSocket connection closed");
}

public void Send(string message)
{
SendAsync(message, (completed) =>
{
if (completed)
{
Console.WriteLine($"Sent message: {message}");
}
else
{
Console.WriteLine($"Failed to send message: {message}");
}
});
}
}
}
namespace WebsocketServer.Commands
{
[Command("websocket")]
public class CommandWebsocket : OpenMod.Core.Commands.Command
{
private readonly IWebsocketServer m_WebsocketServer;
public CommandWebsocket(IServiceProvider serviceProvider, IWebsocketServer WebsocketServer) : base(serviceProvider)
{
m_WebsocketServer = WebsocketServer;
}

protected override async Task OnExecuteAsync()
{
Console.WriteLine("a");
m_WebsocketServer.SendMessage("test");
await UniTask.CompletedTask;
}
}
}
namespace WebsocketServer.Commands
{
[Command("websocket")]
public class CommandWebsocket : OpenMod.Core.Commands.Command
{
private readonly IWebsocketServer m_WebsocketServer;
public CommandWebsocket(IServiceProvider serviceProvider, IWebsocketServer WebsocketServer) : base(serviceProvider)
{
m_WebsocketServer = WebsocketServer;
}

protected override async Task OnExecuteAsync()
{
Console.WriteLine("a");
m_WebsocketServer.SendMessage("test");
await UniTask.CompletedTask;
}
}
}
in this case is where Im injecting the service, any help to pinpoint the issue?
I
Insire17d ago
can you go into detail what troubles/issue you are having?
H
Hath17d ago
when running the websocket command i did for testing, the websocket message is not sent neither are any errors shown I guess I'm failing to initialize the service? but the websocket inside the service does turn on
I
Insire17d ago
is this code being used in some sort of game egnine? im seeing a bunch of references i have never seen
H
Hath17d ago
Yea
I
Insire17d ago
then i can only recommend to properly do async
I
Insire17d ago
aka this
No description
H
Hath17d ago
await the sendmessage inside the command u mean? oh
I
Insire17d ago
for the rest, somebody else has to help you
H
Hath17d ago
sure, ty
Want results from more Discord servers?
Add your server
More Posts
HelpI'm new to C# in VSC and i have a Problem wich is why i can't execute the Code. What am i supposed t✅ Checking data from SQL databasehelp on verge of tears✅ How to export data from datagridview into excel fileI've tried like a few ways and cant really make sense of it, if someone can guide me through it it wProblem with Material Design Lib in wpfI have a Combobox item in Stack panel. Im using Material Desing for automatic styling but the Popup✅ Title: Issues with Authentication and Authorization using JWT with AutoRender Mode in Blazor NET8Hello friends, I am currently facing challenges with Authentication and Authorization using JWT wit✅ How do I reference a specific private instance of a classI would like to know how I could reference a specific instance of a class, if the class is private aReceiving Game Controller Input while Minimized (WinForms C#)Pretty general question here. How would I go about receiving input from my user while they aren't tprocess startIve been trying to figure out why the proccess.start() hasnt been working✅ Configurations say that appsettings is returning nullBasically, i have a configuration and i'm trying to access a value from my appsettings, and all that✅ ASP.NET Core Web Api + SignalRHi, I have an API deployed on IIS but when a person wants to access it they get an error: "ERR_CONNE✅ .NET sdk not workingSo, I've installed the .NET sdk and did what I was told, but it's still not being detected by vscodeMEF No exports were found that match the constraintHi, i'm trying to implement MEF in my WPF app, however i'm getting this error, not sure why anymore.✅ WPF - set UIElement (image) in XAML depending on databound Property Enum valueI am trying to dynamically change a StatusBar image via a view's XAML depending upon a databound VieASP.NET Controller API + Blazor Client UI resources and helpHello! I'm doing a side project where I created the backend API with simple CRUD functionality and A✅ Run PuppeteerSharp in Docker ContainerI have an endpoint that creates PDFs using PuppeteerSharp. I work fine locally because it can automaDocker + Blazor + MSSQL handshake errorWhen I run dotnet ef database update I get the following error : ``Microsoft.Data.SqlClient.SqlExceProblem with corner radius XAML propertyI use a border with these properties: ```<Border CornerRadius="3" Background="{StaticResource Edito✅ :white_check_mark: Dictionary<int, ...> lookup time seems a bit too slowI was profiling a program, and I saw this: ```cs 100 % HasObject • 136 ms • 189 217 Calls • Engine.Rhow to read .txt file next to the Program.csi have the following code that is begin run ```cs string fileContent = File.ReadAllText("hamlet.txt✅ Linker Error Trying to Build OpenSSL Static LinkingHello, I've been trying to compile and build OpenSSL to try and encrypt a text file using AES 256 bu