在HTML5 SSE和直接的Ajax轮询之间是否存在很大差异(在性能,浏览器实现可用性,服务器负载等方面)?从服务器端看,它似乎EventSource只是每隔约3秒左右点击指定的页面(虽然我知道时间是灵活的).
当然,在客户端设置比设置定时器并且$.get经常使用它更简单,但还有其他什么吗?它会发送更少的标题,还是做其他一些我不知道的魔法?
html5 server-side javascript-events ajax-polling server-sent-events
我正在寻找使用PHP/Javascript(Jquery)实现聊天室,同时具有群聊和私聊功能.
问题是如何以自然的方式不断更新界面,以及如何在私人聊天中显示"X正在输入..."消息.
显而易见的方式似乎是每隔x秒/毫秒javascript ping服务器并在最后一次ping和现在之间获取新消息列表.然而,这可能会使界面看起来有点不自然,如果突然聊天室充斥着5条消息.我希望每条消息都会在输入时显示.
有没有办法使javascript保持与服务器的连续连接,服务器将任何新消息推送到此连接,并且javascript将它们添加到接口,以便它们同时出现,几乎在服务器收到它们之后?
我知道有一些轮询选项需要你安装一些apache模块等,但我很糟糕的系统管理员,因此我更喜欢在共享主机帐户或php上有一个非常容易安装的解决方案/ mysql唯一的解决方案.
我当前正在轮询服务器以检查新数据,然后相应地更新AngularJS应用程序中的模型.他大致正在做的事情:
setInterval(function () {
$http.get('data.json').then(function (result) {
if (result.data.length > 0) {
// if data, update model here
} else {
// nothing has changed, but AngularJS will still start the digest cycle
}
});
}, 5000);
Run Code Online (Sandbox Code Playgroud)
这很好,但大多数请求不会导致任何新的数据或数据更改,但$ http服务并不真正知道/关心,仍然会触发摘要周期.我认为这是不必要的(因为摘要周期是应用程序中最重的操作之一).有没有办法仍然可以使用$ http,但如果没有任何改变,以某种方式跳过摘要?
一种解决方案是不使用$ http而是使用jQuery,然后调用$ apply让Angular知道模型已经改变:
setInterval(function () {
$.get('data.json', function (dataList) {
if (dataList.length > 0) {
// if data, update model
$scope.value = dataList[0].value + ' ' + new Date();
// notify angular manually that the model has changed.
$rootScope.$apply(); …Run Code Online (Sandbox Code Playgroud) 注意: 我更换了我的投票系统,
websockets但我仍然想知道上述问题的答案.
我正在尝试减少传统轮询消息系统的AJAX请求,但我不知道如何获取它:
$chatbox = $("#chatbox");
setInterval(function(){
// I send the sha1 of the chatbox html content to verify changes.
$.post("post.php", {checksum: hex_sha1($chatbox.html())}, function (data, status) {
switch (status) {
case "success":
// If version of "post.php" checksum is different than mine (there are changes) data isn't empty; I assign data as the new content of the chatbox.
if(data){
$chatbox.html(data);
$chatbox.scrollTop($chatbox[0].scrollHeight);
}
break;
default:
$chatbox.html('Connection error...');
break;
}
});
}, 1000);
Run Code Online (Sandbox Code Playgroud)
好吧,如你所见,我使用setInterval()with 1000 …
我正在研究AJAX,因为我正在使用Javascript和PHP制作实时应用程序.它需要能够在不刷新页面的情况下进行更新并实时更新.
我尝试使用setInterval()轮询服务器但是为了快速我必须每秒都有它.它似乎使用了大量的带宽.它确实有效,我有一个计划与我的托管服务提供商'无限带宽'.虽然网站上有很多压力,但我想使用Push技术.
学习如何设置Push非常困难.从我设法弄清楚,你必须托管某种类型的推送服务器或彗星服务器.我也研究过websockets,但你也必须自己主持它.当我没有服务器计算机时,我不明白该怎么做,我的托管服务提供商网站上没有关于此的文档.
特定主机提供商是否为您托管推送服务器?有没有办法获得服务器而不必托管它?这似乎是一个比投票更好的选择,但同时它似乎非常令人困惑.
我有一个依赖于非常"实时"数据的Web应用程序 - 所以如果有变化,它需要每1秒更新一次.
我想知道以下解决方案的优点和缺点是什么.
解决方案1 - 轮询很多
所以每1秒,我向服务器发送一个请求并获取一些数据.一旦我获得了数据,我会等待1秒再重复一遍.如果州改变并采取适当的行动,我会检测到客户端.
解决方案2 - 阻止很多
所以我向服务器发起一个请求,在30秒后会超时.服务器通过每秒检查一次来监视服务器上的数据.如果服务器注意到数据已更改,则会将数据发送回客户端,客户端会采取相应的操作.
脚本
实质上,数据的大小相当小,但是根据实时事件以随机间隔进行更改.问题是,Web UI将在2,000个实例的区域中运行,因此我每秒有2,000个来自UI的请求,或者我有2,000个长时间运行的请求,最多需要30秒?
非常感谢帮助和建议,特别是如果您在类似的卷下使用AJAX请求.
$(document).ready(function() {
(function poll() {
setTimeout(function() {
$.ajax({
url: "/project1/api/getAllUsers",
type: "GET",
success: function(data) {
console.log("polling");
},
dataType: "json",
complete: poll,
timeout: 5000
}), 5000
});
})();
});?
Run Code Online (Sandbox Code Playgroud)
这只是保持执行速度与服务器响应速度一样快,但我希望它只会每5秒轮询一次.有什么建议?
编辑:我应该补充,请求完成后5秒钟会更好.
我正在创建一个Ngrx Angular 2应用程序,并试图让我的http调用在一段时间后继续轮询.我已经看到了该interval()函数的使用,但是在Ngrx的情况下,当内部完成服务调用时@Effect(),它会产生错误.请指教:
@Injectable()
export class TasksEffects {
constructor(
private actions$: Actions,
private TS: TaskService
){}
@Effect()
onLoadTasksLoadTasks$: Observable<Action> = this.actions$.ofType(tasksActions.ActionTypes.LOAD_TASKS)
.switchMap(() => {
return this.TS.index()
.map((res) => new tasksActions.LoadTasksSuccessAction(res.json()))
.catch(err => of(new tasksActions.LoadTasksFailAction(err)));
});
Run Code Online (Sandbox Code Playgroud)
我想每十秒运行一次switchMap功能.这不起作用.
@Effect()
onLoadTasksLoadTasks$: Observable<Action> = this.actions$.ofType(tasksActions.ActionTypes.LOAD_TASKS)
.switchMap(() => {
return this.TS.index()
.map((res) => new tasksActions.LoadTasksSuccessAction(res.json()))
.catch(err => of(new tasksActions.LoadTasksFailAction(err)));
}).interval(10000);
Run Code Online (Sandbox Code Playgroud)
类型错误是:
我们使用简单的Ajax更新在Rails中实现了一个简单的聊天室功能.现在,在每个聊天室中,消息属于特定用户.我们想要显示用户列表(类似用户在场).请提出建议.我们没有使用Jabber,XMPP等.
聊天室模型是:
class ChatRoom < ActiveRecord::Base
validates_presence_of :title
has_many :messages,:foreign_key=> "chat_room_id"
has_many :stories,:foreign_key=>"chat_room_id"
has_many :topics,:foreign_key=>"chat_room_id"
end
Run Code Online (Sandbox Code Playgroud)
消息是每个用户发送的聊天记录.
消息模型是:
class Message < ActiveRecord::Base
belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)
USer模型是:
class User < ActiveRecord::Base
acts_as_authentic :crypto_provider => Authlogic::CryptoProviders::BCrypt
validates_presence_of :nick
validates_uniqueness_of :nick
has_many :questions
end
Run Code Online (Sandbox Code Playgroud)
请提出建议
所以我创建了以下mixin:
var Polling = {
startPolling: function() {
var self = this;
setTimeout(function() {
self.poll();
if (!self.isMounted()) {
return;
}
self._timer = setInterval(self.poll(), 15000);
}, 1000);
},
poll: function() {
if (!this.isMounted()) {
return;
}
var self = this;
console.log('hello');
$.get(this.props.source, function(result) {
if (self.isMounted()) {
self.setState({
error: false,
error_message: '',
users: result
});
}
}).fail(function(response) {
self.setState({
error: true,
error_message: response.statusText
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
注意console.log('hello');的poll功能.根据这个逻辑,我应该每15秒看一次.
现在让我们看一下react组件:
//= require ../../mixins/common/polling.js
//= require ../../mixins/common/state_handler.js
//= require …Run Code Online (Sandbox Code Playgroud) ajax-polling ×10
ajax ×4
javascript ×4
jquery ×3
php ×2
polling ×2
angular ×1
angularjs ×1
chat ×1
comet ×1
html5 ×1
ngrx ×1
ngrx-effects ×1
optimization ×1
reactjs ×1
real-time ×1
reverse-ajax ×1
server-side ×1