Beispiel #1
0
        /// <summary>
        /// Page load event
        /// </summary>
        /// <param name="req"></param>
        protected override void PageLoad(HttpRequest req)
        {
            try
            {
                if (req.UrlParameters.Count > 0)
                {
                    ///
                    /// the client send
                    ///

                    string operation = req.GetQueryStringValue("op");
                    switch (operation)
                    {
                        case "listen":
                            //  Thread.Sleep(1000);
                            string jsonsettings = req.GetQueryStringValue("set");
                            FrameIndex settings = JsonConvert.DeserializeObject<FrameIndex>(HttpHelper.CleanJsonString(jsonsettings));
                            string frame = imgPrefix + (globalcounter++) + img_value_delimeter + settings.ZoomX + img_value_delimeter + settings.ZoomY + img_value_delimeter + ".jpg";
                            SendMessage(new FrameIndex() { Code = MessageType.getFrame, Img = frame }, false);
                            break;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
Beispiel #2
0
        /// <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);
                }
            }
        }