Note that there are some explanatory texts on larger screens.

plurals
  1. POChrome 16 websocket binary
    text
    copied!<p>I'm trying to set up a websocket server in node.js but having problems. I found a bit code here on stackoverflow and heres the servercode I have now:</p> <pre><code>var net = require("net"), crypto = require("crypto"), users = []; net.createServer(function(socket) { this.name = "Anonymous"; users.push(socket); socket.on('data', function(buffer) { if(buffer.toString('utf-8').substring(0, 14) === "GET / HTTP/1.1") { this.securyPattern = /Sec-WebSocket-Key: (.*)/g; this.key = this.securyPattern.exec(buffer); this.magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; this.sha1 = crypto.createHash("sha1"); this.sha1.update(this.key[1] + this.magic); this.accept = this.sha1.digest("base64"); socket.write("HTTP/1.1 101 Switching Protocols\r\nUpgrade: WebSocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + this.accept + "\r\n\r\n"); } else { console.log(buffer); console.log(buffer.toString('utf-8')); } }); socket.on('end', function() { users.splice(users.indexOf(socket), 1); }); }).listen(1337); </code></pre> <p>Everything works fine as it connects, and users.length is updated when that happens and when someone disconnects.</p> <p>The problem is that I dont know how to read messages except the header (which is plain text), so the lines that I have to print the buffer and buffer.toString('utf-8') only prints something binary different all the time, example for the word "hello":</p> <pre><code>&lt;Buffer 81 85 71 dc c1 02 19 b9 ad 6e 1e&gt; ??q??☻↓??n▲ &lt;Buffer 81 85 8e 8f 0f a2 e6 ea 63 ce e1&gt; ????☼???c?? </code></pre> <p>I'm sending this "hello" with Chrome 16 using: myWebSocket.send("hello"); where myWebSocket is the WebSocket object.</p> <p>So how do I read and write messages to the socket with this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload