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