Esempio n. 1
0
 /// <summary>
 /// 实现 OnTunnelConnect 方法
 /// 在客户端成功连接 WebSocket 信道服务之后会调用该方法,此时通知所有其它在线的用户当前总人数以及刚加入的用户是谁
 /// </summary>
 /// <param name="tunnel">成功连接的 WebSocket 信道</param>
 public void OnTunnelConnect(Tunnel tunnel)
 {
     if (userMap.ContainsKey(tunnel.Id))
     {
         room.AddTunnel(tunnel);
         room.Broadcast("people", new { total = room.TunnelCount, enter = userMap[tunnel.Id] });
     }
     else
     {
         tunnel.Close();
     }
 }
        public void TestRoomBroadcastWithInvalidTunnels()
        {
            using (var mock = helper.CreateWebRequestProviderMock())
            {
                mock.SetupResponseBody(new
                {
                    code = 0,
                    data = new
                    {
                        invalidTunnelIds = new string[] { "tunnel1", "tunnel2" }
                    }.ToJson()
                }.ToJson());

                TunnelRoom room = new TunnelRoom();

                room.AddTunnel(Tunnel.GetById("tunnel1"));
                room.AddTunnel(Tunnel.GetById("tunnel2"));
                room.AddTunnel(Tunnel.GetById("tunnel3"));
                var result = room.Broadcast("test-type", "test-message");

                Assert.IsTrue(result.HasTunnelInvalidInfo);
                Assert.AreEqual(2, result.TunnelIvalidInfos.Count());
                Assert.AreEqual(2, result.TunnelIvalidInfos.Count(x => x.TunnelId == "tunnel1" || x.TunnelId == "tunnel2"));
            }
        }
        public void TestRoomBroadcast()
        {
            using (var mock = helper.CreateWebRequestProviderMock())
            {
                mock.SetupResponseBody(new { code = 0 }.ToJson());

                TunnelRoom room = new TunnelRoom();
                Assert.AreEqual(0, room.TunnelCount);

                room.AddTunnel(Tunnel.GetById("tunnel1"));
                room.AddTunnel(Tunnel.GetById("tunnel2"));
                Assert.AreEqual(2, room.TunnelCount);

                room.RemoveTunnelById("tunnel1");
                Assert.AreEqual(1, room.TunnelCount);

                room.AddTunnel(Tunnel.GetById("tunnel3"));
                room.Broadcast("test-type", "test-message");


                var packets = helper.ResolvePackets(mock.GetRequestContent());

                // emit 1 packet
                Assert.AreEqual(1, packets.Length);
                var firstPacket = packets[0];

                // the first packet should be a message packet
                Assert.AreEqual(2, firstPacket.tunnelIds.Length);
                Assert.AreEqual("tunnel2", firstPacket.tunnelIds[0]);
                Assert.AreEqual("tunnel3", firstPacket.tunnelIds[1]);
                Assert.AreEqual("message", firstPacket.type);

                // the message are encoded correctly
                var message = new { type = "", content = "" }.ParseFromJson(firstPacket.content);
                Assert.AreEqual("test-type", message.type);
                Assert.AreEqual("test-message", message.content);
            }
        }