Beispiel #1
0
        // When we receive a message, we first need to check if it is a "publish" message.
        // Once the message has been verified, we can parse it into a JObject and send it
        // to our GetRequestType-function to determine the type of request.
        void Socket_OnMessage(string message)
        {
            Console.WriteLine("WebsocketCoinigy::Socket_OnMessage()  {0}", message);

            string PUBLISH_REGEX   = @"^{""event""*.:*.""#publish""";
            string AUTHTOKEN_REGEX = @"^{""event""*.:*.""#setAuthToken""";

            // Determine if message is a publish message using regex
            if (Regex.Match(message, PUBLISH_REGEX).Success)
            {
                // If so, parse the string
                var jObj = JObject.Parse(message);

                // Retrieve the channel's name
                string channelName = jObj["data"]["channel"].ToString();

                // Determine request type
                string requestType = GetRequestType(jObj);
                if (string.IsNullOrEmpty(requestType))
                {
                    return;
                }

                InvokeMessageReceived(channelName, requestType, message);
            }
            else if (Regex.Match(message, AUTHTOKEN_REGEX).Success)
            {
                // If so, parse the string
                var    jObj  = JObject.Parse(message);
                string token = jObj["data"]["token"].ToString();
                Console.WriteLine("token: {0}", token);
                socket.SetAuthToken(token);
                OnClientReady?.Invoke();
            }
        }
Beispiel #2
0
    public void ApplyClientTechnique(Technique newTechnique)
    {
        technique = newTechnique;
        foreach (var x in new TechniqueClientController[] { PINPinTechnique, PatternPinTechnique, Pattern3DPinTechnique })
        {
            x.gameObject.SetActive(false);
        }
        TechniqueClientController controller;

        switch (technique)
        {
        case Technique.PIN: controller = PINPinTechnique; break;

        case Technique.Pattern: controller = PatternPinTechnique;  break;

        case Technique.Pattern3D: controller = Pattern3DPinTechnique; break;

        default: throw new System.InvalidOperationException();
        }
        controller.gameObject.SetActive(true);
        techniqueClientPointer.Controller = controller;
        OnClientReady?.Invoke();
#if UNITY_ANDROID
        Handheld.Vibrate(); // to add permissions to manifest
#endif
    }
 public override void SyncronizationStatusChanged(TwilioIPMessagingClient client, ClientSynchronizationStatus status)
 {
     if (status == ClientSynchronizationStatus.Completed)
     {
         OnClientReady?.Invoke(this, new EventArgs());
     }
 }
Beispiel #4
0
        private void On_Opened()
        {
            Console.WriteLine("---OPENED");

            //socket.Emit("auth", this.credentials, ack: (string name, object error, object data) =>
            //Console.WriteLine(this.credentials.ToString());
            OnClientReady?.Invoke();
            return;

            socket.Emit("auth", this.credentials, ack: (string name, object err, object data) =>
            {
                if (!((bool)err) && (data != null))
                {
                    this.socket.SetAuthToken((string)data);
                    this.socket.Connect();
                    OnClientReady?.Invoke();
                    this.socket.Subscribe("ORDER-PLNX--BTC--ETH");

                    /*var scChannel = this.socket.subscribe("ORDER-PLNX--BTC--ETH");
                     * scChannel.watch(function(data) {
                     *  console.log(data);
                     * });*/
                }
                else
                {
                    Console.WriteLine(err);
                }
            });

            /*{
             *  OnClientReady?.Invoke();
             * });*/
        }
Beispiel #5
0
        private void On_Opened()
        {
            socket.Emit("auth", credentials, ack: (string name, object error, object data) =>
            {
                isAuthorized = true;

                OnClientReady?.Invoke();
            });
        }
