Esempio n. 1
0
        //When PokeIn sees ChatMessage custom class as a parameter, it automaticly defines ChatMessage JS class on client side.
        public void Send(ChatMessage message)
        {
            //Create JSON method from custom class
            string json = JSON.Method("ChatMessageFrom", message); //ChatMessageFrom( {Username:'******', Message:'message' } );

            CometWorker.SendToAll(json);
        }
Esempio n. 2
0
        //Client side calls this method to mark his status and start listen for his events
        public void StartListenForEvents()
        {
            string sessionId = CometWorker.GetSessionId(ClientId);

            //to make consistent log-in process lock the user status
            lock (MessageBroker.PreUserDefinitions)
            {
                string userName;
                MessageBroker.SessionUserPair.TryGetValue(sessionId, out userName);

                if (userName == null)//User Session Lost
                {
                    PokeIn.Comet.BrowserHelper.RedirectPage(ClientId, "/Sample/Home");
                    return;
                }

                MessageBroker.PreUserDefinitions.TryGetValue(userName, out userDefinition);
            }

            string jsonMethod = PokeIn.JSON.Method("SetTitle", "Hello " + userDefinition.Username + "! ");

            CometWorker.SendToClient(ClientId, jsonMethod);


            //Call Role Based User Initializitation
            InitializeUser();
        }
Esempio n. 3
0
        public void gotShot(string xs, string ys)
        {
            string playerId = otherPlayerId;

            CometWorker.SendToClient(playerId,
                                     EXTML.Method("playerShot:andY:andWidth:andHeight:", xs, ys, opponentSize.Width, opponentSize.Height));
        }
Esempio n. 4
0
        public void GetServerTime()
        {
            string jsonMethod = JSON.Method("UpdateTime", DateTime.Now);

            CometWorker.SendToClient(_clientId, jsonMethod);
            CometWorker.SendToAll(jsonMethod);
        }
Esempio n. 5
0
        public void SetName(string userName)
        {
            if (_username != "")
            {
                CometWorker.SendToClient(_clientId, "alert('You already have a username!');btnChat.disabled = '';");
                return;
            }

            lock (Names)
            {
                if (Names.ContainsKey(userName))
                {
                    Names[userName] = _clientId;
                }
                else
                {
                    Names.Add(userName, _clientId);
                    Users.Add(_clientId, userName);
                }
            }
            _username = userName;

            //Create JSON Method
            string json = JSON.Method("UsernameSet", userName);//UsernameSet('UserName');

            CometWorker.SendToClient(_clientId, json);
        }
Esempio n. 6
0
        public void SendPrivateMessage(string destinationUser, string message)
        {
            string privateMessage = "<strong>" + GetUserName(_clientID) + "</strong>::" + message;
            string json           = PokeIn.JSON.Method("HandlePrivateMessage", privateMessage);

            CometWorker.SendToClient(destinationUser, json);
        }
Esempio n. 7
0
        public void UpdateJoint()
        {
            string message = JSON.Method("SharedNumber", number);

            //Send the above message directly to the joint
            CometWorker.SendToAll(message);
        }
Esempio n. 8
0
        static void CometWorker_OnClientCreated(string clientId)
        {
            //Client connection is done
            string message = JSON.Method("UpdateString", "Now, you are connected!");

            CometWorker.SendToClient(clientId, message);
        }
Esempio n. 9
0
        public void SetName(string userName)
        {
            if (_username != "")
            {
                CometWorker.SendToClient(_clientID, "alert('You already have a username!');");
                return;
            }
            lock (Names)
            {
                if (Names.ContainsKey(userName))
                {
                    CometWorker.SendToClient(_clientID, "alert('Another user is using the name you choose! Please try another one.');");
                    return;
                }
                else
                {
                    Names.Add(userName, _clientID);
                    Users.Add(_clientID, userName);
                    _username = userName;
                }
            }

            CometWorker.SendToClient(_clientID, JSON.Method("UsernameSet", PokeIn.JSON.Tidy(userName)));
            CometWorker.SendToAll(JSON.Method("AppendToChat", "<strong>" + PokeIn.JSON.Tidy(userName) + " has enterred the building!</strong>"));
            RenderMemberList();
        }
