Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary messages give TypeError: Invalid non-string/buffer chunk #523

Closed
kamstrup opened this issue Jun 30, 2015 · 4 comments
Closed

Binary messages give TypeError: Invalid non-string/buffer chunk #523

kamstrup opened this issue Jun 30, 2015 · 4 comments

Comments

@kamstrup
Copy link

I am unable to get any of the examples with binary messages working. Here's my example project:

index.js

var http = require('http')
  , express = require('express')
  , app = express()
  , WebSocket = require('ws')
;

app.use(express.static(__dirname + '/public'));

var server = http.createServer(app);
server.listen(8080);

var wss = new WebSocket.Server({ server: server })

wss.on('connection', function(sock) {
    console.log("CONN: " + sock);
    sock.on('error', function(err) { console.log("CONNERR: " + sock + " - " + err); });
    sock.on('close', function() { console.log("CONNECLS " + sock); });
    sock.on('message', function(msg) { console.log("msg: %s", msg); });

    var binData = new Float32Array(4);
    binData[0] = 0.0;
    binData[1] = 1.1;
    binData[2] = 2.2;
    binData[3] = 3.3;
    sock.send(binData,
              {binary: true, mask: false},
              function(err) { if (err) console.log("sock send err: " + err); })
})

public/index.html

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>WS TEST</title>
    <script>
      var ws = new WebSocket('ws://localhost:8080');
      ws.onmessage = function(event) {
        console.log("MSG: " + event)
      };
    </script>
</head>
<body>
WS TEST
</body>
</html>

Terminal session:

$ node index.js
CONN: [object Object]
sock send err: TypeError: Invalid non-string/buffer chunk

package.json:

{
  "name": "wshacks",
  "version": "1.0.0",
  "description": "hack around with ws",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.13.0",
    "ws": "^0.7.2"
  }
}

Node version 0.12.5

@kamstrup
Copy link
Author

Stack trace from the error:

TypeError: Invalid non-string/buffer chunk
    at validChunk (_stream_writable.js:186:14)
    at DeflateRaw.Writable.write (_stream_writable.js:215:12)
    at PerMessageDeflate.compress (/Users/kamstrup/wshacks/node_modules/ws/lib/PerMessageDeflate.js:260:17)
    at Sender.applyExtensions (/Users/kamstrup/wshacks/node_modules/ws/lib/Sender.js:270:54)
    at /Users/kamstrup/wshacks/node_modules/ws/lib/Sender.js:116:10
    at Sender.flush (/Users/kamstrup/wshacks/node_modules/ws/lib/Sender.js:256:3)
    at Sender.send (/Users/kamstrup/wshacks/node_modules/ws/lib/Sender.js:126:8)
    at WebSocket.send (/Users/kamstrup/wshacks/node_modules/ws/lib/WebSocket.js:262:18)
    at WebSocketServer.<anonymous> (/Users/kamstrup/wshacks/index.js:25:10)
    at WebSocketServer.emit (events.js:107:17)

@kamstrup
Copy link
Author

Adding perMessageDeflate: false to the server's constructor options seems to make everything work.

@3rd-Eden
Copy link
Member

Marking as a bug, thanks for your report. @nkzawa Any idea on why this is happening?

@nkzawa
Copy link
Contributor

nkzawa commented Jun 30, 2015

Umm, It seems you can't send TypedArray when perMessageDeflate is enabled. Sorry, I will fix the issue. You can use Buffer instead of TypedArray for the meanwhile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants