Coder Social home page Coder Social logo

Sockets joining two messages. about node HOT 7 OPEN

arreehm avatar arreehm commented on June 27, 2024
Sockets joining two messages.

from node.

Comments (7)

arreehm avatar arreehm commented on June 27, 2024

Do you want repo of whole this process?

from node.

arreehm avatar arreehm commented on June 27, 2024

+"\u0004" at write, and parsing on recieving socket solves the problem, but IMO it should be automatic, so reclassify this as feature request if you wish.

from node.

RedYetiDev avatar RedYetiDev commented on June 27, 2024

Do you want repo of whole this process?

Yes, providing reproduction steps (and code) is the best way for people to see and understand what went wrong

from node.

arreehm avatar arreehm commented on June 27, 2024

https://github.com/arreehm/socketConnector

I don't know if it's expected, but it mostly doesn't happen.

from node.

arreehm avatar arreehm commented on June 27, 2024

Like I understand, if it's sends in a buffer at one time it's two different thing, but I never managed to break the JSON.parse being in on('data', like that, I fixed it with unicode symbol, but anyhow, it feels broken.

from node.

arreehm avatar arreehm commented on June 27, 2024

Now I see, that I should write my own encoding, where there is size of JSON specified and read up buffer up to that length, but it takes away ease of javascript, I expected it to be done by default.

from node.

arreehm avatar arreehm commented on June 27, 2024

Solution I have is to encode/decode JSON String by following thing:

const { Buffer } = require('node:buffer')

const encode = (string, encoding)=>{
    string = Buffer.from(string, encoding)
    let length = string.byteLength
    length = Buffer.from(`<${length}>`)
    let buffer = Buffer.concat([length, string])
    return buffer
}

const decode = (buffer, encoding, perChunk)=>{
    let stop = false
    while(!stop) {
        let index = buffer.indexOf('>')
        let sub = buffer.subarray(1, index)
        let length = Number(sub.toString())
        let ourChunk = buffer.subarray(index+1, length+index+1)
        perChunk(ourChunk.toString())
        buffer = buffer.subarray(length+index+1)
        if(buffer.byteLength===0) stop = true
    }
}

module.exports = (encoding)=>{
    return {
        encode: (string)=>{
            return encode(string, encoding)
        },
        decode: (buffer, perChunk)=>{
            return decode(buffer, encoding, perChunk)
        }
    }
}

from node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.