Esempio n. 10
0
        static void CheckMessages()
        {
            PokeInWCFMessageFormat[] results = null;

            try
            {
                results = proxy.PingMessages();
            }
            catch (System.Net.WebException e)
            {
                ServerConnected = false;
                CometWorker.SendToAll("UpdateServiceStatus(" + ServerConnected.ToString().ToLower() + ");");
                ConnectToWCF();
            }
            if (results != null)
            {
                if (results.Length > 0)
                {
                    foreach (PokeInWCFMessageFormat message in results)
                    {
                        CometWorker.SendToClients(message.Clients, message.Message);
                    }
                }
            }

            //check messages every 1 second
            //You may implement a WCF instance to both side for efficiency or check out the Multiple Server sample project
            Thread.Sleep(1000);
            CheckMessages();
        }
Esempio n. 11
0
 public void playerFires(int location)
 {
     if (otherPlayerId != string.Empty)
     {
         CometWorker.SendToClient(otherPlayerId, EXTML.Method("playerShot", location));
     }
 }
Esempio n. 12
0
        public void Test()
        {
            //the below number will be increased for only the owner of this instance
            number++;
            string message = JSON.Method("NonSharedNumber", _clientId, number);

            //Send the above message directly to the client
            CometWorker.SendToJoint(_jointId, message);
        }
Esempio n. 13
0
 public void Dispose()
 {
     lock (Names)
     {
         Users.Remove(_clientID);
         Names.Remove(_username);
     }
     RenderMemberList();
     CometWorker.SendToAll(JSON.Method("AppendToChat", "<strong>" + _username + " has left the building!</strong>"));
 }
Esempio n. 14
0
        static void CometWorker_FileUploadRequested(ref HttpFile file)
        {
            string fileName = file.ServerMapPath + "\\Upload\\" + file.FileName;

            file.SaveAs(fileName);
            string resourceName = file.FileName.Replace("&", "_");

            ResourceManager.AddReplaceResource(fileName, resourceName, ResourceType.Image, file.ClientId);
            CometWorker.SendToClient(file.ClientId, JSON.Method("ShowImage", resourceName));
        }
Esempio n. 15
0
        public WCFSample(string clientId)
        {
            ClientId = clientId;
            CometWorker.SendToClient(ClientId, "UpdateServiceStatus(" + ServerConnected.ToString().ToLower() + ");");

            if (ServerConnected)
            {
                proxy.AddClient(clientId);
            }
        }
Esempio n. 16
0
 static void UpdateClients()
 {
     while (true)
     {
         if (CometWorker.ActiveClientCount > 0)
         {
             CometWorker.SendToAll(JSON.Method("UpdateTime", DateTime.Now));
         }
         Thread.Sleep(500);
     }
 }
Esempio n. 17
0
        protected override void InitializeUser()
        {
            //Admin screen might be open on different browsers or machines
            UsersPool.AdminClientIDs.Add(ClientId);//Admin logged in

            //let him know for waiting user requests
            lock (UsersPool.Requests)
            {
                string jsonMethod = PokeIn.JSON.Method("RequestList", UsersPool.Requests);
                CometWorker.SendToClient(ClientId, jsonMethod);
            }
        }
Esempio n. 18
0
        public void joinGame(int w, int h)
        {
            screenSize = new Size(w, h);

            if (w == 0 || h == 0)
            {
                return;
            }

            lock (waitingRoom)
            {
                if (waitingRoom.Any())
                {
                    otherPlayerId = waitingRoom.First();
                    waitingRoom.Remove(otherPlayerId);
                }
                else
                {
                    waitingRoom.Add(details.ClientId);
                    Log(details.ClientId + " joined to room");
                    return;
                }
            }

            GondorGame gm = null;

            try
            {
                CometWorker.GetClientObject(otherPlayerId, "Server", out gm);
            }catch {}

            if (gm == null)
            {
                Log("Game object for " + otherPlayerId + " not found");
                lock (waitingRoom)
                {
                    waitingRoom.Add(details.ClientId);
                }
                return;
            }

            int[] nums = prepareGame();
            Log(otherPlayerId + " game starts");
            opponentSize    = gm.screenSize;
            gm.opponentSize = screenSize;

            gm.startGame(nums);

            gm.otherPlayerId = details.ClientId;
            startGame(nums);
            Log(details.ClientId + " game starts");
        }
