Пример #1
0
        async private void CreateConnection()
        {
            await Task.Run(() => {
                try
                {
                    Debug.Log("ElmoGameNetwork: Try To Create Connection...");

                    CloseConnection();
                    connection = factory.CreateConnection();
                }
                catch (Exception ex)
                {
                    Debug.Log("Couldent Connect to the Network: " + ex.ToString());

                    DoMainThread.ExecuteOnMainThread(() => OnCouldNotConnect?.Invoke());
                    DoMainThread.ExecuteOnMainThread(() =>
                                                     MyTime.IInvoke(() => {
                        CreateConnection();
                    }, 5));
                }
            });

            if (connection != null && connection.IsOpen)
            {
                OnConnect();
            }
        }
Пример #2
0
 private void Connection_ConnectionShutdown(object sender, ShutdownEventArgs e)
 {
     Debug.Log("ElmoGameNetwork: Disconnected: " + e.ToString());
     DoMainThread.ExecuteOnMainThread(() => {
         OnDesconnected?.Invoke();
     });
     // ForceToReconnect();
 }
Пример #3
0
 private void OnMessageRecived(string message)
 {
     if (debugMode)
     {
         Debug.Log("ElmoGameNetwork: OnMessageRecived: " + message);
     }
     DoMainThread.ExecuteOnMainThread(() => {
         OnMessageReceived?.Invoke(message);
     }
                                      );
 }
Пример #4
0
 private void Connection_RecoverySucceeded(object sender, EventArgs e)
 {
     try
     {
         Debug.Log("ElmoGameNetwork: Reconnect Successfully");
         DoMainThread.ExecuteOnMainThread(() => CreateConnection());
     }
     catch (Exception ex)
     {
         Debug.LogError(ex);
     }
 }
Пример #5
0
        private void OnConnect()
        {
            Debug.Log("ElmoGameNetwork: " + "Connected Successfully ");

            connection.CallbackException       += Connection_CallbackException;
            connection.ConnectionBlocked       += Connection_ConnectionBlocked;
            connection.ConnectionRecoveryError += Connection_ConnectionRecoveryError;
            connection.ConnectionShutdown      += Connection_ConnectionShutdown;
            connection.ConnectionUnblocked     += Connection_ConnectionUnblocked;
            connection.RecoverySucceeded       += Connection_RecoverySucceeded;
            DoMainThread.ExecuteOnExit(() => UnsubscribeFromEvents());
            DoMainThread.ExecuteOnMainThread(() => {
                OnConnected?.Invoke();
            });
            Subscribe();
        }
Пример #6
0
        private void Subscribe()
        {
            try
            {
                channel_Received = connection.CreateModel();
                channel_Send     = connection.CreateModel();

                routingKey = NetworkConfig.UUID;
                queueName  = NetworkConfig.QueuePrefix + routingKey;

                channel_Received.QueueDeclare(queueName, false, false, true, null);
                channel_Received.QueueBind(queueName, NetworkConfig.IN_Exchange, routingKey, null);
                props         = channel_Received.CreateBasicProperties();
                props.Headers = new Dictionary <string, object>();
                props.ReplyTo = routingKey;
                props.Headers.Add("user_id", NetworkConfig.UserID);
                props.Headers.Add("hmac", "null");
                var consumer = new EventingBasicConsumer(channel_Received);
                consumer.Received += (ch, ea) => {
                    var body = ea.Body;
                    channel_Received.BasicAck(ea.DeliveryTag, false);
                    string text = System.Text.Encoding.UTF8.GetString(body);
                    OnMessageRecived(text);
                };
                string consumerTag = channel_Received.BasicConsume(queueName, false, consumer);
                Debug.Log("ElmoGameNetwork: " + "Subscribe Successfully ");
                DoMainThread.ExecuteOnMainThread(() => {
                    OnSubscribe?.Invoke();
                });
                connection.AutoClose = true;
            }
            catch (System.Exception ex)
            {
                ForceToReconnect();

                Debug.LogError(ex.ToString());
            }
        }