Example #1
0
        public CustomApplicationContext()
        {
            trayIcon = new NotifyIcon()
            {
                Text        = "VRKnockServer",
                Icon        = Properties.Resources.AppIcon,
                ContextMenu = new ContextMenu(new MenuItem[]
                {
                    new MenuItem("Info", Info),
                    new MenuItem("Exit", Exit)
                }),
                Visible = true
            };
            trayIcon.Click += Info;


            if (string.IsNullOrEmpty(Properties.Settings.Default.ServerId))
            {
                Properties.Settings.Default.ServerId = Guid.NewGuid().ToString();
                Properties.Settings.Default.Save();
            }

            Console.WriteLine("ServerId: " + Properties.Settings.Default.ServerId);

            Console.WriteLine("Adding Firewall Rule...");
            Program.AddFirewallRule();


            Console.WriteLine("ConnectionMethod: " + Properties.Settings.Default.ConnectionMethod);
            if (Properties.Settings.Default.ConnectionMethod == "DIRECT")
            {
                try
                {
                    Console.WriteLine("Starting WebSocket Server...");

                    var port = 16945;
                    socketServer = new WebSocketServer(port);
                    socketServer.AddWebSocketService <SocketServer>("/", () =>
                    {
                        return(innerSocketServer = new SocketServer());
                    });
                    socketServer.Start();

                    Console.WriteLine("Web Service Running!");
                    Console.WriteLine(socketServer.Address + ":" + port);

                    // Console.WriteLine(localIp);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    trayIcon.ShowBalloonTip(2000, "VRKnock", "Failed to start Server!", ToolTipIcon.Error);
                    return;
                }
            }
            else if (Properties.Settings.Default.ConnectionMethod == "BRIDGE")
            {
                try
                {
                    Console.WriteLine("Starting WebSocket Client...");


                    socketClient = new WebSocket("wss://bridge.vrknock.app")
                    {
                        Origin = "ws://" + Properties.Settings.Default.ServerId + ".servers.vrknock.app:16945"
                    };

                    _messageHandler = new SocketMessageHandler()
                    {
                        Send = (s, t) =>
                        {
                            socketClient.Send("{\"_type\":\"forward\",\"source\":\"" + Properties.Settings.Default.ServerId + "\",\"target\":\"" + t + "\",\"payload\":" + s + '}');
                            return(null);
                        }
                    };

                    socketClient.OnOpen += (sender, args) =>
                    {
                        Console.WriteLine("Connected to bridge!");

                        socketClient.Send("{\"_type\":\"register\",\"payload\":{\"type\":\"server\",\"serverId\":\"" + Properties.Settings.Default.ServerId + "\"}}");
                    };
                    socketClient.OnMessage += (sender, args) =>
                    {
                        Console.WriteLine(args.Data);
                        _messageHandler.OnMessage(args);
                    };

                    socketClient.ConnectAsync();



                    // Console.WriteLine(localIp);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                    trayIcon.ShowBalloonTip(2000, "VRKnock", "Failed to connect to bridge!", ToolTipIcon.Error);
                    return;
                }
            }

            try
            {
                Console.WriteLine("Initializing VR...");
                NotificationManager.GetInstance().Init();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                trayIcon.ShowBalloonTip(2000, "VRKnock", "Failed to init VR! Please make sure SteamVR is running!",
                                        ToolTipIcon.Error);
                return;
            }

            Program.RegisterAutoLaunchApp();


            trayIcon.ShowBalloonTip(2000, "VRKnock", "Server Running!", ToolTipIcon.Info);
        }