将Html5 SSE发送给特定客户端 [英] Sending Html5 SSE to specific clients

查看:78
本文介绍了将Html5 SSE发送给特定客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将服务器事件(推送通知)发送给特定用户(基于他们的用户名)任何想法怎么做?

i want to send the server event(push notificaton) to specific users(based on their usernames) any idea how to do it?

推荐答案

HTTP工作在断开模式。服务器不记得它的客户端。



你应该从客户端编写脚本来实现服务器轮询



http://api.jquery.com/category/ajax/ [ ^ ]



http://stackoverflow.com/questions/3583203/server-polling-with-javascript [ ^ ]



http://stackoverflow.com/questions/1155245/javascript-poll-server-will-这导致堆栈溢出 [ ^ ]
HTTP works in disconnected mode. The server does not remember its clients.

You should implement server polling from client side scripting

http://api.jquery.com/category/ajax/[^]

http://stackoverflow.com/questions/3583203/server-polling-with-javascript[^]

http://stackoverflow.com/questions/1155245/javascript-poll-server-will-this-cause-a-stack-overflow[^]


SSE广播到所有打开的连接。在JSON流中包含您的组,并在javascript中进行组检查。



注意:这不是一种安全的方法,不要以这种方式使用敏感数据,因为每个人都收到每条消息,它只会被你的javascript以不同的方式处理。



例如:



服务器端C#

SSE broadcasts to all open connections. Include your group in your JSON stream and do the group check in the javascript.

Note : This is not a secure method, do not use sensitive data this way, since everyone gets every single message, it''ll only be handled by your javascript differently.

Example :

Server-Side C#
string JSON = "data: {'IntendedFor' = 'AdminClerks', 'Message' = 'You\'ve got a server event'}";
Response.Write(JSON+'\n\n');
Response.Flush();





客户端HTML



Client HTML

<input id='myGroup' type='hidden' value='AdminClerk' />





客户端Javascript



Client Javascript

var myGroup = document.GetElementById('myGroup').value;
var source = new EventSource('<insert url="" here="">');
source.onmessage = function (e) {
  var myJSONResponse = JSON.parse(e.data);
  if (myJSONResponse.IntendedFor == myGroup)
    alert("You've got a server event");
}


这篇关于将Html5 SSE发送给特定客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