te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How to send old messages with Websockets - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send old messages with Websockets - Stack Overflow

programmeradmin5浏览0评论

I've got a working Websockets example, where clients receive messages from the server.

I'm not sure how I should send old messages to clients when they connect.

Example:

  • Each client supplies their name when they connect
  • The server responds with "[name] just connected" (to all clients)
  • Any new clients would NOT get these messages

I'm wondering if there's any way clients can receive old messages (either all of them, or messages in the last 5 minutes would be acceptable).

I suspect I may have to capture this information myself, store it somewhere (like a database) and send the messages to new clients myself. Is that right, or am I missing something?

If anyone has pseudo code, or a link to an example of how others have implemented this, that would be handy.

I've got a working Websockets example, where clients receive messages from the server.

I'm not sure how I should send old messages to clients when they connect.

Example:

  • Each client supplies their name when they connect
  • The server responds with "[name] just connected" (to all clients)
  • Any new clients would NOT get these messages

I'm wondering if there's any way clients can receive old messages (either all of them, or messages in the last 5 minutes would be acceptable).

I suspect I may have to capture this information myself, store it somewhere (like a database) and send the messages to new clients myself. Is that right, or am I missing something?

If anyone has pseudo code, or a link to an example of how others have implemented this, that would be handy.

Share Improve this question edited Sep 26, 2014 at 12:17 Tom Doyle 1823 silver badges15 bronze badges asked Jun 28, 2013 at 7:53 Drew KhouryDrew Khoury 1,3908 silver badges21 bronze badges 4
  • Isn't it as simple as the server sends the list of currently connected clients to any new client? – JeffRSon Commented Jun 28, 2013 at 7:57
  • @JeffRSon I want to be able to send any arbitrary piece of data. – Drew Khoury Commented Jun 28, 2013 at 8:06
  • I'm not sure whether it makes sense to send outdated messages. Let's assume a client had connected and disconnected. Will you send both messages to any new client? Or will you know, that a disconnected message has to "delete" a previous connected message? If it's not for logging you should only send current state. And for logging you have to keep the whole bunch of messages in order to send it - however only once of course. – JeffRSon Commented Jun 28, 2013 at 8:32
  • would this be possible here: stackoverflow./questions/26056688/… – Tom Doyle Commented Sep 26, 2014 at 11:37
Add a ment  | 

2 Answers 2

Reset to default 8 +50

You could do something like this:

  1. Each message should have an id -> muid (Message Unique ID)
  2. Each time a client send s a message, it gets an ACK from the server along with the muid for the sent message.
  3. Each time a new message is received in the server side, a muid is assigned, sent with the ACK and also sent with the message to every connected user. This way the view will be able to present, for every user, the same sequence at some point in the time.
  4. Each time a new user connects it sends the last muid it has received so the server knows where this user stopped receiving messages. The server could then send as many old messages as you want, depending on the kind of storage you implement:
    1. Full history: I would remend a database storage with proper indexing
    2. Last N messages: Depending on the size of N you could simply store the last N messages in a fixed size Array and send them, all or the needed chunk, on each reconnection. Keep in mind that this will consume memory so, storing last 1024 messages for 1024 different chats would eat quite a bit of memory, specially if messages are of unlimited size.

Hope it helps

You will have to capture it by your own and store it on server... once user connects you will have to name that data to all connected clients and the messages which you have stored back to the user who has connected. So, you will have to code to broadcast the data to users

By the way what are you using server side? (Node, Erlang , etc)

You can check following link if you are using node.js

http://martinsikora./nodejs-and-websocket-simple-chat-tutorial

发布评论

评论列表(0)

  1. 暂无评论