Ejemplo n.º 1
0
        private static XmppClientConnection CreateConnection(ConnEvents prm)
        {
            var connection = new XmppClientConnection();

            connection.Username      = JRobotConfig.X.Username;
            connection.Password      = JRobotConfig.X.Password;
            connection.Server        = JRobotConfig.X.Server;
            connection.ConnectServer = JRobotConfig.X.ConnectionServer;
            connection.OnError      += prm.OnError;
            connection.OnMessage    += prm.OnMessage;
            connection.OnAuthError  += prm.OnAuthError;
            connection.OnLogin      += prm.OnLogin;
            connection.OnPresence   += prm.OnPresence;
            return(connection);
        }
Ejemplo n.º 2
0
        private static void MainCode()
        {
            var stop = false;

            var cc  = new Dictionary <string, List <Message> >();
            var prm = new ConnEvents
            {
                OnError = delegate(object sender, Exception exception)
                {
                    Log.Error("error in connection: {0}", exception.ToString());
                    LastError = exception.Message;
                    stop      = true;
                },
                OnAuthError = delegate(object sender, Element element)
                {
                    Log.Error("auth error: {0}", element.ToString());
                    LastError = "Auth error";
                    stop      = true;
                },
                OnLogin = delegate
                {
                    Log.Debug("OnLogin event received");
                    conn.Send(new Presence(ShowType.chat, "Online")
                    {
                        Type = PresenceType.available
                    });
                },
                OnPresence = delegate(object sender, Presence pres)
                {
                    Log.Debug("OnPresence event received, type: {0}", pres.Type.ToString());
                    if (pres.Type != PresenceType.subscribe)
                    {
                        return;
                    }
                    Log.Debug("accepting subscription to: {0}", pres.From);
                    (new PresenceManager(conn)).ApproveSubscriptionRequest(pres.From);
                    SendHello(pres.From);
                },
                OnMessage = delegate(object sender, Message msg)
                {
                    if (string.IsNullOrEmpty(msg.Body))
                    {
                        return;
                    }
                    Log.Debug("new message from: {0}", msg.From);
                    lock (cc)
                    {
                        var key = msg.From.ToString();
                        if (!cc.ContainsKey(key))
                        {
                            cc[key] = new List <Message>();
                            var thr = new Thread(() => ClientThread(key, cc));
                            thr.IsBackground = true;
                            thr.Start();
                        }
                        // чтобы в это время не отрабатывалось чтение очереди
                        lock (cc[key])
                        {
                            cc[key].Add(msg);
                        }
                    }
                }
            };

            conn = CreateConnection(prm);

            Log.Info("connecting to server...");
            conn.Open();

            var oldState = XmppConnectionState.Disconnected;
            var oldTime  = DateTime.Now;
            var waitStep = 0;

            while (!stop)
            {
                try
                {
                    if (DateTime.Now.Subtract(oldTime).TotalMilliseconds < 1000)
                    {
                        Thread.Sleep(100);
                    }
                    else
                    {
                        var state = conn.XmppConnectionState;
                        if (oldState != state)
                        {
                            Log.Info("Connection state: {0}", state);
                            oldState = state;
                        }
                        var needReconnect = state == XmppConnectionState.Disconnected;
                        if (!needReconnect && state != XmppConnectionState.SessionStarted)
                        {
                            waitStep++;
                            if (waitStep >= 10)
                            {
                                needReconnect = true;
                            }
                        }
                        if (needReconnect)
                        {
                            try
                            {
                                Log.Info("Try to reconnect to server");
                                conn.Close();
                                conn     = CreateConnection(prm);
                                waitStep = 0;
                                conn.Open();
                            }
                            catch (Exception ex)
                            {
                                Log.Error("exception during reconnect: {0}", ex.ToString());
                                Log.Error("stack trace: {0}", ex.StackTrace);
                            }
                        }
                        oldTime = DateTime.Now;
                    }
                }
                catch (ThreadAbortException)
                {
                    stop = true;
                    Log.Info("main thread aborted.");
                    break;
                }
                catch (Exception e)
                {
                    stop = true;
                    Log.Error("exception: {0}", e.ToString());
                    break;
                }
            }

            Log.Info("closing connection to server.");
            conn.Close();
        }