Xenira / LiveSplit-Websocket

This provides a websocket to connect to LiveSplit
MIT License
6 stars 5 forks source link

Automatically receive a message when a split is made #14

Open mathieuarthur opened 4 months ago

mathieuarthur commented 4 months ago

Is your feature request related to a problem? Please describe. It would be nice to be able to receive a message when a split is done directly from autosplitter so we can execute a function

Describe the solution you'd like Just add a message sent to the client at every split

Describe alternatives you've considered I don't see another one

Additional context none

mathieuarthur commented 4 months ago

I can't make a dev env now but I looked at your code maybe we should just add this ?

using LiveSplit.Model;
using LiveSplit.Model.Input;
using LiveSplit.Options;
using LiveSplit.TimeFormatters;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using WebSocketSharp.Server;

namespace LiveSplit.UI.Components
{
    public class ServerComponent : IComponent
    {
        public Settings Settings { get; set; }
        public WebSocketServer Server { get; set; }

        protected LiveSplitState State { get; set; }
        protected Form Form { get; set; }
        protected TimerModel Model { get; set; }
        protected ITimeFormatter DeltaFormatter { get; set; }
        protected ITimeFormatter SplitTimeFormatter { get; set; }

        protected bool AlwaysPauseGameTime { get; set; }

        public float PaddingTop => 0;
        public float PaddingBottom => 0;
        public float PaddingLeft => 0;
        public float PaddingRight => 0;

        public string ComponentName => $"LiveSplit Websocket ({ Settings.Port })";

        public IDictionary<string, Action> ContextMenuControls { get; protected set; }

        public ServerComponent(LiveSplitState state)
        {
            Settings = new Settings();
            Model = new TimerModel();

            DeltaFormatter = new PreciseDeltaFormatter(TimeAccuracy.Hundredths);
            SplitTimeFormatter = new RegularTimeFormatter(TimeAccuracy.Hundredths);

            ContextMenuControls = new Dictionary<string, Action>();
            ContextMenuControls.Add("Start Server (WS)", Start);

            State = state;
            Form = state.Form;

            Model.CurrentState = State;
            State.OnStart += State_OnStart;

            // Register the OnSplit event handler
            State.OnSplit += State_OnSplit;
        }

        private void State_OnSplit(object sender, EventArgs e)
        {
            // Send a message to the WebSocket when a split is made
            foreach (var connection in Server.WebSocketServices["/livesplit"].Sessions.Sessions)
            {
                connection.Context.WebSocket.Send("split");
            }
        }

        public void Dispose()
        {
            State.OnStart -= State_OnStart;
            State.OnSplit -= State_OnSplit;
            if (Server != null)
            {
                Server.Stop();
            }
        }

}
Xenira commented 4 months ago

Will have a look at this once I am back from work. Looks like this is going in the right direction though

mathieuarthur commented 4 months ago

With this we can know when a split is done and what action achieve ! For example send another request from client to the component to get the delta time of the split we just did !

mathieuarthur commented 3 months ago

Please explain how to make a dev environment. Like I tried to import your project with livesplit core etc but I don't have any idea of which websocket sharp version you used etc