per-message deflate compression fixes

This commit is contained in:
Benjamin Sergeant 2018-11-13 17:46:05 -08:00
parent e847716076
commit 54da891f79
4 changed files with 43 additions and 13 deletions

View File

@ -1,11 +1,11 @@
/*
* cmd_websocket_chat.cpp
* broadcast-server.js
* Author: Benjamin Sergeant
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
*/
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
const wss = new WebSocket.Server({ port: 8080, perMessageDeflate: true });
// Broadcast to all.
wss.broadcast = function broadcast(data) {

View File

@ -0,0 +1,28 @@
#!/usr/bin/env python
import os
import asyncio
import websockets
connections = set()
async def echo(websocket, path):
connections.add(websocket)
try:
async for message in websocket:
print(message)
for ws in connections:
if ws != websocket:
await ws.send(message)
except:
raise
finally:
connections.remove(websocket)
asyncio.get_event_loop().run_until_complete(
websockets.serve(echo, 'localhost', 8080))
asyncio.get_event_loop().run_forever()

View File

@ -14,7 +14,7 @@ namespace ix
{
public:
WebSocketPerMessageDeflateOptions(
bool enabled = true,
bool enabled = false,
bool clientNoContextTakeover = false,
bool serverNoContextTakeover = false,
uint8_t clientMaxWindowBits = kDefaultClientMaxWindowBits,
@ -33,14 +33,14 @@ namespace ix
static bool startsWith(const std::string& str, const std::string& start);
static std::string removeSpaces(const std::string& str);
static uint8_t const kDefaultClientMaxWindowBits;
static uint8_t const kDefaultServerMaxWindowBits;
private:
bool _enabled;
bool _clientNoContextTakeover;
bool _serverNoContextTakeover;
int _clientMaxWindowBits;
int _serverMaxWindowBits;
static uint8_t const kDefaultClientMaxWindowBits;
static uint8_t const kDefaultServerMaxWindowBits;
};
}

View File

@ -578,7 +578,8 @@ namespace ix
_rxbuf.begin()+ws.header_size + (size_t) ws.N);
// Reply back right away
sendData(wsheader_type::PONG, pingData, _enablePerMessageDeflate);
bool compress = false;
sendData(wsheader_type::PONG, pingData, compress);
emitMessage(PING, pingData, ws, onMessageCallback);
}
@ -627,15 +628,15 @@ namespace ix
const wsheader_type& ws,
const OnMessageCallback& onMessageCallback)
{
// ws.rsv1 means the message is compressed
std::string decompressedMessage;
size_t wireSize = message.size();
// When the RSV1 bit is 1 it means the message is compressed
if (_enablePerMessageDeflate && ws.rsv1)
{
std::string decompressedMessage;
if (_perMessageDeflate.decompress(message, decompressedMessage))
{
onMessageCallback(decompressedMessage, decompressedMessage.size(),
messageKind);
onMessageCallback(decompressedMessage, wireSize, messageKind);
}
else
{
@ -644,7 +645,7 @@ namespace ix
}
else
{
onMessageCallback(message, message.size(), messageKind);
onMessageCallback(message, wireSize, messageKind);
}
}
@ -753,7 +754,8 @@ namespace ix
WebSocketSendInfo WebSocketTransport::sendPing(const std::string& message)
{
return sendData(wsheader_type::PING, message, _enablePerMessageDeflate);
bool compress = false;
return sendData(wsheader_type::PING, message, compress);
}
WebSocketSendInfo WebSocketTransport::sendBinary(const std::string& message)