如何通知推到一个在线的Web应用程序的用户? [英] How to push a notification to an online web application user?

查看:117
本文介绍了如何通知推到一个在线的Web应用程序的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个大学项目,我必须做出一个包含邀请选择与接受/拒绝只有注册用户之间的聊天。如果邀请被接受,则打开聊天否则将消息发送到发送方。

I'm working on a college project where I have to made a chat that contains invitation options with ACCEPT/REJECT between registered users only. If an invitation is accepted, then open the chat otherwise send a message to the sender.

我已经完成了,除了这个邀请选项整个项目。我知道,我可以使用 COOKIE 这(与的setInterval ),但我需要的东西更安全的想法做到这一点

I've completed the whole project except this invitation option. I know that I can use COOKIE for this (with setInterval), but I need something more secure idea to do this.

在我当前的PHP 邀请函数(例如:邀请发件人:a@a.com,邀请:b@b.com )我用来验证 b@b.com 用户(注册与否),并发送回JSON核查。如果用户注册,然后打开自我端的客舱只( a@a.com ),并可以发送邮件到 b@b.com 。该 b@b.com 用户,当它打开,邀请客舱只得到这些消息发信人:b@b.com为:一@ a.com

In my current PHP invite function, (for example: invitation sender: a@a.com, invitation for: b@b.com) I used to verify the b@b.com user (registered or not) and send back the JSON with verification. If user is registered, then open a chatbox on self-end only (a@a.com) and can send message to b@b.com. The b@b.com user will only gets these message when it opens the chatbox with invitation sender : b@b.com, for: a@a.com.

我不知道如何直接发送邀请 b@b.com 与选项,并取回点击事件(接受/拒绝)来`a@a.com。

I have no idea to how to send an invitation directly to b@b.com with option and get back the clicked event (accept/reject) to `a@a.com.

目前没有在我的脑海里一个念头:饼干的setInterval 。但有这种想法的缺点是,这两个用户页面不断地检查该Cookie,这意味着带宽和更多的工艺在用户端和最重要的是,它没有获得更多的负荷。 而且我已经用户3 的setInterval 功能为我的项目:状态检查,新封邮件检查和聊天选项

Currently there is a single idea in my mind: cookie with setInterval. But there is a demerit of this idea is, both users page continuously checks the cookie, it means more load on bandwidth and more process on user-end and most of all, it's not secure. And I'm already user 3 setInterval functions into my project : status check, new msgs check and chat options.

那么,有谁能够提出一个更好的办法?

So, can anybody suggest a better approach?

推荐答案

这是一个相当宽泛的问题,但我会尽力提供一些基本的设计指导。我希望用户在进入他们的电子邮件地址和其他人的电子邮件地址,然后点击一个登录按钮。

This is rather a broad question, but I'll try to offer some basic design guidance. I expect that a user will enter their email address and the email address of the other person, and click on a "log on" button.

接下来会发生什么取决于你的设计,但是这是我会做什么。这涉及到AJAX操作,这是罚款为目的。

