public static void RemoveSocket(PlayerWebsocket socket) { lock (socketsLock) { if (sockets.ContainsKey(socket.playerID)) { sockets[socket.playerID].Close(); sockets.Remove(socket.playerID); } } EventEngine.AddEvent(new PlayerOnline(socket.playerID, false)); Player player = Browsergame.Game.Engine.StateEngine.GetState().GetPlayer(socket.playerID); string msg = string.Format("Socket closed. Player {0}. Token: {1}. Thread: {2}", player.Name, player.token.Substring(0, 5), Thread.CurrentThread.ManagedThreadId); Logger.log(11, Category.WebSocket, Severity.Debug, msg); }
public HttpResponseMessage Post(PostData logindata) { if (logindata == null) { return(new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest)); } string token = Security.getToken(logindata.name, logindata.pw); State state = StateEngine.GetState(); if (state.GetPlayer(token) == null) { Event e = new NewPlayer(0, logindata.name, token); EventEngine.AddEvent(e); e.processed.WaitOne(); } HttpResponseMessage response = Request.CreateResponse(); response.Content = new StringContent(token); return(response); }
private static bool RouteToEvent(PlayerWebsocket socket, SocketMessage message) { if (!message.eventType.GetCustomAttributes(typeof(RoutableEvent)).Any()) { throw new Exception(string.Format("Event '{0}' is not marked as routable", message.action)); } var constructor = message.eventType.GetConstructors()[0]; var constructorParams = new object[constructor.GetParameters().Count()]; foreach (var param in constructor.GetParameters()) { try { Type type = param.ParameterType; object paramValue = null; if (param.Name == "playerID") { paramValue = socket.playerID; } else { var jsonParam = message.jsonPayload[param.Name]; if (jsonParam == null) { throw new ArgumentException(string.Format("{0}. parameter named '{1}' not found in payload", param.Position, param.Name)); } if (type.GetTypeInfo().IsEnum) { if (jsonParam.Value.GetType().Name == "String") { paramValue = Enum.Parse(type, (string)jsonParam.Value); } else { paramValue = Enum.ToObject(type, jsonParam.Value); } } else { if (type == typeof(string)) { paramValue = jsonParam.ToString(); } if (type == typeof(bool)) { paramValue = JsonConvert.DeserializeObject(jsonParam.ToString().ToLower(), type); } else { paramValue = JsonConvert.DeserializeObject(jsonParam.ToString(), type); } } } constructorParams[param.Position] = paramValue; } catch (Exception e) { throw new Exception(string.Format("Failed to parse param '{0}'.", param.Name), e); } } var eventObject = (Event)Activator.CreateInstance(message.eventType, constructorParams); //constructorParams EventEngine.AddEvent(eventObject); return(true); }