Giter VIP home page Giter VIP logo

Comments (8)

valyala avatar valyala commented on July 25, 2024 1

@leavengood , I mentioned your library in README.md.

from fasthttp.

leavengood avatar leavengood commented on July 25, 2024

I have a rough version working now. The gorilla Upgrade method is pretty tied in with how net/http does hijacking (which I must admit is pretty hard to use as alluded to in the README for fasthttp), and it was hard to even try to refactor it for fasthttp. So for now I ended up just adding an exported NewServerConn function to gorilla that takes a *net.Conn and just calls the existing newConn to create a *websocket.Conn. I then made my own fasthttp.Handler which does all the necessary checks of the HTTP request for websockets, calls ctx.Hijack() and returns the Switching Protocols status code.

This works fine with my simple JavaScript websockets code.

I still think this would be better if done in gorilla, so I will try adding that next. I made just have to add a new FastHTTPUpgrader struct or something like that, since Upgrader is pretty tied to net/http. The good news is that websocket.Conn doesn't depend on net/http at all, and it does most of the hard work for websockets.

I don't yet know if the gorilla project will be open to adding support for fasthttp though, even if I do all the work.

from fasthttp.

valyala avatar valyala commented on July 25, 2024

If anyone is working on this, or if there are other ideas on how to get websockets working with fasthttp, please reply.

@leavengood , there are plans to add websockets support to fasthttp, but currently I'm busy with other stuff. So it would be great if somebody implements websockets on top of fasthttp.

from fasthttp.

leavengood avatar leavengood commented on July 25, 2024

I have implemented a first attempt at supporting fasthttp in the gorilla websocket package. It can be seen in my fork:

leavengood/websocket@a24f11b

A lot of the changes are just making various utility functions usable for both net/http and fasthttp, but the "meat" of it is here:

leavengood/websocket@a24f11b#diff-34c6b408d72845d076d47126c29948d1R222

I have copied the style of the normal Upgrader a bit, and it works, but I don't know how much it fits in with the style of fasthttp. Here is an example of what using it would look like:

handler := func(c *websocket.Conn) {
    // Do something with the connection
}
upgrader := &websocket.FastHTTPUpgrader{
    Handler:     handler,
    // Don't check the origin header
    CheckOrigin: func(ctx *fasthttp.RequestCtx) bool { return true },
}
if err := fasthttp.ListenAndServe(":12345", upgrader.UpgradeHandler); err != nil {
    log.Fatalf("Error in ListenAndServe: %s", err)
}

Let me know what you think of this, and I can see about adding some tests and then making a PR to the main gorilla websocket repo.

from fasthttp.

leavengood avatar leavengood commented on July 25, 2024

I made a PR and have made some adjustments to account for fasthttp needing Go 1.4 or above:

gorilla/websocket#98

from fasthttp.

valyala avatar valyala commented on July 25, 2024

@leavengood , gorilla/websocket#98 looks cool! It would be great if it will be merged eventually into gorilla websockets.

from fasthttp.

leavengood avatar leavengood commented on July 25, 2024

It may need some more tweaking but they seem to be open to the idea. Worse case I could maintain a fork with fasthttp support. Their project seems pretty stable with a few commits now and then, so it wouldn't be hard to keep a fork up-to-date. For the moment if other people need this they can use my fork, though at the moment the new code is in a fasthttp branch.

from fasthttp.

leavengood avatar leavengood commented on July 25, 2024

Gorilla websockets does not currently want to merge in my changes, so I have closed the PR and merged that code into the master branch of my fork (https://github.com/leavengood/websocket). If anyone wishes to use websockets with fasthttp, they can import my fork. I'm going to close this issue as well.

from fasthttp.

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.