/// <summary> /// Creates a report proxy that can send information to a /// websocket client /// </summary> /// <param name="context"></param> public WSReportProxy(UserContext context) { if (context == null) return; Context = context; Remote = WSReport; }
/// <summary> /// Registers a user with the system according to the given Alchemy Context. /// </summary> /// <param name="ctx">The user context.</param> public static void RegisterUser(UserContext ctx) { //create the user object User user = new User(ctx); //normally we would check the bool being returned from this function to see if it was added //but really the only time it wouldn't be added is if it already existed, which is ok in this case. users.TryAdd(ctx.ClientAddress.ToString(), user); }
/// <summary> /// Deregister the user with the given Alchemy Context. /// </summary> /// <param name="ctx">The user context.</param> public static void DeregisterUser(UserContext ctx) { User user; bool successful = users.TryRemove(ctx.ClientAddress.ToString(), out user); if (successful) { user.IsConnected = false; } }
/// <summary> /// Called when [receive]. /// </summary> /// <param name="context">The context.</param> private void OnReceiveContext(UserContext context) { if (OnReceive != null) { var json = context.DataFrame.ToString(); OnReceive(json); } }
/// <summary> /// Fired when a request for a map or maps is received. /// </summary> /// <param name="parameters">The parameters table.</param> /// <param name="ctx">The user context.</param> private static void mapRequest(Hashtable parameters, UserContext ctx) { if (parameters["mapid"].ToString() == "0") Server.Send(DB.Maps.GetAll(), ctx); else { int mapid = int.Parse(parameters["mapid"].ToString()); Server.Send(DB.Maps.Get(mapid), ctx); } }
/// <summary> /// Initializes a new instance of the <see cref="AlchemyWebSocket" /> class. /// </summary> /// <param name="context">The context.</param> /// <param name="logger">The logger.</param> /// <exception cref="System.ArgumentNullException">context</exception> public AlchemyWebSocket(UserContext context, ILogger logger) { if (context == null) { throw new ArgumentNullException("context"); } _logger = logger; UserContext = context; context.SetOnDisconnect(OnDisconnected); context.SetOnReceive(OnReceiveContext); _logger.Info("Client connected from {0}", context.ClientAddress); }
public override void OnDisconnect(UserContext context) { Console.WriteLine("OnDisconnect"); User user; try { user = _users.Where(o => o.Context.ClientAddress == context.ClientAddress).Single(); _users.Remove(user); } catch { return; } SendToAll(new Message { Detail = "has left the conversation.", From = user.Username, Type = Message.Activity }); }
/// <summary> /// Called when [receive]. /// </summary> /// <param name="context">The context.</param> private void OnReceive(UserContext context) { if (OnReceiveDelegate != null) { var json = context.DataFrame.ToString(); if (!string.IsNullOrWhiteSpace(json)) { try { var bytes = Encoding.UTF8.GetBytes(json); OnReceiveDelegate(bytes); } catch (Exception ex) { _logger.ErrorException("Error processing web socket message", ex); } } } }
/// <summary> /// Receive a message from the system and do something useful with it. /// </summary> /// <param name="context"></param> private void OnReceive(Alchemy.Classes.UserContext context) { try { string json = context.DataFrame.ToString(); object obj = JsonConvert.DeserializeObject(json); if (callbacks.ContainsKey(obj.GetType())) { CallContext callContext = new CallContext { Context = context, Data = obj, Callback = callbacks[obj.GetType()] }; ThreadPool.QueueUserWorkItem(new WaitCallback(this.QueueCallback), callContext); } } catch (Exception e) { ErrorResponse errorResponse = new ErrorResponse { OriginalData = new { e.Message } }; context.Send(JsonConvert.SerializeObject(errorResponse)); } }
public override void OnReceive(UserContext context) { Console.WriteLine("OnReceive"); var user = GetConnectedUserByContext(context); ReceivedMessage message; try { message = JsonConvert.DeserializeObject<ReceivedMessage>(context.DataFrame.ToString()); } catch { return; } if (message.Command == "Register") { user.Username = message.Alias; SendToAll(new Message { Detail = "has joined the conversation.", From = user.Username, Type = Message.Activity }); return; } var messageToSend = StripHTML(message.Message); SendToAll(new Message { Detail = messageToSend, From = user.Username, Type = Message.Convo }); }
/// <summary> /// Service a users request for an API key. /// </summary> /// <param name="parameters">The hash table of parameters associated with the api key request.</param> /// <param name="ctx">The user context.</param> public static void RequestApiKey(Hashtable parameters, UserContext ctx) { User user = Users.UserControl.GetUserByContext(ctx); string key = user.ApiKey; string username = parameters["user"].ToString(); string password = parameters["pass"].ToString(); if (user.ApiKey != String.Empty) { if (accessList.ContainsKey(user.ApiKey)) accessList[user.ApiKey] = getGroups(username, password); else { key = Generator.RandomAlphaNumeric(Config.ApiKeyLength); user.ApiKey = key; accessList.TryAdd(key, getGroups(username, password)); } } else { key = Generator.RandomAlphaNumeric(Config.ApiKeyLength); user.ApiKey = key; accessList.TryAdd(key, getGroups(username, password)); } DeliverApiKey(user); }
public User(int id, UserContext context) { this.id = id; this.context = context; }
/// <summary> /// Called when [disconnected]. /// </summary> /// <param name="context">The context.</param> private void OnDisconnected(UserContext context) { _disconnected = true; }
private void SendMessage(UserContext userContext, string message) { try { userContext.Send(message); } catch (Exception) { // Remove client //if (GameHosts.ContainsKey(userContext.ClientAddress.ToString())) // GameHosts.Remove(userContext.ClientAddress.ToString()); } }
public UserContextEventArgs(UserContext userContext) { UserContext = userContext; }
void wsClient_OnConnectedEvent(Object sender, UserContext context) { if (loginstates) return; ResponseFromTossServer data = new ResponseFromTossServer() { Type = Com.Huen.Sockets.CommandType.HeartBeatReq, Data = new _pms_data_type() }; wsClient.Send(JsonConvert.SerializeObject(data)); }
void wsClient_OnDisconnectEvent(Object sender, UserContext context) { if (loginstates) loginstates = false; }
private void OnDisconnect(UserContext context) { Console.WriteLine("Disconnect."); string dictionaryKey = context.ClientAddress.ToString(); if (ClientHandler.Games.Values.Contains(dictionaryKey)) { string key = ClientHandler.Games.First(pair => pair.Value.Equals(dictionaryKey)).Key; if (!String.IsNullOrEmpty(key) && ClientHandler.Games.ContainsKey(key)) { ClientHandler.Games.Remove(key); } } if (ClientHandler.GameHosts.ContainsKey(dictionaryKey)) { ClientHandler.GameHosts.Remove(dictionaryKey); } _server.Stop(); _server.Dispose(); }
private void OnReceive(UserContext context) { ThreadPool.QueueUserWorkItem(ClientHandler.HandleCommand, new Message(context.ClientAddress.ToString(), context.DataFrame.ToString())); }
private void OnConnect(UserContext context) { Console.WriteLine("Connected."); if (!ClientHandler.GameHosts.ContainsKey(context.ClientAddress.ToString())) ClientHandler.GameHosts.Add(context.ClientAddress.ToString(), context); }
/// <summary> /// Sending a message to the client. /// </summary> /// <param name="context"></param> private void OnSend(Alchemy.Classes.UserContext context) { log.InfoFormat("Send {0}", context.ClientAddress.ToString()); }
private void OnDisconnect(Alchemy.Classes.UserContext context) { log.InfoFormat("Disconnected {0}", context.ClientAddress.ToString()); }
public override void OnConnected(UserContext context) { Console.WriteLine("OnConnected"); _users.Add(new User { Context = context }); }
private void OnConnect(Alchemy.Classes.UserContext context) { _clients.Add(context); }
/// <summary> /// Initializes a new instance of the <see cref="Context"/> class. /// </summary> public Context(WebSocketServer server, TcpClient connection) { Server = server; Connection = connection; Buffer = new byte[_bufferSize]; UserContext = new UserContext(this); Cancellation = new CancellationTokenSource(); Cancellation = CancellationTokenSource.CreateLinkedTokenSource (Handler.Shutdown.Token, this.Cancellation.Token); ReceiveEventArgs = new SocketAsyncEventArgs(); SendEventArgs = new SocketAsyncEventArgs(); Handler.RegisterContext(this); if (connection != null) { UserContext.ClientAddress = connection.Client.RemoteEndPoint; } }
public AlchemySession(UserContext context) { this.context = context; }
private User GetConnectedUserByContext(UserContext context) { return _users.Where(u => u.Context.ClientAddress == context.ClientAddress).Single(); }
/// <summary> /// Initializes a new instance of the <see cref="Context"/> class. /// </summary> public Context(WebSocketServer server, TcpClient connection) { Server = server; Connection = connection; Buffer = new byte[_bufferSize]; UserContext = new UserContext(this); // mjb IsSecure = server.IsSecure; if (IsSecure == true) { //SslStream sslStream = new SslStream(connection.GetStream(), false); //sslStream.AuthenticateAsServer(server.SSLCertificate, false, System.Security.Authentication.SslProtocols.Default, false); //NetworkStream = sslStream; SslStream = new SslStream(connection.GetStream(), false); SslStream.AuthenticateAsServer(server.SSLCertificate, false, System.Security.Authentication.SslProtocols.Default, false); } else { //NetworkStream = Connection.GetStream(); } ReceiveEventArgs = new SocketAsyncEventArgs(); SendEventArgs = new SocketAsyncEventArgs(); Handler.RegisterContext(this); if (connection != null) { UserContext.ClientAddress = connection.Client.RemoteEndPoint; } }
void wsClient_OnSendEvent(Object sender, UserContext context) { }
/// <summary> /// Retrieve a user by their context. /// </summary> /// <param name="ctx">The users alchemy context.</param> /// <returns>The user object for the user.</returns> public static User GetUserByContext(UserContext ctx) { User user; users.TryGetValue(ctx.ClientAddress.ToString(), out user); return user; }
void wsClient_OnConnectEvent(Object sender, UserContext context) { }
/// <summary> /// Fired when a request to update the map database values is received. /// </summary> /// <param name="parameters">The parameters table.</param> /// <param name="ctx">The user context.</param> private static void mapUpdate(Hashtable parameters, UserContext ctx) { }
void wsClient_OnReceiveEvent(Object sender, UserContext context) { ResponseFromTossServer r = JsonConvert.DeserializeObject<ResponseFromTossServer>(context.DataFrame.ToString()); _pms_data_type pmsdata; Clean c = null; Debug.WriteLine(" >>> PMS r.Type: " + r.Type.ToString()); RoomItem roomitm = null; switch (r.Type) { case Com.Huen.Sockets.CommandType.RegisterRes: // write connected indentity break; case Com.Huen.Sockets.CommandType.HeartBeatRes: // write connection alive break; case Com.Huen.Sockets.CommandType.Message: // process message pmsdata = JsonConvert.DeserializeObject<_pms_data_type>(r.Data.ToString()); switch (pmsdata.cmd) { case STRUCTS.PMS_SET_MORNING_CALL_REQ: foreach (var floor in _floor) { foreach (var room in floor.list) { if (room.RoomNum.Equals(pmsdata.extension)) { room.Hour = pmsdata.hour; room.Minutes = pmsdata.minutes; break; } } } break; case STRUCTS.PMS_SET_LANGUAGE_REQ: foreach (var floor in _floor) { foreach (var room in floor.list) { if (room.RoomNum.Equals(pmsdata.extension)) { room.Languages = pmsdata.language.ToString(); break; } } } break; } break; case Com.Huen.Sockets.CommandType.MakeupRoomReq: case Com.Huen.Sockets.CommandType.MakeupRoomCancel: case Com.Huen.Sockets.CommandType.MakeupRoomDone: case Com.Huen.Sockets.CommandType.MakeupRoomConfirm: case Com.Huen.Sockets.CommandType.LaundaryReq: case Com.Huen.Sockets.CommandType.LaundaryCancel: case Com.Huen.Sockets.CommandType.LaundaryDone: case Com.Huen.Sockets.CommandType.ParcelExistEnd: case Com.Huen.Sockets.CommandType.DnDReq: case Com.Huen.Sockets.CommandType.DnDCancel: c = JsonConvert.DeserializeObject<Clean>(r.Data.ToString()); break; case Com.Huen.Sockets.CommandType.MorningCall: c = JsonConvert.DeserializeObject<Clean>(r.Data.ToString()); if (c == null) break; foreach (var floor in _floor) { roomitm = floor.list.FirstOrDefault(x => x.RoomNum.Equals(c.callee)); if (roomitm == null) continue; else break; } if (c.result == 502) { var pms = roomitm.PMSDATA; if (pms.repeat_week == 0) { roomitm.Hour = -1; roomitm.Minutes = -1; pms.hour = -1; pms.minutes = -1; roomitm.PMSDATA = pms; } } break; default: break; } if (c != null) { foreach (var floor in _floor) { roomitm = floor.list.FirstOrDefault(x => x.RoomNum.Equals(c.caller)); if (roomitm == null) continue; else break; } if (roomitm == null) return; if (r.Type == Com.Huen.Sockets.CommandType.MakeupRoomReq) { roomitm.States_Clean = "1"; } else if (r.Type == Com.Huen.Sockets.CommandType.MakeupRoomCancel) { roomitm.States_Clean = "0"; } else if (r.Type == Com.Huen.Sockets.CommandType.MakeupRoomDone) { roomitm.States_Clean = "2"; } else if (r.Type == Com.Huen.Sockets.CommandType.MakeupRoomConfirm) { roomitm.States_Clean = "3"; } else if (r.Type == Com.Huen.Sockets.CommandType.LaundaryReq) { roomitm.States_Laundary = "1"; } else if (r.Type == Com.Huen.Sockets.CommandType.LaundaryCancel) { roomitm.States_Laundary = "0"; } else if (r.Type == Com.Huen.Sockets.CommandType.LaundaryDone) { roomitm.States_Laundary = "0"; } else if (r.Type == Com.Huen.Sockets.CommandType.DnDReq) { roomitm.States_DnD = "1"; } else if (r.Type == Com.Huen.Sockets.CommandType.DnDCancel) { roomitm.States_DnD = "0"; } } }
private void RemoveUser(UserContext context) { _activeUsers.Remove(context); }