Coder Social home page Coder Social logo

Comments (7)

statianzo avatar statianzo commented on September 13, 2024

What is your existing http server?

from fleck.

conradz avatar conradz commented on September 13, 2024

It's actually a totally custom HTTP server (couldn't find any existing one that fit). It runs in a regular application (e.g. not IIS). It's sort of like Kayak, but Kayak didn't support some of the things I wanted, such as HTTPS.

from fleck.

statianzo avatar statianzo commented on September 13, 2024

Fleck doesn't have this available specifically, because its main interface is through the WebSocketServer which aims to open its own socket. However, the parts would likely be composable enough that it could be used. Basically, what you would need is to do something similar to WebSocketServer.OnClientConnect when you detect that a connection is a WebSocket connection.

connection = new WebSocketConnection(
                clientSocket,
                _config,
                bytes => RequestParser.Parse(bytes, _scheme),
                r => HandlerFactory.BuildHandler(r,
                                                 s => connection.OnMessage(s),
                                                 connection.Close,
                                                 b => connection.OnBinary(b)));

Create a new WebSocketConnection object passing it the socket, an initialize action for setting up the callbacks, a function that returns a WebSocketHttpRequest object, and a function for building handlers using that request. Similar to this:

connection = new WebSocketConnection(
                new SocketWrapper(existingSocket), //Your socket wrapped with SocketWrapper
                _config, //An initializer like the one from WebSocketServer.Start()
                bytes => mapToRequest(myrequestobject), //mapToRequest maps your request to a Fleck WebSocketHttpRequest
                r => HandlerFactory.BuildHandler(r,
                                                 s => connection.OnMessage(s),
                                                 connection.Close,
                                                 b => connection.OnBinary(b))); //Keep this the same

Hope that helps.

from fleck.

conradz avatar conradz commented on September 13, 2024

@statianzo I tried this approach and it seems to be almost working. The only problem is that the WebSocketConnection will wait to receive some data from the socket before calling the request parser handler.

Maybe you could change it to try the request parser even with empty data, and if that fails wait for more data. It already has the code in it to wait for more data, the only addition would be to try the request parser immediately with no data.

I also thought about buffering the HTTP data in my server, and then making a fake ISocket object that would "replay" the buffered data back for the WebSocketConnection.

from fleck.

statianzo avatar statianzo commented on September 13, 2024

Try calling WebSocketConnection's, CreateHandler. It has the behavior that will take care of creating a handler and sending the return handshake. I know it's private at the moment, but do it through reflection and if that's all it takes to make it work, I wouldn't have a problem making it public.

from fleck.

conradz avatar conradz commented on September 13, 2024

OK, I did it! See this gist for the complete code. Making CreateHandler a public function should help.

from fleck.

statianzo avatar statianzo commented on September 13, 2024

That's great. Good work. I'll get the change in to make CreateHandler public soon.

from fleck.

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.