//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); }
//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(); }
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)); }
public void GetServerTime() { string jsonMethod = JSON.Method("UpdateTime", DateTime.Now); CometWorker.SendToClient(_clientId, jsonMethod); CometWorker.SendToAll(jsonMethod); }
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); }
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); }
public void UpdateJoint() { string message = JSON.Method("SharedNumber", number); //Send the above message directly to the joint CometWorker.SendToAll(message); }
static void CometWorker_OnClientCreated(string clientId) { //Client connection is done string message = JSON.Method("UpdateString", "Now, you are connected!"); CometWorker.SendToClient(clientId, message); }
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(); }
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(); }
public void playerFires(int location) { if (otherPlayerId != string.Empty) { CometWorker.SendToClient(otherPlayerId, EXTML.Method("playerShot", location)); } }
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); }
public void Dispose() { lock (Names) { Users.Remove(_clientID); Names.Remove(_username); } RenderMemberList(); CometWorker.SendToAll(JSON.Method("AppendToChat", "<strong>" + _username + " has left the building!</strong>")); }
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)); }
public WCFSample(string clientId) { ClientId = clientId; CometWorker.SendToClient(ClientId, "UpdateServiceStatus(" + ServerConnected.ToString().ToLower() + ");"); if (ServerConnected) { proxy.AddClient(clientId); } }
static void UpdateClients() { while (true) { if (CometWorker.ActiveClientCount > 0) { CometWorker.SendToAll(JSON.Method("UpdateTime", DateTime.Now)); } Thread.Sleep(500); } }
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); } }
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"); }
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); } } }
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); }
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(); } }
//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())); }
public void UnSubscribe() { CometWorker.Groups.UnpinClient(ClientId); string message = ""; if (IsDesktop) { message = EXTML.Method("UnSubscribed"); } else { message = JSON.Method("UnSubscribed"); } CometWorker.SendToClient(ClientId, message); }
//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() + ");"); } }
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); }
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 = ""; } }
/// <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"); }
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(); }
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)); } }
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("<", "<"); command = command.Replace(">", ">"); CometWorker.SendToClient(ClientId, command); } }