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

implement sending a message for WS and add a websocketFrame class #30

Merged
merged 1 commit into from
Dec 4, 2022
Merged

Conversation

jodevsa
Copy link

@jodevsa jodevsa commented Dec 4, 2022

nodejs#932

can be tested with:

const { WebSocket } = require('./lib/websocket/websocket.js')

const ws = new WebSocket('wss://echo.websocket.events/')

ws.addEventListener('message', (event) => {
  console.log('received:', event.data) // "echo.websocket.events sponsored by Lob.com"
})

ws.addEventListener('open', () => {
  let i = 0
  setInterval(() => {
    const text = `hello. This is message number #${i}`
    console.log('sending:', text)
    ws.send(text)
    i++
  }, 1000)
})

Screenshot 2022-12-04 at 01 14 07

@jodevsa jodevsa mentioned this pull request Dec 4, 2022
@KhafraDev KhafraDev merged commit 6a6b1b3 into KhafraDev:ws Dec 4, 2022
@KhafraDev
Copy link
Owner

KhafraDev commented Dec 4, 2022

This is great, thank you so much for working on this!

We are so much closer to finishing this now - this was one of the last hurdles that needed to be completed!

const rsv2 = chunk[0] & 0x20
const rsv3 = chunk[0] & 0x10

const frame = WebsocketFrame.from(chunk)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind that there is no guarantee that chunk is a complete frame. It might be just the first few bytes of a frame, a slice in the middle of a frame, the tail of frame and the beginning of another, or multiple frames.

Copy link
Author

@jodevsa jodevsa Dec 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lpinca, Thank you for your feedback. I was under the impression that any websocket server will flush the socket buffer after each complete frame and we'll always get a chunk with a full frame.

Do you know an easy way to spin up a ws server where I can test these cases?
1- buffer with incomplete frame
2- buffer with multiple frames
3. etc..

Copy link

@lpinca lpinca Dec 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression that any websocket server will flush the socket buffer after each complete frame and we'll always get a chunk with a full frame.

This is outside of the control of any server. It depends on the OS, the protocol (TCP), the network, etc. When you call socket.write(data), there is no guarantee that the other peer receives all the data as a single TCP packet.

Do you know an easy way to spin up a ws server where I can test these cases?

It is easier to do it without a real server. Take a look at https://github.com/websockets/ws/blob/8.11.0/lib/receiver.js and https://github.com/websockets/ws/blob/8.11.0/test/receiver.test.js.

Keep also in mind that the WHATWG spec advertises support for the permessage-deflate extension, so if the server you are connecting to also supports permessage-deflate, you might receive frames whose payload data is compressed. If decompression is slower than incoming data, you also have to handle backpressure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @lpinca

@lpinca lpinca mentioned this pull request Dec 8, 2022
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants