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 - Implementing permessage-deflate in WebSockets - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Implementing permessage-deflate in WebSockets - Stack Overflow

programmeradmin3浏览0评论

I hava a problem understanding and implementing a permessage-deflate extension in WebSockets.

So far, I have added 'Sec-WebSocket-Extensions: permessage-deflate' inside handshake code. It seems to work all fine.

However when I send a "TEST" message from the server (Node.js) to the Client (JS), it seems that the browser (both Chrome and Firefox) is not depressing the data itself.

How to properly implement data pression and depression using permessage-deflate extension?

Request Header:

GET ws://localhost/ HTTP/1.1
Host: localhost
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: null
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Sec-WebSocket-Key: X3RofjiYbzVR8zUPI5ZI6w==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: Exodus_101

Response Header:

HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
sec-websocket-accept: sFO1Id30BZe63QWcm894hnnb+Pg=
Sec-WebSocket-Protocol: Exodus_101
Sec-WebSocket-Extensions: permessage-deflate

I hava a problem understanding and implementing a permessage-deflate extension in WebSockets.

So far, I have added 'Sec-WebSocket-Extensions: permessage-deflate' inside handshake code. It seems to work all fine.

However when I send a "TEST" message from the server (Node.js) to the Client (JS), it seems that the browser (both Chrome and Firefox) is not depressing the data itself.

How to properly implement data pression and depression using permessage-deflate extension?

Request Header:

GET ws://localhost/ HTTP/1.1
Host: localhost
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: null
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Sec-WebSocket-Key: X3RofjiYbzVR8zUPI5ZI6w==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: Exodus_101

Response Header:

HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
sec-websocket-accept: sFO1Id30BZe63QWcm894hnnb+Pg=
Sec-WebSocket-Protocol: Exodus_101
Sec-WebSocket-Extensions: permessage-deflate
Share Improve this question edited May 20, 2015 at 16:09 user1491657 asked May 20, 2015 at 11:28 user1491657user1491657 1011 gold badge1 silver badge3 bronze badges 5
  • copy and paste the handshake negotiation – vtortola Commented May 20, 2015 at 14:23
  • @vtortola Hi! I just edited the post above. I have included the headers. – user1491657 Commented May 20, 2015 at 16:10
  • Handshake seems to be ok, How are you pressing and depressing? – vtortola Commented May 20, 2015 at 16:27
  • I'm not, I thought the whole pressing and depressing functionality will happen automatically when adding permessage-deflate in to the header. My problem is the understanding of how to implement pressing and depressing. – user1491657 Commented May 20, 2015 at 16:49
  • Humm.. You have to actually enable something in node.js or add an extension for that to happen, not just return a header. – vtortola Commented May 20, 2015 at 17:01
Add a ment  | 

1 Answer 1

Reset to default 10

Both, the server and the client presses the payload data portion of WebSocket data messages on a per-message basis using parameters negotiated during the opening handshake

permessage-deflate header is used in the handshake to indicate whether a connection should use pression.

  1. When a client sends a websocket request, it send's permessage-deflate in the websocket extensions header IF the client browser supports it. The server knows if a client supports pression based on this header.
  2. If the server decides to use pression, it responds with the same header similar to an ACK message. The client after receiving the response decides whether or not to press the data based on the server's response.

Once both the sever and the client decides to use pression, they individually have to press the message using deflate pression technique. i.e You'll have to enable pression on the server by using the "perMessageDeflate" option while creating the websocket server. the ws node module enables this by default. The ws module takes care of the header flags so that you don't need to implicitly set it.

Note: Deflate uses a bination of LZ77 and Huffman coding to press data.The client_max_window_bits; server_max_window_bits=10 header flags are used to set a custom buffer/'sliding window' used by the LZ77 algorithm to decrease memory overhead.

发布评论

评论列表(0)

  1. 暂无评论