Esempio n. 19
0
        public static void UpdateAdminScreens(string userName, int appleCount)
        {
            lock (AdminClientIDs)
            {
                if (AdminClientIDs.Count > 0)//admin logged in
                {
                    string jm = JSON.Method("UpdateUserRequest", userName, appleCount);

                    //send message to each admin screen
                    CometWorker.SendToClients(AdminClientIDs.ToArray(), jm);
                }
            }
        }
Esempio n. 20
0
        public void ClassTest(TestClass tc, DateTime dt)
        {
            string message = "";

            if (IsDesktop)
            {
                message = EXTML.Method("TestClassReceived", tc.number, tc.items.Count, tc.text, dt.ToShortDateString());
            }
            else
            {
                message = JSON.Method("TestClassReceived", tc.number, tc.items.Count, tc.text, dt.ToShortDateString());
            }
            CometWorker.SendToClient(ClientId, message);
        }
Esempio n. 21
0
        protected void Button2_Click(object sender, EventArgs e)
        {
            Test   test;
            string clientId = this.HiddenField1.Value;

            if (clientId != "")
            {
                //get the pokein instance
                CometWorker.GetClientObject <Test>(clientId, "Test", out test);

                //call pokein method of this instance
                test.GetTime();
            }
        }
Esempio n. 22
0
        //First OnClientCreated Event Handler Called By Default.aspx
        public static void OnClientCreatedToFirst(string ClientId)
        {
            //reading data from session
            object   obj = CometWorker.ReadSessionObject("openingTime");
            DateTime dt  = DateTime.MinValue;

            if (obj != null)
            {
                dt = (DateTime)obj;
            }

            //Do not send a message to the client during its OnClientConnected phase.
            //Instead use this event in order to start sending messages
            CometWorker.SendToClient(ClientId, JSON.Method("s", "OnClientCreatedToFirst event is fired, page loaded at" + dt.ToLongTimeString()));
        }
Esempio n. 23
0
        public void UnSubscribe()
        {
            CometWorker.Groups.UnpinClient(ClientId);
            string message = "";

            if (IsDesktop)
            {
                message = EXTML.Method("UnSubscribed");
            }
            else
            {
                message = JSON.Method("UnSubscribed");
            }

            CometWorker.SendToClient(ClientId, message);
        }
Esempio n. 24
0
        //Event Fired!
        void AdminSent(string userName)
        {
            if (userName == userDefinition.Username)
            {
                //update our definition
                MessageBroker.PreUserDefinitions.TryGetValue(userName, out userDefinition);

                int countLeft = 0;
                lock (UsersPool.Requests)
                {
                    UsersPool.Requests.TryGetValue(userDefinition.Username, out countLeft);
                }

                CometWorker.SendToClient(ClientId, "AdminSentApple(" + userDefinition.Apples.ToString() + "," + countLeft.ToString() + ");");
            }
        }
Esempio n. 25
0
        public void Subscribe()
        {
            string message = "";

            if (IsDesktop)
            {
                CometWorker.Groups.PinClientID(ClientId, "ServerTime-Desktop");
                message = EXTML.Method("Subscribed");
            }
            else
            {
                CometWorker.Groups.PinClientID(ClientId, "ServerTime-Web");
                message = JSON.Method("Subscribed");
            }

            CometWorker.SendToClient(ClientId, message);
        }
Esempio n. 26
0
        public void leaveRoom()
        {
            lock (sessionKeys)
            {
                sessionKeys.Remove(details.ClientId);
            }

            lock (waitingRoom)
            {
                waitingRoom.Remove(details.ClientId);
            }

            if (otherPlayerId != string.Empty)
            {
                CometWorker.SendToClient(otherPlayerId, EXTML.Method("PlayerLeft"));
                otherPlayerId = "";
            }
        }
