public SharedChatMessage ChatMessageSave(SharedChatMessage msg) { //:messageid, :channelid, :userid, :usertype, :username, :isturbo, :channelname, :message, :sentiment SQLiteParameter[] sQLiteParameters = new SQLiteParameter[] { new SQLiteParameter(":messageid", msg.MessageId), new SQLiteParameter(":channelid", msg.ChannelId), new SQLiteParameter(":userid", msg.UserId), new SQLiteParameter(":usertype", msg.UserType), new SQLiteParameter(":username", msg.UserName), new SQLiteParameter(":isturbo", msg.IsTurbo?1:0), new SQLiteParameter(":channelname", msg.ChannelName), new SQLiteParameter(":message", msg.Message), new SQLiteParameter(":sentiment", msg.Sentiment) }; // Insert SharedChatMessage result = msg; try { lock (this) { string sql = InsertChatMessageSQL; m_context.Database.ExecuteSqlCommand(sql, sQLiteParameters); } int resultid = m_context.Database.SqlQuery <int>(GetLastInsertedChatMessageSQL, new SQLiteParameter[] { }).FirstOrDefault(); result.MId = resultid; } catch { } return(result); }
public SharedChatMessage InsertChatMessage(SharedChatMessage message) { return(data.ChatMessageSave(message)); }
/// <summary> /// Page load event. /// </summary> /// <param name="req"> /// The request from the browser. /// </param> protected override void PageLoad(HttpRequest req) { /// /// The ChatServer application is strictly relate with the javascript implementation, /// so if the request contains query url string parameters means that invoking by the XMLHttpRequest object (ajax pattern). /// You can choose a different way to distinguish 'pure' pageload event (called in Asp.net postback) from ajax event. /// /// The query url request has this pattern: ?op=<operation>&<par1>=<value1>&<par2>=<value2> where op stands for 'operation'. /// string page = Request.Paths[Request.Paths.Count - 1]; if (req.UrlParameters.Count > 0) { SharedChatMessage sharedresponse = null; ChatMessage message = null; string operation = req.GetQueryStringValue("op"); switch (operation) { case "login": /// /// Login operation process steps: /// 1) Check username and password , if one of these is empty we respond with an alert /// 2) Validation of the user /// 3) Respond with a redirect /// string username = req.GetQueryStringValue("username"); string password = req.GetQueryStringValue("password"); if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(password)) { BuildChatResponse(new ChatMessage() { MessageCode = (int)MessageType.alert, Value = "Login request error." }, true); return; } currentUsername = username; currentPassowrd = password; isValidUser = true; BuildChatResponse(new ChatMessage() { MessageCode = (int)MessageType.eval, Value = "window.location=\"" + roomPage + "\"" }, true); return; case "listen": /// /// When the room page is loaded start to send 'listen' operation request in loop, /// at every listen request we respond with a chatmessage getting from the queue or,if is empty ,with a skip action message. /// But firstable we sent adduser action message with SharedChatMessage envelop for notify the new user. /// if (!sendAdduser) { sendAdduser = true; message = new ChatMessage() { MessageCode = (int)MessageType.adduser, User = currentUsername, }; BuildChatResponse(message, false); this.response = sharedresponse = new SharedChatMessage(Response.ResponseData, Response.AppRequest, message); return; } if (localqueuemessages.Count == 0) { System.Threading.Thread.Sleep(500); BuildChatResponse(new ChatMessage() { MessageCode = (int)MessageType.skip, Value = "" }, false); } else { ChatMessage msg = null; if (localqueuemessages.TryDequeue(out msg)) BuildChatResponse(msg, false); } return; case "message": /// /// A chat message is sent by the user, /// firstable we build ChatMessage packet replacing the response with SharedChatMessage envelop, /// that is why SharedChatMessage is visible to the other session (see OnNewShareResponse). /// /// string value = req.GetQueryStringValue("value"); message = new ChatMessage() { MessageCode = (int)MessageType.chatmessage, Value = value, User = currentUsername }; BuildChatResponse(message, false); sharedresponse = new SharedChatMessage(Response.ResponseData, Response.AppRequest, message); Response = sharedresponse; return; default: throw new InvalidOperationException("Invalid request"); } } if (page == roomPage) { /// /// if the user not perform the login the roomPage will not be loaded, will be sent a login page /// if (!isValidUser) { BuildResponseFile(ApplicationDirectory() + "\\" + loginPage,MimeType.text_html); return; } else { byte[] room = Helper.GetFile(ApplicationDirectory() + "\\" + roomPage); string msg = new string(Encoding.UTF8.GetChars(room)); msg = msg.Replace("<%ws_username%>", this.currentUsername); BuildResponse(msg); } } }