gorilla / websocket

Package gorilla/websocket is a fast, well-tested and widely used WebSocket implementation for Go.
https://gorilla.github.io
BSD 2-Clause "Simplified" License
21.67k stars 3.46k forks source link

Support WebAssembly / GopherJS #432

Open silbinarywolf opened 5 years ago

silbinarywolf commented 5 years ago

While I understand that the following implementation exists: https://github.com/gopherjs/websocket

It'd be nice if I could write code once for my game project that runs natively and on the web.

If I were to make a PR with support for GopherJS / WebAssembly and assuming the code is all good, would it be accepted?

ghost commented 5 years ago

I am not the owner of the project, but I think think a rough outline of your proposal will be required to answer your question. Some things that the outline might include:

silbinarywolf commented 5 years ago

Thanks for giving me something to think about. I'm not convinced the following is a good idea, but it's currently unclear to me on how we could go about this in a better way.

ghost commented 5 years ago

Regarding headers: Is it possible to write cross platform code that Dials a connection? When running in the browser, cookies, user agent and other headers are handled automatically by the browser. When running outside the browser, the application is responsible for supplying all of these headers.

Once the connection is hand, it does seem that cross platform code is possible.

garyburd commented 5 years ago

Help wanted: Describe the subset of the package API that will work in the browser and describe how that API will be implemented in the browser.

albrow commented 4 years ago

I recently helped add WebAssembly support to pion/webrtc and have an open PR to do the same for goleveldb. I would be interested in building WebAssembly support for this package to and we would use it in libp2p/go-ws-transport and 0xProject/0x-mesh (my project at work).

Is this still something you're interested in and would you accept a PR?

albrow commented 4 years ago

After looking more at the API for gorilla/websocket, I think adding WebAssembly support is harder than I first imagined. The API surface area is large and too much of it would not translate well to the native JavaScript API (this is in contrast to pion/webrtc which closely mimics the JavaScript API and goleveldb where only a small portion of the code related to storage needed to be changed at all). For now, it was much easier for me to add WebAssembly support in the higher-level libp2p/go-ws-transport package. I can still help offer advice or review a PR if someone else wants to take this on.

nhooyr commented 4 years ago

For anyone who needs this feature now, I've implemented it v1.6.3 of my WebSocket library with @albrow's review.

See https://godoc.org/nhooyr.io/websocket#hdr-Wasm