Skip to content

websocket.Conn is so close to net.Conn, but not quite #441

@joonas-fi

Description

@joonas-fi

I had a requirement where I just wanted to pipe bytes in both directions over HTTP.

I bridged the gap by making a super small adapter: https://github.com/function61/holepunch-server/blob/master/pkg/wsconnadapter/wsconnadapter.go
With that adapter the WebSocket now implements net.Conn so I can host SSH servers on it etc.

I know we can't make Conn an io.Reader and io.Writer.. well we could but we probably shouldn't, as it would make the API confusing with Write() and Read() operating on bytes, while NextReader() and NextWriter() take messageType..

But, as you saw from my adapter, the difference is so small, but I spent quite some time coding it having to learn more about WebSockets than I wanted, because I had to learn how gorilla/websocket works and why does it work like that (probably because WebSockets don't support streaming a frame with unbounded length, but instead you have to implements streaming by splitting the stream yourself into multiple frames or something like that).

Anyways, the difference is so small, do you guys think we could merge that adapter into module like gorilla/websocket/wsconnadapter, and mention this somewhere in the docs because I don't think my use case is too exotic and I would love more people benefit by not having to go through the same process I did.

Of course the design of my adapter might not be perfect.. perhaps it could be made more composable by writing an adapter where we get a true unbounded io.Writer and io.Reader where we could pipe unbounded amount of data, and those adapters could be used to make net.Conn compatible interfaces and even more.

Sorry if I'm rambling .. I'm a bit tired. :)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions