Пример #1
0
 public void KickPlayer(int playerId)
 {
     if (player.Id != _room.RoomManager.Id)
     {
         return;
     }
     // _room.RemovePlayerById(playerId);
     ClientWebsocketsManager.Send(WebscoketSendObjs.LeaveRoom(playerId));
 }
Пример #2
0
        public void RoomMessage(string message)
        {
            RoomMessage roomMessage = new RoomMessage(0)
            {
                Name    = player.WeixinName,
                Message = message
            };

            ClientWebsocketsManager.Send(roomMessage);
        }
Пример #3
0
        public virtual IActionResult WebSocketHandler(string askMethodName, Dictionary <string, string> methodParam)
        {
            if (_inngeGame != null && _inngeGame.IsStarted != true)
            {
                ClientWebsocketsManager.Send(new Alert(player.Id, "游戏还未启动!请房主先启动游戏"));
                return(null);
            }
            methodParam.Remove("askMethodName");
            methodParam.Add("playerId", player.Id.ToString());
            IGameProject gameProject = _room.InningGame.IGameProject;
            string       respondStr  = gameProject.ClinetHandler(askMethodName, methodParam);

            return(Content(respondStr));
        }
Пример #4
0
        /// <summary>
        /// 检查玩家session保存的房间Id等,初始化房间信息
        /// </summary>
        private void LoadRoomInfo()
        {
            string gameCityId_;
            string roomId_;

            #region session中保存有房间信息就读取,没有就从Request.Query中读取
            if (session.Keys.Contains("RoomId") && session.Keys.Contains("CityId"))
            {
                gameCityId_ = session.GetString("CityId");
                roomId_     = session.GetString("RoomId");
            }
            else
            {
                gameCityId_ = httpContextAccessor.HttpContext.Request.Query["gameCityId"];
                roomId_     = httpContextAccessor.HttpContext.Request.Query["roomId"];
            }
            #endregion
            #region sesson或Request.Query中有房间信息就使用
            if (gameCityId_ != null && gameCityId_.Length > 0)
            {
                _gameCity = CityGameController.GameCityList.FindGameCityById(gameCityId_);
            }
            if (roomId_ != null && roomId_.Length > 0)
            {
                _room = _gameCity.FindRoomById(roomId_);
                if (_room == null)
                {
                    throw new RoomIsNotExistException(player.Id, "房间已经不存在了");
                }
                _inngeGame   = _room.InningGame;
                _gameProject = _inngeGame.IGameProject;
                #endregion
                #region 保存玩家websocket对象
                IPlayerJoinRoom roomPlayer = _room.Players.FirstOrDefault(p => p.Id == player.Id);
                if (null != roomPlayer)
                {
                    roomPlayer.WebSocketLink = ClientWebsocketsManager.FindClientWebSocketByPlayerId(player.Id);
                }
                #endregion
            }
        }
Пример #5
0
        public void RoomMessage(string message)
        {
            if (message.Length > 30)
            {
                message = message.Substring(0, 30);
            }
            message = ToolsStrFiler.ForWebClient(message);
            RoomMessage roomMessage = new RoomMessage(0)
            {
                Name    = player.WeixinName,
                Message = message
            };

            if (null != _room)
            {
                foreach (IPlayerJoinRoom item in _room.Players)
                {
                    if (null != item.WebSocketLink)
                    {
                        ClientWebsocketsManager.SendToWebsocket(roomMessage, item.WebSocketLink);
                    }
                }
            }
        }
Пример #6
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        //用于指定 ASP.NET 应用程序将如何响应每一个 HTTP 请求
        // Configure 方法必须接受一个 IApplicationBuilder 参数。一些额外服务,比如 IHostingEnvironment 或 ILoggerFactory 也可以被指定,如果在它们可用情况下,这些服务将会被服务器 注入 进来
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            app.UseApplicationInsightsRequestTelemetry();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            //app.UseExceptionHandler(WxPayConfig.SiteName);//出现错误重定向到主页
            app.UseApplicationInsightsExceptionTelemetry();
            app.UseStaticFiles();
            app.UseSession();
            app.UseCookieAuthentication(new CookieAuthenticationOptions()
            {
                AuthenticationScheme  = "MyCookieMiddlewareInstance",
                LoginPath             = new PathString("/Game/Index/"),
                AccessDeniedPath      = new PathString("/Game/Index/"),
                AutomaticAuthenticate = true,
                AutomaticChallenge    = true
            });
            app.Map("/ws", appBuilder =>
            {
                var webSocketOptions = new WebSocketOptions()
                {
                    KeepAliveInterval = TimeSpan.FromSeconds(60),
                    ReceiveBufferSize = 4 * 1024,
                };
                appBuilder.UseWebSockets(webSocketOptions);
                appBuilder.Run(context =>
                {
                    WebSocket websocket = null;
                    if (context.WebSockets.IsWebSocketRequest)
                    {
                        websocket    = context.WebSockets.AcceptWebSocketAsync().Result;
                        var playerId = context.Session.GetInt32("playerId");
                        ClientWebsocketsManager.Add(playerId, websocket);
                    }

                    while (websocket != null && websocket.State == WebSocketState.Open)
                    {
                        Thread.Sleep(60000);
                    }
                    ;
                    return(null);
                });
            });
            app.UseMvc(routes =>
            {
                routes.MapRoute(name: "areaRoute",
                                template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Game}/{action=Index}/{id?}");
            });
        }