在html5服务器发送事件中,为什么我在fiddler上看到轮询日志
我无法弄清楚为什么我会在 fiddler 中看到我尝试使用 HTML 5 实现的服务器发送事件的轮询日志。请在下面找到我的 fiddler 日志的图像
如果其服务器发送了事件,则应从服务器发送消息,而无需系统一次又一次地调用该方法来查找更新。或者我没有正确实施它。这是我的代码:
<script type="text/javascript">
$(function () {
var userStatusChangeEvent = new EventSource("/events");
userStatusChangeEvent.onmessage = function (event) {
data = $.parseJSON(event.data);
for (index = 0; index < data.length; index++) {
$elem = $('#' + data[index].Id);
isOnline = data[index].IsOnline;
if (isOnline) {
$elem.addClass('is_online');
$('.loginTimestamp', $elem).html(data[index].Time);
}
else {
$elem.removeClass('is_online');
$('.loginTimestamp', $elem).html('');
}
}
};
});
</script>
public virtual ActionResult Events()
{
var userStream = new UserServerSentStatusResult();
userStream.ChangeUserStatus = new LoggedUsersViewModel().Users.Where(x => x.HasChanged).ToList();
userStream.Content = () =>
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(userStream.ChangeUserStatus);
};
userStream.ChangeUserStatus.ForEach(x =>
{
x.HasChanged = false;
x.Time = DateTime.Now.ToShortTimeString();
});
return userStream;
}
I am not able to figure out that why do I see polling log in fiddler for the server sent event I am trying to implement using HTML 5. Please find the image of my fiddler log below
If its server sent event, the message should be sent from server without system calling the method again and again to look for updates. Or i have not implemented it properly. Here my code:
<script type="text/javascript">
$(function () {
var userStatusChangeEvent = new EventSource("/events");
userStatusChangeEvent.onmessage = function (event) {
data = $.parseJSON(event.data);
for (index = 0; index < data.length; index++) {
$elem = $('#' + data[index].Id);
isOnline = data[index].IsOnline;
if (isOnline) {
$elem.addClass('is_online');
$('.loginTimestamp', $elem).html(data[index].Time);
}
else {
$elem.removeClass('is_online');
$('.loginTimestamp', $elem).html('');
}
}
};
});
</script>
public virtual ActionResult Events()
{
var userStream = new UserServerSentStatusResult();
userStream.ChangeUserStatus = new LoggedUsersViewModel().Users.Where(x => x.HasChanged).ToList();
userStream.Content = () =>
{
var serializer = new JavaScriptSerializer();
return serializer.Serialize(userStream.ChangeUserStatus);
};
userStream.ChangeUserStatus.ForEach(x =>
{
x.HasChanged = false;
x.Time = DateTime.Now.ToShortTimeString();
});
return userStream;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Fiddler 中打开流模式
Turn on the Streaming mode in Fiddler