我想在我的网站上实时显示所有在线用户。虽然不知道如何去做。在新用户登录后添加新用户并不难,但是我还需要删除不再登录的用户。
任何想法如何做到这一点?我应该使用jQuery检查哪些用户已注销并从列表中将其删除吗?
最佳答案
您的问题将是人们在不注销的情况下离开,他们的 session 将仍然存在多长时间,直到您设置了超时时间才可以收集他们的 session 数据(实际上可能更长)
为了获得真正准确的登录者和访问站点的计数,您将需要每个客户端每隔几秒钟或几分钟向服务器发送一次“心跳”。在每个心跳触发器上,您都希望使在指定时间内未 checkin 的所有用户过期。
心跳信号可能最好至少由用户名和时间戳组成,但可以包含您要跟踪的任何信息。
当客户端向服务器发送单曲时,它会检查该用户名的现有记录,如果记录已经存在,则覆盖时间戳信息,否则添加新记录。后记会删除所有尚未 checkin 的用户条目。最好使信号比到期更频繁地发生。每30秒发出一次信号,每分钟清理一次。
(编辑后,改变了主意,最好按相同的顺序而不是分别地进行所有操作)
然后从 Activity 表返回当前登录的用户,这就像SELECT * FROM table一样容易,因为该表始终是干净的。
客户端:
这是客户端库的一个简单示例,用于处理触发心跳功能并捕获结果。
//Client-side parent variable declaration (simulated namespacing)
var Client = Client || {};
Client.Pulse = null; //Holds a pointer to the heartbeat timer (id)
/* If you needed to cancel the heartbeat processes you could
* pass this variable to clearTimeout(). Calling Client.Heartbeat()
* again would start the cycle back up.
*/
//Initial event that will kick off the chain when the DOM is ready
$(document).ready(function(){
Client.Heartbeat(); //Initial call to the Heartbeat function
});//ready
/// Sends the heartbeat signal and retrieves the currently active user list
Client.Heartbeat = function(){
var sUsername = 'SomeUser';
/* Note: If you have an active session running on the server it would not be
* necessary to send the username since you could pull it on the backend
* script which would be more tamper-proof anyway. I'm just giving an
* example of sending data to the server using the jQuery.ajax method
* If you were storing the username to be sent from the client; this wouldn't
* be a good place to do it anyway
*
* Remove the "data : {...}" line below to exclude sending information to the server
* The "type : 'post'" line would not be necessary either
*/
$.ajax({ //Send the signal and recieve the information about active users back
url : '/lib/server/Heartbeat.php',
type : 'post',
dataType : 'json',
data : {Username : sUsername },
success : function(jActiveUsers,sStatus,jqXHR){
/* This is the callback function of the request, jActiveUsers will be the
* json object with the information you choose to send back to it
*/
Client.RenderActiveUsers(jActiveUsers); //Call the display function
//Trigger the next delayed call to Client.Heartbeat
Client.Pulse = setTimeout(function(){
Client.Heartbeat();
},30000); //30 second delay before next trigger
}//success
});//$.ajax
}//Heartbeat
/// Processes the results sent by the server and draws them to the UI
Client.RenderActiveUsers = function(jActiveUsers){
/* This is where you would get a handle whatever element(s) on the page
* will be displaying the information about currently active users
* and filling it with the list of users how ever you would wish to display it.
*/
}//RenderActiveUsers
setTimeout()调用以重新开始该过程,所以如果您使用setInterval()且服务器花费的时间比预期的长,则这是处理间隔的一种更干净的方式,您会看到客户开始自我竞争。在成功的回调中使用setTimeout()还可让您在服务器端心跳处理器停止工作的情况下优雅地失败。客户端也将这样做,而不是继续尝试失败(如果您希望它继续尝试,您只需在失败的响应上添加一个重新触发)。I apologize I am not familiar with Java as a back-end service, I will be making some assumptions based on how PHP works; so I cannot guarantee it will map directly to your environment. Database examples will assume MySQL. code examples will be pseudo-cody PHP(ish)
ActiveUsers
-----------------------------------------
| Field | Type | NULL | Key |
-----------------------------------------
| Id | int | | PRI |
| Username | varchar(xx) | | |
| LastPulse | datetime | | |
| FirstPulse | datetime | | |
-----------------------------------------
Sessions,您可以使用它存储有关特定访问者的信息(例如其当前状态)。在PHP中,这是通过在客户端和服务器之间来回传递标识符来实现的,允许服务器识别特定的客户端,并在后端为您存储与该 session 相关的变量(例如,具有当前登录状态的 boolean 值用户名或一个字符串,用于在登录后保留其用户名。)if($LoggedIn && ($Username != null)){ //Session information
SELECT `Id` FROM `ActiveUsers` WHERE `Username` = '{$Username}' LIMIT 1
UPDATE `ActiveUsers` SET `LastPulse` = NOW() WHERE `Id` = {$Id}
INSERT INTO `ActiveUsers` (`Username`,`LastPulse`,`FirstPulse`) VALUES ('{$Username}',NOW(),NOW())
DELETE FROM `ActiveUsers` WHERE `LastPulse` < (NOW() - INTERVAL 2 MINUTE)
ActiveUsers表,以便仅存在 Activity 用户的记录,您现在可以查询获取这些记录,以及您希望从此表和任何其他表链接到的其他信息SELECT `Username`, UNIX_TIMESTAMP(`FirstPulse`) AS `FirstPulse` FROM `ActiveUsers` ORDER BY `FirstPulse` ASC
json_encode()和print() ed以及"application/json" header 值转换为JSON,以便jQuery可以正确处理它。当然,这在Java实现上会有所不同,但是“创建数组,将其转换为JSON字符串并使用指定的适当 header 将其打印出来”的整个过程将是相同的。关于java - 如何使用Ajax显示在线用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7867517/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t