GazeServer.cs 1.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebSocketSharp.Server;
using TrackerBridge;

namespace GazeWebSocketServer
{
    class GazeServer
    {
        private WebSocketServer wssv;
14
        public Boolean isRunning = false;
15 16 17 18 19 20 21 22 23
        public GazeServer(Int32 port, String endpoint)
        {
            wssv = new WebSocketServer(port);
            wssv.Log.Level = WebSocketSharp.LogLevel.Trace;
            wssv.AddWebSocketService(endpoint, () => new GazeCoordinateBehavior());
        }
        public void Start()
        {
            wssv.Start();
24
            isRunning = true;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
        }

        public void Publish(GazeData gazeData)
        {
            WebSocketServiceManager wssm = wssv.WebSocketServices;
            foreach(WebSocketServiceHost serviceHost in wssm.Hosts)
            {
                if (serviceHost.Type == typeof(GazeCoordinateBehavior))
                {
                    serviceHost.Sessions?.Broadcast(gazeData.ToString());
                }
            }
        }
    }
}