Ejemplo n.º 1
0
        public void TestToxMessage()
        {
            string messageFormat        = "Hey! This is test message number ";
            int    messageCount         = 100;
            int    receivedMessageCount = 0;

            EventHandler <ToxEventArgs.FriendMessageEventArgs> callback = (sender, args) =>
            {
                if (args.MessageType != ToxMessageType.Message || args.Message != messageFormat + receivedMessageCount)
                {
                    Assert.Fail("Message arrived got garbled");
                }

                receivedMessageCount++;
            };

            _tox2.OnFriendMessageReceived += callback;

            for (int i = 0; i < messageCount; i++)
            {
                var sendError = ToxErrorSendMessage.Ok;
                _tox1.SendMessage(0, messageFormat + i.ToString(), ToxMessageType.Message, out sendError);
                if (sendError != ToxErrorSendMessage.Ok)
                {
                    Assert.Fail("Failed to send message to friend: {0}", sendError);
                }
            }

            while (receivedMessageCount != messageCount)
            {
                DoIterate();
            }
            _tox2.OnFriendMessageReceived -= callback;
            Console.WriteLine("Received all messages without errors");
        }
Ejemplo n.º 2
0
        public bool sendMsg(ToxId toxid, string msg)
        {
            // check if this message is send to itself
            if (toxid.ToString() == tox.Id.ToString())
            {
                return(false); // this is not allowed
            }
            // wait toxcore online
            int maxOnlineWaitTime = 20000; // 20s
            int onlineWaitCount   = 0;

            while (!tox.IsConnected)
            {
                Thread.Sleep(10);
                onlineWaitCount += 10;
                if (onlineWaitCount > maxOnlineWaitTime)
                {
                    return(false);
                }
            }

            ToxKey toxkey    = toxid.PublicKey;
            int    friendNum = tox.GetFriendByPublicKey(toxkey);

            if (friendNum == -1)
            {
                int res = tox.AddFriend(toxid, "add friend");
                if (res != (int)ToxErrorFriendAdd.Ok)
                {
                    return(false);
                }
                friendNum = tox.GetFriendByPublicKey(toxkey);
            }
            int waitCount = 0;
            int maxCount  = 500;

            if (connectedList.IndexOf(toxkey.GetString()) == -1)
            {
                maxCount = 200 * 1000; // first time wait for 200s
            }
            while (tox.GetFriendConnectionStatus(friendNum) == ToxConnectionStatus.None && waitCount < maxCount)
            {
                if (waitCount % 1000 == 0)
                {
                    Console.WriteLine("target is offline." + waitCount / 1000);
                }
                waitCount += 10;
                Thread.Sleep(10);
            }
            if (waitCount == maxCount)
            {
                Console.WriteLine("Connect Failed");
                connectedList.Remove(toxkey.GetString());
                return(false);
            }
            connectedList.Add(toxkey.GetString());
            int msgRes = tox.SendMessage(friendNum, msg, ToxMessageType.Message);

            return(msgRes > 0);
        }
Ejemplo n.º 3
0
        public FriendConversationViewModel(Tox tox, uint friendNumber)
        {
            var messages = new SourceList <MessageViewModel>();

            messages.Connect()
            .AutoRefresh(x => x.Read)
            .Filter(x => !x.Read)
            .AsObservableList()
            .CountChanged
            .ToPropertyEx(this, x => x.UnreadCount);

            this.SendMessage = ReactiveCommand.Create <string>(msg =>
            {
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    _ = tox.SendMessage(friendNumber, msg, ToxMessageType.Message, out _);
                    messages.Add(new MessageViewModel("Me", msg, DateTime.Now.Ticks));
                }
            });
            ReadOnlyObservableCollection <MessageViewModel> msgs;

            messages.Connect()
            .Bind(out msgs)
            .Subscribe();

            this.Messages = msgs;

            this.SendTypedMessage = ReactiveCommand.Create(() =>
            {
                var msg           = this.InputMessage;
                this.InputMessage = string.Empty;
                return(msg);
            }, this.WhenAnyValue(x => x.InputMessage, (string s) => !string.IsNullOrWhiteSpace(s)));

            this.WhenAnyValue(x => x.InputMessage)
            .Select(msg => !string.IsNullOrWhiteSpace(msg))
            .Where(x => x)
            .Do(x => tox.SetTypingStatus(friendNumber, true, out _))
            .Throttle(TimeSpan.FromMilliseconds(500))
            .Subscribe(x => tox.SetTypingStatus(friendNumber, false, out _));

            tox.Events().Friends
            .Message
            .Where(x => x.FriendNumber == friendNumber && x.MessageType == ToxMessageType.Message)
            .Select(x => x.Message)
            .Subscribe(msg => messages.Add(new MessageViewModel(tox.GetFriendName(friendNumber, out _), msg, DateTime.Now.Ticks)));

            this.WhenAnyObservable(x => x.SendTypedMessage)
            .InvokeCommand(this, x => x.SendMessage);
        }