Beispiel #6
0
        private void Read()
        {
            try
            {
                while (_isBroadcasting)
                {
                    lock (_lock)
                    {
                        foreach ((string _, ClientSocket socket) in _clientSockets.Where(s => s.Value.IsConnected()))
                        {
                            if (!socket.PendingData())
                            {
                                continue;
                            }

                            DataMessage message = socket.Read();

                            if (message.Route == "authentication")
                            {
                                if (!OnClientAuthentication?.Invoke(message) ?? false)
                                {
                                    socket.Disconnect();
                                }
                                else
                                {
                                    OnClientPrepare?.Invoke(socket);
                                    OnClientReady?.Invoke(socket);
                                    socket.Emit("ready");
                                }
                            }
                            else
                            {
                                //Check socket specific handlers
                                socket.ProcessHandler(message);

                                //check global handlers
                                if (!_messageHandlers.ContainsKey(message.Route))
                                {
                                    continue;
                                }
                                foreach (Action <DataMessage> handler in _messageHandlers[message.Route])
                                {
                                    handler(message);
                                }
                            }
                        }
                    }

                    //check reading 200x a second
                    Thread.Sleep(1000 / 200);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Server Read Exception: {0}", ex);
            }
        }
        public async void StartClient()
        {
            CredentialsAuth SpotifyAuth  = new CredentialsAuth(SpotifyClientID, SpotifyClientToken);
            Token           SpotifyToken = await SpotifyAuth.GetToken();

            Spotify.TokenType   = SpotifyToken.TokenType;
            Spotify.AccessToken = SpotifyToken.AccessToken;
            this.SpotifyToken   = SpotifyToken;
            OnClientReady?.Invoke();
        }
Beispiel #8
0
 private void _scClient_OnOpened() => OnClientReady?.Invoke();
Beispiel #9
0
        // When we receive a message, we first need to check if it is a "publish" message.
        // Once the message has been verified, we can parse it into a JObject and send it
        // to our GetRequestType-function to determine the type of request.
        private void On_Message(string message)
        {
            Console.WriteLine("---MESSAGE: '{0}'", message);

            if (message == "#1")
            {
                socket.Emit("auth", this.credentials, ack: (string name, object err, object data) =>
                {
                    if (!((bool)err) && (data != null))
                    {
                        this.socket.SetAuthToken((string)data);
                        this.socket.Connect();
                        OnClientReady?.Invoke();
                        this.socket.Subscribe("ORDER-PLNX--BTC--ETH");

                        /*var scChannel = this.socket.subscribe("ORDER-PLNX--BTC--ETH");
                         * scChannel.watch(function(data) {
                         *  console.log(data);
                         * });*/
                    }
                    else
                    {
                        Console.WriteLine(err);
                    }
                });
                return;

                this.socket.Subscribe("ORDER-PLNX--BTC--ETH");
            }

            string PUBLISH_REGEX = @"^{""event""*.:*.""#publish""";
            string SETAUTH_REGEX = @"^{""event""*.:*.""#setAuthToken""";

            if (Regex.Match(message, SETAUTH_REGEX).Success)
            {
                var    jo    = JObject.Parse(message);
                string token = jo["data"]["token"].ToString();
                Console.WriteLine("Set auth token: {0}", token);
                this.socket.SetAuthToken(token);

                this.socket.Subscribe("TRADE-PLNX--BTC--ETH");
                return;
            }

            // Determine if message is a publish message using regex
            var m = Regex.Match(message, PUBLISH_REGEX);

            if (!m.Success)
            {
                return;
            }

            // If so, parse the string
            var jObj = JObject.Parse(message);

            // Retrieve the channel's name
            string channelName = jObj["data"]["channel"].ToString();

            // Determine request type
            string requestType = GetRequestType(jObj);

            if (string.IsNullOrEmpty(requestType))
            {
                return;
            }

            InvokeMessageReceived(channelName, requestType, message);
        }
Beispiel #10
0
        /// <summary>
        ///     Starts client connection.
        /// </summary>
        public static void StartClient([NotNull] string ipAddress, ushort port, string password)
        {
            if (IsClientActive)
            {
                throw new InvalidOperationException(
                          "QNet is unable to start client while there is already active instance of client.");
            }
            if (ipAddress == null)
            {
                throw new ArgumentNullException(nameof(ipAddress));
            }

            var configuration = new QNetConfiguration
            {
                IpAddress      = ipAddress,
                Port           = port,
                MaxConnections = 2
            };

            if (OnClientPrepare == null)
            {
                throw new NullReferenceException("QNet is unable to start client. OnClientPrepare event is not set.");
            }

            Client = new QNetClient();
            Client = InternalStartPeer(Client, configuration, () =>
            {
                QNetHandlerStack.RegisterClientHandlers(Client);
                OnClientRegisterHeaders?.Invoke();
            });
            OnClientPrepare.Invoke(out var nickname, out var token);

            Client.OnMessagePoll += reader =>
            {
                // as the server always send the server frame
                // we need to read that right here
                QNetSimulation.ReceivedServerFrame = reader.ReadUInt32();
                QNetSimulation.AdjustServerFrames  = QNetSimulation.ReceivedServerFrame > QNetTime.ServerFrame;
            };

            Client.OnConnectionReady += (reader, writer) =>
            {
                var tickRate    = reader.ReadInt32();
                var frameNumber = reader.ReadUInt32();

                // set TickRate
                QNetTime.TickRate = tickRate;

                // initialize server frame count
                QNetSimulation.ReceivedServerFrame  = frameNumber;
                QNetSimulation.EstimatedServerFrame = frameNumber;

                // write player data
                writer.WriteString(nickname);
                writer.WriteUInt32(token);
                writer.WriteString(JEMBuild.BuildVersion);
                OnClientReady?.Invoke(reader, writer);
            };

            Client.OnDisconnection += (lostConnection, reason) =>
            {
                // stop peer
                if (InternalRunningPeers.Contains(Client))
                {
                    InternalStopPeer(Client, reason);
                }

                // update active state
                IsClientActive = false;

                // and de-initialize game
                if (!IsHostActive && QNetGameInitializer.GameInitialized && !QNetGameInitializer.GameIsDeInitializing
                    ) // ignore if host, server will de-initialize it anyway
                {
                    QNetGameInitializer.DeInitialize(() =>
                    {
                        OnClientDisconnected?.Invoke(IsHostActive, lostConnection, reason);
                    });
                }
                else
                {
                    if (!QNetGameInitializer.GameInitialized && !QNetGameInitializer.GameIsInitializing)
                    {
                        OnClientDisconnected?.Invoke(IsHostActive, lostConnection, reason);
                    }
                }
            };

            IsClientActive = true;
        }