简体   繁体   English

维护会话的JavaScript HTTP客户端

[英]JavaScript HTTP Client that maintains session

I have written a little PHP scoreboard which basically monitors a mysql table with realtime sports scores in it. 我写了一个小PHP记分牌,基本上可以监视带有实时体育比赛成绩的mysql表。 Users use it by loading up the page, which then reloads itself every 5 seconds. 用户通过加载页面来使用它,然后页面每5秒重新加载一次。 The last-loaded scoreboard is stored in the session and compared to the current-loaded scoreboard. 最后加载的计分板存储在会话中,并与当前加载的计分板进行比较。 If a player has changed ranking the PHP script adds javascript to the page which shows a red or green highlight effect for that player depending on if they lost or gained rank. 如果玩家改变了排名,PHP脚本会在页面上添加javascript,这会根据玩家失去或获得排名而对该玩家显示红色或绿色的突出显示效果。

What I'm looking for is basically a way to write a JavaScript script I can host on my site that is remote includable on other sites which will essentially maintain a session with my server, loading the scoreboard every 5 seconds and displaying the results, including the highlighting of changed rank players. 我正在寻找的基本上是一种写​​JavaScript脚本的方法,该脚本可以在我的站点上托管,而在其他站点上则是远程包含的,这实际上将与服务器保持会话,每5秒钟加载记分板并显示结果,包括突出显示了排名变化的球员。

Basically I guess I need an HTTP client implemented in JavaScript that can do this. 基本上我想我需要一个用JavaScript实现的HTTP客户端才能做到这一点。 I've Googled around but there's a lot of jargon to parse. 我已经在Google周围搜索了,但是有很多行话可以解析。 Also I would prefer not to use jquery or whatever other library since these may already be present on the remote sites and I don't want to cause conflicts. 我也不想使用jquery或其他任何库,因为这些可能已经存在于远程站点上,并且我不想引起冲突。 A pure javascript solution would be ideal I think. 我认为,纯JavaScript解决方案将是理想的选择。

There are 2 approaches that I can think of when dealing with a scoreboard application: 1) Using PHP + AJAX + MySQL which is great if your application and database are on the same server, and the amount of information returned by the AJAX app is small. 处理记分板应用程序时,我可以想到两种方法:1)使用PHP + AJAX + MySQL,如果您的应用程序和数据库位于同一服务器上,并且AJAX应用程序返回的信息量很小,那将非常有用。

2) Using a websocket app such as socket.io ( http://socket.io/ ) or even ratchet ( http://socketo.me/ ) to update information on the scoreboard ONLY when there is an update. 2)仅当有更新时,才使用socket.io( http://socket.io/ )甚至棘轮( http://socketo.me/ )之类的websocket应用程序更新记分板上的信息。

I personally prefer using the websocket method when dealing with Scoreboards simply because it also allows me to do custom alerts when there is an update. 我个人更喜欢在处理记分板时使用websocket方法,这仅仅是因为它还允许我在有更新时发出自定义警报。

As everyone suggested you should use ajax to retrieve the data from the php server instead of reloading the page. 正如每个人的建议,您应该使用ajax从php服务器检索数据,而不是重新加载页面。 If you don't want to use any library (like jQuery) you could use pure ajax like this example . 如果您不想使用任何库(例如jQuery),则可以使用纯ajax这样的示例 Then use the function setTimeout to call the function each 5 minutes. 然后使用函数setTimeout每5分钟调用一次函数。

But it could be great if you could use jQuery because you already have an ajax function so you can do something like this: 但是如果您可以使用jQuery,那可能很棒,因为您已经有了一个ajax函数,因此可以执行以下操作:

$.ajax({
  type: "POST",
  url: "some.php",
  success:function(msg){
         //you can update the data here
  }),
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM