Пример #1
0
        public void ReconnectTests()
        {
            var onOpenCount = 0;

            Socket.OnOpenDelegate onOpenCallback = () => onOpenCount++;

            var socketFactory = new DotNetWebSocketFactory();
            var socket        = new Socket(socketFactory, new Socket.Options
            {
                channelRejoinInterval = TimeSpan.FromMilliseconds(200),
                logger = new BasicLogger()
            });

            socket.OnOpen += onOpenCallback;
            var url = string.Format("ws://{0}/phoenix_sharp_test", host);

            socket.Connect(url);
            Assert.IsTrue(socket.state == Socket.State.Open);
            Assert.AreEqual(1, onOpenCount);

            var afterJoinCount = 0;
            var roomChannel    = socket.MakeChannel("tester:phoenix-sharp");

            roomChannel.On("after_join", m => afterJoinCount++);
            var param = new Dictionary <string, object> {
                { "auth", "doesn't matter" },
            };

            roomChannel.Join(param);
            Assert.That(() => afterJoinCount, Is.EqualTo(1).After(networkDelay, 10));

            socket.UnexpectedDisconnect();
            Assert.That(() => onOpenCount, Is.EqualTo(2).After(networkDelay + 1000, 100));
            Assert.That(() => afterJoinCount, Is.EqualTo(2).After(networkDelay, 10));
        }
Пример #2
0
        private (Presence, Channel) NewPresence(string id, OnJoinDelegate onJoin = null, OnLeaveDelegate onLeave = null, OnSyncDelegate onSync = null)
        {
            var socketFactory = new DotNetWebSocketFactory();
            var socket        = new Socket(socketFactory, new Socket.Options
            {
                channelRejoinInterval = TimeSpan.FromMilliseconds(200),
                logger = new BasicLogger()
            });
            var url = string.Format("ws://{0}/phoenix_sharp_test", host);

            socket.Connect(url);
            Assert.IsTrue(socket.state == Socket.State.Open);
            var channel  = socket.MakeChannel("presence");
            var presence = new Presence(channel);

            if (onJoin != null)
            {
                presence.onJoin = onJoin;
            }
            if (onLeave != null)
            {
                presence.onLeave = onLeave;
            }
            if (onSync != null)
            {
                presence.onSync = onSync;
            }

            Reply?joinOkReply    = null;
            Reply?joinErrorReply = null;

            channel.Join(new Dictionary <string, object> {
                { "id", id }
            })
            .Receive(Reply.Status.Ok, r => joinOkReply       = r)
            .Receive(Reply.Status.Error, r => joinErrorReply = r);
            Assert.That(() => joinOkReply.HasValue, Is.True.After(networkDelay, 10));
            return(presence, channel);
        }
Пример #3
0
        public void MultipleJoinIntegrationTest()
        {
            var onOpenCount = 0;

            Socket.OnOpenDelegate   onOpenCallback   = () => onOpenCount++;
            Socket.OnClosedDelegate onClosedCallback = (code, message) => onOpenCount--;

            List <String> onMessageData = new List <string>();

            Socket.OnMessageDelegate onMessageCallback = m => onMessageData.Add(m);

            var socketFactory = new DotNetWebSocketFactory();
            var socket        = new Socket(socketFactory, new Socket.Options
            {
                channelRejoinInterval = TimeSpan.FromMilliseconds(200),
                logger = new BasicLogger()
            });

            socket.OnOpen    += onOpenCallback;
            socket.OnClose   += onClosedCallback;
            socket.OnMessage += onMessageCallback;

            socket.Connect(string.Format("ws://{0}/socket", host), null);
            Assert.IsTrue(socket.state == Socket.State.Open);
            Assert.AreEqual(1, onOpenCount);

            Reply?  joinOkReply      = null;
            Reply?  joinErrorReply   = null;
            Message afterJoinMessage = null;
            Message closeMessage     = null;
            Message errorMessage     = null;

            //Try to join for the first time
            var param = new Dictionary <string, object> {
                { "auth", "doesn't matter" },
            };

            var roomChannel = socket.MakeChannel("tester:phoenix-sharp");

            roomChannel.On(Message.InBoundEvent.phx_close, m => closeMessage = m);
            roomChannel.On(Message.InBoundEvent.phx_error, m => errorMessage = m);
            roomChannel.On("after_join", m => afterJoinMessage = m);

            roomChannel.Join(param)
            .Receive(Reply.Status.Ok, r => joinOkReply       = r)
            .Receive(Reply.Status.Error, r => joinErrorReply = r);

            Assert.That(() => joinOkReply.HasValue, Is.True.After(networkDelay, 10));
            Assert.IsNull(joinErrorReply);

            Assert.That(() => afterJoinMessage != null, Is.True.After(networkDelay, 10));
            Assert.AreEqual("Welcome!", afterJoinMessage.payload["message"].Value <string>());

            Assert.AreEqual(Channel.State.Joined, roomChannel.state);

            socket.Disconnect(null, null);

            Assert.AreEqual(Socket.State.Closed, socket.state);

            socket.Connect(string.Format("ws://{0}/socket", host), null);
            Assert.IsTrue(socket.state == Socket.State.Open);
            Assert.AreEqual(1, onOpenCount);
        }