Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:58:53+00:00 2026-05-26T22:58:53+00:00

I’m trying to set up a websocket server in node.js but having problems. I

  • 0

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:

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);

Everything works fine as it connects, and users.length is updated when that happens and when someone disconnects.

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”:

<Buffer 81 85 71 dc c1 02 19 b9 ad 6e 1e>
??q??☻↓??n▲
<Buffer 81 85 8e 8f 0f a2 e6 ea 63 ce e1>
????☼???c??

I’m sending this “hello” with Chrome 16 using:
myWebSocket.send(“hello”); where myWebSocket is the WebSocket object.

So how do I read and write messages to the socket with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:58:53+00:00Added an answer on May 26, 2026 at 10:58 pm

    Note that after the handshake, the data is framed with 2 or more header bytes at the beginning of each frame. Also, note that payload sent from the client (browser) to the server is masked using a simple 4-byte running XOR mask.

    The framing definition is defined in section 5 of the spec

    Instead of implementing your own WebSocket server in Node you might consider using a higher level abstraction like Socket.IO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.