/// <summary> /// Broadcasts a position message to all online users /// </summary> /// <param name="Message">The chat message to be broadcasted</param> /// <param name="AContext">The user's connection context</param> private static void SetPosition(RCATContext RContext, ServerMessage message) { try { User user = new User(); user = (User)serializer.Deserialize(new JTokenReader(message.Data), typeof(User)); dataConnector.SetPosition(user.n, user.p, message.TimeStamp); string[] clients = dataConnector.GetAllUsersNames(); dynamic data = new { n = user.n, p = user.p }; RContext.Broadcast(data, clients, ResponseType.Position, message.TimeStamp); } catch (Exception e) { Log.Error(e); } }
/// <summary> /// Sends an error message to the client who caused the error /// </summary> /// <param name="ErrorMessage">Details of the error</param> /// <param name="AContext">The user's connection context</param> private static void SendError(string ErrorMessage, UserContext AContext, ServerMessage message) { Log.Warn("Error Message: " + ErrorMessage); Message r = new Message(); r = new Message(); r.Type = ResponseType.Error; r.Data = new { Message = ErrorMessage }; //AContext.Send(JsonConvert.SerializeObject(r)); }
/// <summary> /// Event fired when a client connects to the Alchemy Websockets server instance. /// Adds the client to the online users list. /// </summary> /// <param name="AContext">The user's connection context</param> public static void OnConnect(RCATContext RContext, ServerMessage message) { try { Log.Info("Client Connection From : " + (string)message.Data); SendAllUsers(RContext, message.Data); } catch (Exception e) { Log.Error("Exception in OnConnect", e); } }
// NOTE: This is not safe code. You may end up broadcasting to people who // disconnected. Luckily for us, Alchemy handles exceptions in its event methods, so we don't // have random, catastrophic changes. /// <summary> /// Event triggered when a user disconnected from a proxy /// Remove a user from the online users list /// and broadcast the disconnection message to all connected users. /// </summary> /// <param name="AContext">The user's connection context</param> public static void OnDisconnect(RCATContext RContext, ServerMessage message) { try { Log.Info("[PROXY->SERVANT]: User disconnected: " + message.Data); User user = dataConnector.GetUser(message.Data); Message r = new Message(); if (!String.IsNullOrEmpty(user.n)) { dataConnector.RemoveUser(user.n); string[] clients = dataConnector.GetAllUsersNames(); RContext.Broadcast(new { n = user.n }, clients, ResponseType.Disconnect, message.TimeStamp); } else Log.Warn("[PROXY->SERVANT]: User " + user.n + " not found OnDisconnect."); } catch (Exception e) { Log.Error("[PROXY->SERVANT]: Error in OnDisconnect.",e); } }
/// <summary> /// Sends client data to the server /// </summary> /// <param name="client"></param> public void SendPosition(User client, long timestamp) { //ServerContext server = clientPerServer[client.Name]; ServerContext server = PickServer(); ServerMessage resp = new ServerMessage(); resp.Type = ResponseType.Position; resp.Data = client; resp.TimeStamp = timestamp; server.Send(Newtonsoft.Json.JsonConvert.SerializeObject(resp)); }