/// <summary> /// Handler called when a new message from a client arrives to the server. /// </summary> /// <param name="p_webSocketSession">The connection session.</param> /// <param name="p_message">The message send by the client session.</param> private void NewMessageReceived(WebSocketSession p_webSocketSession, string p_message) { WebSocketMessage v_request = JsonConvert.DeserializeObject <WebSocketMessage>(p_message); if (v_request.v_code == (int)request.Login) { string v_userId = (string)v_request.v_data; if (!p_webSocketSession.Cookies.ContainsKey("user_id")) { p_webSocketSession.Cookies.Add("user_id", v_userId); } } WebSocketMessage v_response = new WebSocketMessage(); if (!this.v_httpSessions.ContainsKey(p_webSocketSession.Cookies["user_id"])) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } Session v_httpSession = this.v_httpSessions[p_webSocketSession.Cookies["user_id"]]; if (v_httpSession == null) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } switch (v_request.v_code) { case (int)request.Login: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; List <ChatUser> v_userList = new List <ChatUser>(); try { string v_onlineUsers = ""; for (int i = 0; i < this.v_chatSessions.Count; i++) { if (this.v_chatSessions[i].Cookies.ContainsKey("user_id")) { v_onlineUsers += this.v_chatSessions[i].Cookies["user_id"] + ", "; } } v_onlineUsers = v_onlineUsers.Remove(v_onlineUsers.Length - 2); string v_sql = "select x.*" + "from (" + " select user_id, " + " user_name, " + " 1 as online " + " from users " + " where user_id in ( " + v_onlineUsers + ") " + " " + " union " + " " + " select user_id, " + " user_name, " + " 0 as online " + " from users " + " where user_id not in ( " + v_onlineUsers + ") " + ") x " + "order by x.online desc, x.user_name "; System.Data.DataTable v_table = v_database.v_connection.Query(v_sql, "chat_users"); if (v_table != null && v_table.Rows.Count > 0) { for (int i = 0; i < v_table.Rows.Count; i++) { ChatUser v_user = new ChatUser(); v_user.v_user_id = int.Parse(v_table.Rows[i]["user_id"].ToString()); v_user.v_user_name = v_table.Rows[i]["user_name"].ToString(); v_user.v_user_online = int.Parse(v_table.Rows[i]["online"].ToString()); v_userList.Add(v_user); } } } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.UserList; v_response.v_data = v_userList; SendToAllClients(v_response); return; } case (int)request.GetOldMessages: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; List <ChatMessage> v_messageList = new List <ChatMessage>(); try { string v_sql = "select mes.mes_in_code, " + " use.user_name, " + " mes.mes_st_text, " + " mes.mes_dt_timestamp " + "from messages mes " + "inner join messages_users meu " + " on mes.mes_in_code = meu.mes_in_code " + "inner join users use " + " on mes.user_id = use.user_id " + "where meu.user_id = " + v_httpSession.v_user_id + " " + " and meu.meu_bo_viewed = 'N';"; System.Data.DataTable v_table = v_database.v_connection.Query(v_sql, "chat_messages"); if (v_table != null && v_table.Rows.Count > 0) { for (int i = 0; i < v_table.Rows.Count; i++) { ChatMessage v_message = new ChatMessage(); v_message.v_message_id = int.Parse(v_table.Rows[i]["mes_in_code"].ToString()); v_message.v_user_name = v_table.Rows[i]["user_name"].ToString(); v_message.v_text = v_table.Rows[i]["mes_st_text"].ToString(); v_message.v_timestamp = v_table.Rows[i]["mes_dt_timestamp"].ToString(); v_messageList.Add(v_message); } } } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.OldMessages; v_response.v_data = v_messageList; SendToClient(p_webSocketSession, v_response); return; } case (int)request.ViewMessage: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; ChatMessage v_message = JsonConvert.DeserializeObject <ChatMessage>(v_request.v_data.ToString()); try { string v_sql = "update messages_users " + "set meu_bo_viewed = 'Y' " + "where user_id = " + v_httpSession.v_user_id + " " + " and mes_in_code = " + v_message.v_message_id; v_database.v_connection.Execute(v_sql); } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } return; } case (int)request.SendMessage: { OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; string v_text = (string)v_request.v_data; ChatMessage v_message; try { string v_sql = "insert into messages (" + " mes_st_text, " + " mes_dt_timestamp, " + " user_id " + ") values ( " + " '" + v_text + "', " + " datetime('now', 'localtime'), " + " " + v_httpSession.v_user_id + ");" + "select max(mes_in_code) " + "from messages;"; int v_messsageCode = int.Parse(v_database.v_connection.ExecuteScalar(v_sql)); v_sql = "insert into messages_users (" + " mes_in_code, " + " meu_bo_viewed, " + " user_id " + ")" + "select " + v_messsageCode + ", " + " 'N', " + " use.user_id " + "from users use "; // + //"where use.user_id <> " + v_httpSession.v_user_id + ";"; v_database.v_connection.Execute(v_sql); v_sql = "select mes_dt_timestamp " + "from messages " + "where mes_in_code = " + v_messsageCode; v_message = new ChatMessage(); v_message.v_message_id = v_messsageCode; v_message.v_user_name = v_httpSession.v_user_name; v_message.v_text = v_text; v_message.v_timestamp = v_database.v_connection.ExecuteScalar(v_sql); } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.NewMessage; v_response.v_data = v_message; SendToAllClients(v_response); return; } default: { v_response.v_error = true; v_response.v_data = "Unrecognized request code."; SendToClient(p_webSocketSession, v_response); return; } } /*Thread v_sendResponse = new Thread(SendResponse); * v_sendResponse.Start((Object)p_webSocketSession);*/ }
/// <summary> /// Sends a message to the client that generated the request. /// </summary> /// <param name="p_webSocketSession">The connection session.</param> /// <param name="p_message">The message to be send to the client.</param> private void SendToClient(WebSocketSession p_webSocketSession, WebSocketMessage p_message) { p_webSocketSession.Send(JsonConvert.SerializeObject(p_message)); }
private void ExecuteQuery(WebSocketMessage p_response, WebSocketSession p_webSocketSession, string p_query_data, Session v_httpSession) { WebSocketMessage v_response = p_response; QueryData v_query_data = JsonConvert.DeserializeObject <QueryData>(p_query_data); v_response.v_code = (int)response.QueryResult; QueryReturn v_g1 = new QueryReturn(); System.Collections.Generic.List <System.Collections.Generic.List <string> > v_table = new System.Collections.Generic.List <System.Collections.Generic.List <string> >(); OmniDatabase.Generic v_database2 = v_httpSession.v_databases[v_query_data.v_db_index]; OmniDatabase.Generic v_database = OmniDatabase.Generic.InstantiateDatabase( v_database2.v_alias, v_database2.v_conn_id, v_database2.v_db_type, v_database2.v_server, v_database2.v_port, v_database2.v_service, v_database2.v_user, v_database2.v_connection.v_password, v_database2.v_schema ); v_database.v_connection.SetTimeout(0); if (v_query_data.v_cmd_type == -2) { try { v_httpSession.Execute(v_database, v_query_data.v_sql_cmd, true, true); } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } catch (System.InvalidOperationException e) { v_response.v_error = true; v_response.v_data = e.Message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } } else if (v_query_data.v_cmd_type == -3) { string[] v_commands = v_query_data.v_sql_cmd.Split(';'); string v_return_html = ""; int v_num_success_commands = 0; int v_num_error_commands = 0; v_database.v_connection.Open(); foreach (string v_command in v_commands) { if (v_command.Trim() != "") { try { v_httpSession.Execute(v_database, v_command, true, true); v_num_success_commands++; } catch (Spartacus.Database.Exception e) { v_num_error_commands++; v_return_html += "<b>Command:</b> " + v_command + "<br/><br/><b>Message:</b> " + e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>") + "<br/><br/>"; } } } v_response.v_data = "<b>Successful commands:</b> " + v_num_success_commands + "<br/>"; v_response.v_data += "<b>Errors: </b> " + v_num_error_commands + "<br/><br/>"; if (v_num_error_commands > 0) { v_response.v_data += "<b>Errors details:</b><br/><br/>" + v_return_html; } v_database.v_connection.Close(); } else { try { System.Collections.Generic.List <string> v_columns; if (v_query_data.v_cmd_type == -1) { v_table = v_httpSession.QueryList(v_database, v_query_data.v_sql_cmd, true, true, out v_columns); } else { v_table = v_httpSession.QueryListLimited(v_database, v_query_data.v_sql_cmd, v_query_data.v_cmd_type, true, false, out v_columns); } v_g1.v_query_info = "Number of records: " + v_table.Count.ToString(); v_g1.v_data = v_table; v_g1.v_col_names = v_columns; v_response.v_data = v_g1; } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } catch (System.InvalidOperationException e) { v_response.v_error = true; v_response.v_data = e.Message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } catch (System.Data.DuplicateNameException e) { v_response.v_error = true; v_response.v_data = e.Message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } } SendToClient(p_webSocketSession, v_response); }
/// <summary> /// Handler called when a connection is closed. /// </summary> /// <param name="p_webSocketSession">The connection session.</param> /// <param name="p_reason">The reason why connection was closed.</param> private void SessionClosed(WebSocketSession p_webSocketSession, CloseReason p_reason) { lock (v_chatSessionsSyncRoot) this.v_chatSessions.Remove(p_webSocketSession); if (p_reason == CloseReason.ServerShutdown) { return; } WebSocketMessage v_response = new WebSocketMessage(); if (!this.v_httpSessions.ContainsKey(p_webSocketSession.Cookies["user_id"])) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } Session v_httpSession = this.v_httpSessions[p_webSocketSession.Cookies["user_id"]]; if (v_httpSession == null) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } OmniDatabase.Generic v_database = v_httpSession.v_omnidb_database; List <ChatUser> v_userList = new List <ChatUser>(); try { string v_onlineUsers = ""; for (int i = 0; i < this.v_chatSessions.Count; i++) { if (this.v_chatSessions[i].Cookies.ContainsKey("user_id")) { v_onlineUsers += this.v_chatSessions[i].Cookies["user_id"] + ", "; } } v_onlineUsers = v_onlineUsers.Remove(v_onlineUsers.Length - 2); string v_sql = "select x.*" + "from (" + " select user_id, " + " user_name, " + " 1 as online " + " from users " + " where user_id in ( " + v_onlineUsers + ") " + " " + " union " + " " + " select user_id, " + " user_name, " + " 0 as online " + " from users " + " where user_id not in ( " + v_onlineUsers + ") " + ") x " + "order by x.online desc, x.user_name "; System.Data.DataTable v_table = v_database.v_connection.Query(v_sql, "chat_users"); if (v_table != null && v_table.Rows.Count > 0) { for (int i = 0; i < v_table.Rows.Count; i++) { ChatUser v_user = new ChatUser(); v_user.v_user_id = int.Parse(v_table.Rows[i]["user_id"].ToString()); v_user.v_user_name = v_table.Rows[i]["user_name"].ToString(); v_user.v_user_online = int.Parse(v_table.Rows[i]["online"].ToString()); v_userList.Add(v_user); } } } catch (Spartacus.Database.Exception e) { v_response.v_error = true; v_response.v_data = e.v_message.Replace("<", "<").Replace(">", ">").Replace(System.Environment.NewLine, "<br/>"); SendToClient(p_webSocketSession, v_response); return; } v_response.v_code = (int)response.UserList; v_response.v_data = v_userList; SendToAllClients(v_response); }
/// <summary> /// Handler called when a new message from a client arrives to the server. /// </summary> /// <param name="p_webSocketSession">The connection session.</param> /// <param name="p_message">The message send by the client session.</param> private void NewMessageReceived(WebSocketSession p_webSocketSession, string p_message) { WebSocketMessage v_request = JsonConvert.DeserializeObject <WebSocketMessage>(p_message); if (v_request.v_code == (int)request.Login) { string v_userId = (string)v_request.v_data; if (!p_webSocketSession.Cookies.ContainsKey("user_id")) { p_webSocketSession.Cookies.Add("user_id", v_userId); } } WebSocketMessage v_response = new WebSocketMessage(); v_response.v_context_code = v_request.v_context_code; if (!this.v_httpSessions.ContainsKey(p_webSocketSession.Cookies["user_id"])) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } Session v_httpSession = this.v_httpSessions[p_webSocketSession.Cookies["user_id"]]; if (v_httpSession == null) { v_response.v_error = true; v_response.v_data = "Session Object was destroyed. Please, restart the application."; SendToClient(p_webSocketSession, v_response); return; } switch (v_request.v_code) { case (int)request.Login: { v_response.v_code = (int)response.LoginResult; SendToClient(p_webSocketSession, v_response); return; } case (int)request.Query: { Thread thread = new Thread(() => ExecuteQuery(v_response, p_webSocketSession, v_request.v_data.ToString(), v_httpSession)); thread.Start(); return; } default: { v_response.v_error = true; v_response.v_data = "Unrecognized request code."; SendToClient(p_webSocketSession, v_response); return; } } }