ktorio / ktor

Framework for quickly creating connected applications in Kotlin with minimal effort
https://ktor.io
Apache License 2.0
12.85k stars 1.04k forks source link

fragmented websockets messages #1088

Open waldemarmeier opened 5 years ago

waldemarmeier commented 5 years ago

Ktor Version

1.1.4

Ktor Engine Used(client or server and name)

CIO

JVM Version, Operating System and Relevant Context

Windows, JVM 8

Feedback

Hello,

I want to use to the ktor-client to send binary audio data through a websocket connection to a proprietary webservice. The documenation of this service says: "Audio is expected to be delivered at a steady rate, in reasonably sized chunks (20ms to 1s ,multiples of 20 ms recommended) in order for results to be delivered in a timely fashion"

When I cut my audio data in chunks and try to send it with the ws-client in multiple binary frames, I get the following error message: "data frames after the initial data frame must have opcode 0".

As far as I've seen, there is no way to change the opcode of a Frame, because it is set in the in the FrameType enum, so I am kind of stuck.

Is there a way to: 1) manually change the opcode of the following frames to 0 2) or some other way to send the data in chunks?

Btw, the code looks something like this:

         chunkedAudio.forEachIndexed { index, byteChunk ->
                    val frame = Frame.Binary(lastInex== index, ByteBuffer.wrap(byteChunk) )
                    send(frame)
                }
cy6erGn0m commented 5 years ago

Most likely we need to fix WebSocketWriter/Serializer to put zero optcode automatically so one should never put zero opcode to a frame

cy6erGn0m commented 5 years ago

Also it looks like it is impossible to provide fragmented frames support in JS

waldemarmeier commented 5 years ago

Thanks for the answer. So currently, it is not possible ? Before I started implementing my client, I compared several client frameworks and I liked yours the most. Hope you can add support support for fragmented messages!

oleg-larshin commented 4 years ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

JasonMing commented 3 years ago

maxFrameSize option is only available on incoming messages. No user API can set the continuation opcode among continuous frames. DefaultWebSocketSession also will not split the big frames.

There is no any workaround we can do...