What happens next depends on your design, but this is what I would do. This involves AJAX operations, which is fine for the purpose.

  1. 的细节提交给服务器,并创建一个行中的用户表。用户默认获得一个普通的会话cookie,那么你只需要在session_start 在每一页的开始。在谈话表中的一行还将创建。
  2. 我们将假设用户正处于连接,但没有聊天状态。
  3. 使用jQuery或类似,调用定期对服务器,看看从他们身边申请状态需要更新。到与开始,这将决定其他用户是否可用。比方说,这个调用是每10秒,以避免过大的负载,并且每次调用时的时间,用户表与一个 last_seen_at 时间戳更新。
  4. 您的服务器检查现场对话行并认为,如果双方都在线上运行脚本。如果他们是,谈话没有被标记为开始,然后更新行,然后通知用户在接下来的请求更新。
  5. 当您的浏览器应用程序获得某种形式的通知(在回答一个周期性的AJAX请求),就需要重新绘制它的屏幕。生命周期是:开始 - >日志上 - >等待 - >聊天 - >注销。你需要工作的JavaScript来做到这一点。
  6. 在一些下面我的建议表结构的字段被用来捕捉聊天的状态,这样你的PHP应用程序没有记住的东西本身。例如,如果 conversation.accepted_by_user_id 为空,谈话正在等待第二个用户接受聊天请求。如果 message.received_at 填充但 message.sent_at 为空,则意味着消息需要发送到第二用户当他们下一次发送更新请求。
  7. 如果用户或谈话变得太老,可将它们标记为失效或全部删除。
  1. The details are submitted to the server, and a row in a user table is created. The user gets an ordinary session cookie by default, so you just need session_start at the start of each page. A row in a conversations table is also created.
  2. We'll assume that the user is now in a connected but not chatting state.
  3. Using jQuery or similar, a call is made periodically to the server to see if the application status from their side needs updating. To start with, this will determine whether the other user is available. Let's say this call is made every 10 seconds, to avoid excessive load, and every time a call is made, the user table is updated with a last_seen_at timestamp.
  4. You run a script on your server that examines live conversation rows and sees if both parties are online. If they are, and the conversation is not marked as started, then update the row and then notify the users when they next request an update.
  5. When your browser application receives a notification of some kind (in the reply to a periodic AJAX request) it will need to redraw its screen. The life-cycle is: start -> log-on -> waiting -> chat -> log-off. You'll need to work on the JavaScript to do this.
  6. Some of the fields in my suggested table structure below are used to capture the state of the chat, so your PHP application doesn't have to remember things itself. For example if conversation.accepted_by_user_id is null, the conversation is waiting for the second user to accept the chat request. If message.received_at is populated but message.sent_at is null, it means the message needs to be transmitted to the second user when they next send an update request.
  7. Where users or conversations get too old, they can either be marked as stale or deleted entirely.

附注:我们没有试图以检查用户拥有他们所指定的电子邮件地址,或者它们是否存在一样。但是,在大多数情况下,它并不重要 - 我们需要的是对双方都有一个唯一的字符串,它们可以同时记住

Side note: we have made no attempt to check that the users own the email addresses they have specified, or whether they even exist. But, for the most part, it doesn't matter - all we need is for the two parties to have a unique string that they can both remember.

建议表:

user (
    id (pk) int,
    email varchar, // user's email address
    email_to varchar, // the email address of who they wish to chat to
    session_id varchar,
    last_seen_at timestamp
)
conversation (
    id (pk) int,
    started_by_user_id int, // foreign key
    accepted_by_user_id int, // foreign key (null if not yet accepted)
    from_notified_at timestamp,
    to_notified_at timestamp // timestamp (null if not yet accepted)
)
message (
    id (pk),
    conversation_id int, // foreign key
    is_forward boolean, // true = a->b, false = b->a
    message_text varchar, // actual text of message
    received_at timestamp, // when it was sent in a server message
    sent_at timestamp // when it was picked up in a server message
)

您担心Cookie的安全性。如果您使用会话cookie这意味着,只有启动会话可以使用PHP给它的会话ID的用户(也有一些例外:一个Cookie通过脚本注入盗窃被恶意的第三方或捕获通过窃听的数据,您可以使用SSL和阅读关于XSS如果你担心这些事情,但我会说这是超越了大学课程的范围)。

You were worried about the security of cookies. If you use session cookies it means that only the user that initiates a session can use the session ID given to it by PHP (there are a couple of exceptions: the theft of a cookie by a malicious third party via script injection or the capture of data by eavesdropping. You can use SSL and read up on XSS if you are worried about those things, but I would say it is beyond the scope of a college course).

长话短说,你选择了一个不平凡的项目!祝你好运吧,如果有什么你不理解,把它分解成更小的部分,直到作品是每个编程问题。

Long story short, you've chosen a non-trivial project! Good luck with it, and if there is something you don't understand, break it down into a smaller pieces, until the pieces are each programming problems.

现在,您的可以的做这个项目用的插座,但是你应该学会在开始运行前走。我建议你​​这样,先试试它(因为它是更容易,但还是够硬),然后,如果你能做到这一点,就转移到插座。

Now, you can do this project with sockets, but you should learn to walk before you start running. I would suggest you try it this way first (since it is easier but still hard enough) and then if you can do that, move on to sockets.

这篇关于如何通知推到一个在线的Web应用程序的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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