Exemplo n.º 1
0
 private void StopWebSocketServer()
 {
     if (wssv != null)
     {
         Logger.Verbose("Stopping WebSocket service on " + wssv.Address + ":" + wssv.Port + wssv.WebSocketServices.Paths.ElementAt(0));
         wssv.Stop();
         wssv = null;
     }
 }
 private void StopWebSocketClient()
 {
     if (wscl != null)
     {
         Logger.Verbose("Stopping WebSocket client on " + wscl.Url);
         wscl.Close();
         wscl = null;
     }
 }
        private void Wscl_OnMessage(object sender, MessageEventArgs e)
        {
            if (bot == null)
            {
                wscl.Send($"{{\"event\":\"controller-disconnected\"}}");
                return;
            }

            //Logger.Verbose("Action from \"" + _clientName + "\": " + e.Data);
            Logger.Verbose("Instruction/s received from Machina Server: " + e.Data);

            ExecuteInstructionsOnContext(e.Data);
        }
        protected override void OnMessage(MessageEventArgs e)
        {
            //base.OnMessage(e);
            //Console.WriteLine("  BRIDGE: received message: " + e.Data);
            if (_robot == null || _parent.bot == null)
            {
                _parent.wssv.WebSocketServices.Broadcast($"{{\"event\":\"controller-disconnected\"}}");
                return;
            }

            Logger.Verbose("Action from \"" + _clientName + "\": " + e.Data);

            _parent.ExecuteInstructionsOnContext(e.Data);
        }
        /// <summary>
        /// Initialize WebSocket communication infrastructure
        /// </summary>
        /// <returns></returns>
        private bool InitializeWebSocketServer()
        {
            // Close previous instances, if applicable
            if (wssv != null && wssv.IsListening)
            {
                StopWebSocketServer();
            }
            if (wscl != null && wscl.IsAlive)
            {
                StopWebSocketClient();
            }

            wsURL    = txtbox_WSServerURL.Text;
            _authkey = psswrdbox_Key.Password;

            // Check if given URL is a valid Machina Server
            try
            {
                Logger.Verbose("Trying connection to Machina Server on " + wsURL);

                // Add relevant auth info as query parameters
                string fullURL = $"{wsURL}?name={_robotName}&client=machina-bridge&authkey={_authkey}";

                // Establish connection
                wscl            = new WebSocket(fullURL);
                wscl.OnOpen    += Wscl_OnOpen;
                wscl.OnMessage += Wscl_OnMessage;
                wscl.OnClose   += Wscl_OnClose;
                wscl.OnError   += Wscl_OnError;
                wscl.Connect();
                //wscl.Send("hello from Bridge");

                if (wscl.IsAlive)
                {
                    Logger.Info("Successful connection to Machina Remote Server on " + wsURL);
                }
                else
                {
                    throw new Exception();
                }
            }
            catch
            {
                Logger.Verbose("Could not connect to existing Machina Server, initializing locally...");

                // Check validity of URL
                if (!ParseWebSocketURL())
                {
                    Logger.Error("Invalid WebSocket URL \"" + txtbox_WSServerURL.Text + "\"; try something like \"ws://127.0.0.1/Bridge\"");
                    return(false);
                }

                wssv = new WebSocketServer(wsURL);
                wssv.AddWebSocketService(wssvBehavior, () => new WSServerBehavior(bot, this));

                // @TODO: add a check here if the port is in use, and try a different port instead
                try
                {
                    wssv.Start();
                }
                catch
                {
                    Logger.Error("Default websocket server is not available, please enter a new one manually...");
                    return(false);
                }

                if (wssv.IsListening)
                {
                    //Machina.Logger.Info($"Listening on port {wssv.Port}, and providing WebSocket services:");
                    //foreach (var path in wssv.WebSocketServices.Paths) Machina.Logger.Info($"- {path}");
                    Logger.Verbose("Successful initialization of Machina Local Server on " + wsURL);
                    Logger.Info("Waiting for incoming connections on Machina Local Server " + (wsURL + wssvBehavior));
                }
            }

            return(true);
            //lbl_ServerURL.Content = wssvURL + wssvBehavior;
        }