Esempio n. 27
0
        /// <summary>
        /// Sets a server side function target for an event of client element
        /// </summary>
        /// <param name="clientId">The client id.</param>
        /// <param name="elementId">The element id.</param>
        /// <param name="eventName">Name of the event.</param>
        /// <param name="eventTarget">The event target.</param>
        /// <param name="returnValue">The return value.</param>
        public static void SetElementEvent(string clientId, string elementId, string eventName, ClientElementEventReceived eventTarget, string returnValue)
        {
            string fakeId     = elementId.ToLower().Trim();
            string objectType = "document.getElementById('" + elementId + "')";

            if (fakeId == "body" || fakeId == "window" || fakeId == "document" || fakeId == "document.body")
            {
                if (fakeId == "body")
                {
                    fakeId = "document.body";
                }
                objectType = fakeId;
            }
            string simpleName = elementId + "_" + eventName;

            bool hasClient;

            lock (CometWorker.ClientStatus)
            {
                hasClient = CometWorker.ClientStatus.ContainsKey(clientId);
            }
            if (hasClient)
            {
                lock (CometWorker.ClientStatus[clientId])
                {
                    if (CometWorker.ClientStatus[clientId].Events.ContainsKey(simpleName))
                    {
                        CometWorker.ClientStatus[clientId].Events.Remove(simpleName);
                    }
                    CometWorker.ClientStatus[clientId].Events.Add(simpleName, eventTarget);
                }
            }

            if (returnValue.Trim().Length == 0)
            {
                returnValue = "\"\"";
            }

            CometWorker.SendToClient(clientId, @"
            document.__" + simpleName + " = function(ev){PokeIn.Send(PokeIn.GetClientId()+'.BrowserEvents.Fired("
                                     + elementId + "," + eventName + "," + returnValue + ");'); };function c3eb(){var _item = "
                                     + objectType + "; PokeIn.AddEvent(_item, '" + eventName + "', document.__"
                                     + simpleName + ");}" + "\nc3eb();\n");
        }
Esempio n. 28
0
        static ServerInstance()
        {
            //Define image resource
            ResourceManager.AddResource(CometWorker.GetApplicationPath() + "pokein_logo.gif" //resource location
                                        , "Logo"                                             //public name of the resource
                                        , ResourceType.Image                                 //Image type
                                        , string.Empty                                       //This resource is application wide
                                        );

            string message = "PokeIn Library";

            byte[] bt = CometSettings.SerializationEncoding.GetBytes(message);
            //Define text resource
            ResourceManager.AddResource(ref bt
                                        , "Message"         //public name of the resource
                                        , ResourceType.Text //Text type
                                        , "txt"             //file type
                                        , string.Empty      //This resource is application wide
                                        );

            new Thread(delegate()
            {
                while (!CometWorker.IsApplicationRecycling)
                {
                    if (CometWorker.Groups.GroupHasMembers("ServerTime-Desktop"))
                    {
                        string ext = EXTML.Method("ServerTimeUpdated", DateTime.Now);
                        CometWorker.Groups.Send("ServerTime-Desktop", ext);
                    }

                    if (CometWorker.Groups.GroupHasMembers("ServerTime-Web"))
                    {
                        string json = JSON.Method("ServerTimeUpdated", DateTime.Now);
                        CometWorker.Groups.Send("ServerTime-Web", json);
                    }

                    Thread.Sleep(800);
                }
            }).Start();
        }
Esempio n. 29
0
        public void DeleteFilesInFolder()
        {
            int fileCount = 0;

            try
            {
                string[] files = System.IO.Directory.GetFiles(FilePath + "\\Upload");
                fileCount = files.Length;
                foreach (string file in files)
                {
                    System.IO.File.Delete(file);
                }
            }
            catch (Exception e)
            {
                CometWorker.SendToClient(ClientID, JSON.Method("Error", e.Message));
            }
            finally
            {
                CometWorker.SendToClient(ClientID, JSON.Method("FilesDeleted", fileCount));
            }
        }
Esempio n. 30
0
        private void MsdosOutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            if (e.Data != null && !disposed)
            {
                if (e.Data.EndsWith(">cd"))
                {
                    addNext = true;
                    return;
                }
                string command = e.Data;
                if (addNext) // cd called
                {
                    command += ">";
                    addNext  = false;
                }

                command = JSON.Method("ConsoleUpdated", command);
                command = command.Replace("<", "&lt;");
                command = command.Replace(">", "&gt;");
                CometWorker.SendToClient(ClientId, command);
            }
        }