Hath
Hath
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
sure, ty
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
oh
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
await the sendmessage inside the command u mean?
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
Yea
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
I guess I'm failing to initialize the service? but the websocket inside the service does turn on
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
neither are any errors shown
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
when running the websocket command i did for testing, the websocket message is not sent
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
in this case is where Im injecting the service, any help to pinpoint the issue?
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
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;
}
}
}
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
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}");
}
});
}
}
}
17 replies
CC#
Created by Hath on 4/28/2024 in #help
Issue with websocket within a service
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);
}
}
}
17 replies