C
C#7mo ago
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
10 Replies
Hath
HathOP7mo 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?
Insire
Insire7mo ago
can you go into detail what troubles/issue you are having?
Hath
HathOP7mo 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
Insire
Insire7mo ago
is this code being used in some sort of game egnine? im seeing a bunch of references i have never seen
Hath
HathOP7mo ago
Yea
Insire
Insire7mo ago
then i can only recommend to properly do async
Insire
Insire7mo ago
aka this
No description
Hath
HathOP7mo ago
await the sendmessage inside the command u mean? oh
Insire
Insire7mo ago
for the rest, somebody else has to help you
Hath
HathOP7mo ago
sure, ty
Want results from more Discord servers?
Add your server