0

i need help about to create a valid nodejs function that recieve an string or json and return data in the next format;

  0                   1                   2                   3
  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 +-+-+-+-+-------+-+-------------+-------------------------------+
 |F|R|R|R| opcode|M| Payload len |    Extended payload length    |
 |I|S|S|S|  (4)  |A|     (7)     |             (16/64)           |
 |N|V|V|V|       |S|             |   (if payload len==126/127)   |
 | |1|2|3|       |K|             |                               |
 +-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
 |     Extended payload length continued, if payload len == 127  |
 + - - - - - - - - - - - - - - - +-------------------------------+
 |                               |Masking-key, if MASK set to 1  |
 +-------------------------------+-------------------------------+
 | Masking-key (continued)       |          Payload Data         |
 +-------------------------------- - - - - - - - - - - - - - - - +
 :                     Payload Data continued ...                :
 + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
 |                     Payload Data continued ...                |
 +---------------------------------------------------------------+

I did an native function for implement the upgrade websocket connection to stablish handshake and it's works. decode messages on server also work fine, but when i need send some message does not reach, i think that i don't compose a correct format data.

I want to avoid external libraries as "websocket", "socket.io", "ws" etc.. I need a native function and understand how to send little and bigger messages in this protocol from server.

Very thank's for your help

O.Palm
  • 162
  • 1
  • 3
  • 15
  • 2
    Why are you avoiding proven open source libraries that have already been written to do exactly this? FYI, you can also just go look at the source code for these open source libraries and see how they do it. And, how in the world do you expect us to help you debug your code when you don't show us your code? – jfriend00 Feb 12 '17 at 19:33
  • Hi, because i need a simple function for understand the system, not an complex and full library. While the people see this post and try to help me, i search and i try with another snippets. – O.Palm Feb 13 '17 at 12:30
  • 1
    Getting a proven library that implements the websocket protocol will be your simplest option. Don't know why you think that's complex. – jfriend00 Feb 13 '17 at 16:00
  • I understand you, But isn't what I need. Please, can you understand the subject of this post? – O.Palm Feb 13 '17 at 20:46
  • 1
    Nobody can help you fix code that you do not show us. If you have a specific problem in your code, then post that code and explain exactly what the problem is. As it stands now, it appears you're just asking us to write a bunch of code for you. That's not what we do here. We help you fix your code and can only do that when you've tried and then share your code and explain exactly what problem you have with your code. Questions about code MUST include your code. – jfriend00 Feb 13 '17 at 23:13
  • I think you don't understand me. Please, can you read the last phrases inside description of this post? – O.Palm Feb 14 '17 at 09:54
  • 1
    I did read what you said multiple times. You say "I need a native function" (for sending messages). We don't just supply code here. That's not what we do. We help you fix your own code. I will not be responding any more. It appears to me that your question is off-topic here and not appropriate for stack overflow, but you do not seem to understand that. – jfriend00 Feb 14 '17 at 09:57
  • i agree, and... can you continue reading, please? – O.Palm Feb 14 '17 at 09:58
  • 1
    If you want to understand how to send different size messages, then go read the webSocket specification. It is all described in detail there. We aren't going to rewrite all that here. If you have a specific question about a specific part of the specification that you don't understand, then you can ask a SPECIFIC question about a specific piece of that specification. Or, you can go grab any one of 20 different open source implementations and study their code. What you are asking is not what we do here. We don't start from scratch and teach you a specification. – jfriend00 Feb 14 '17 at 10:00
  • Can you see the frame inside description of the post? There, i don't know about compose the structure for distinct type and size of data. – O.Palm Feb 14 '17 at 10:13
  • https://tools.ietf.org/html/rfc6455#section-5.2 and http://stackoverflow.com/questions/8125507/how-can-i-send-and-receive-websocket-messages-on-the-server-side – jfriend00 Feb 14 '17 at 10:30
  • Thank's by the info, we can do it. if i understand correctly.. i understand that is necessary read the total lenght of the message, create an buffer on dependency by message size and convert all to string format, finally send data. is correct? i can think other question.. for bigger files, is necessary cut the message and send chunk formated messages? – O.Palm Feb 14 '17 at 18:36

1 Answers1

1

I found a way to get a code that seems work for little messages;

    var socket = this; // Or your socket "conn"
    var message = '.....'; // Or your string or json that you need send
    var bufferLength = (message.length > 125) ? 4 : 2;
    var preSend = Buffer.alloc(bufferLength);
    preSend[0] = 0x81;
    if (message.length > 125) {
        preSend[1] = 126;
        var length = message.length;
        preSend[2] = length >> 8;
        preSend[3] = length & 0xFF;
    }
    else {
        preSend[1] = message.length;
    }
    socket.write(preSend, 'binary');
    socket.write(message, 'utf8');

But, although this code seem work, i don't know if is valid for the websocket standard for little and bigger messages. Can somebody help me?

O.Palm
  • 162
  • 1
  • 3
  • 15