Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get a client socket from rocket? #353

Closed
iddm opened this issue Jul 17, 2017 · 5 comments
Closed

How to get a client socket from rocket? #353

iddm opened this issue Jul 17, 2017 · 5 comments
Labels
question A question (converts to discussion)

Comments

@iddm
Copy link

iddm commented Jul 17, 2017

I need to get a client socket from rocket's handler and to work with it later. Is this possible to do?
Or, I may handle connections with another thread and to pass client's socket requests to the rocket instance.

To clarify my intentions, this is what I need:

  1. 2 kind of clients: X and Y.
  2. X-Client connects to the rocket (server) and performs REST-api requests.
  3. Rocket responses to the requests of X-client but does not close the socket, just leaves it live it's own life.
  4. I remember this X-client socket from the message handler in some hashmap.
  5. Y-client calls another REST-api calls (I have 2 rocket mounts, one for X and one for Y clients).
  6. The message handler sends some data to X-client socket and answers Y-client "the query has been sent successfully, 200".
  7. The X-Client sends the answer on the Y-client's request back to the rocket by calling X-client's rest-api methods.

X -> Rocket <- Y

As from what I think this can be done in 2 different ways:

  1. I handle the X-client connections by myself and then forward them to the rocket somehow.
  2. I leave this to rocket itself and get sockets from there.
@SergioBenitez SergioBenitez added the question A question (converts to discussion) label Jul 21, 2017
@SergioBenitez
Copy link
Member

There's no support for either of these in Rocket. I'm not sure how they would work, actually; Rocket (Hyper, really) needs to handle the client socket directly to properly implement HTTP.

This is a pretty odd thing to want to do. From the sound of it, you'd be better off having the client connect to two different servers. Can you perhaps explain what you're actually trying to do?

@iddm
Copy link
Author

iddm commented Jul 24, 2017

I need to provide a custom logic for sockets but they must also work as simple http client. So, I want to have a persistent connection between a client and the server. This how it should work:

  1. Client connects to http server, says "hello" via http rest api.
  2. Server answers "ok", but does not close the connection.
  3. Another client calls another resp-api endpoind of my rocket server and the rocket server sends http request to the client which has connected at 1. stage.

I could work without http protocol but it is just simple to use protocol and we have a nice implementation (rocket) for it, so it would be nice to use it...

@SergioBenitez
Copy link
Member

This is definitely outside the scope of Rocket, at least for the time being. Something like WebSockets is likely more amenable to solving your problem. You're essentially trying to implement a publish/subscribe and broadcasting mechanism, something that web sockets is perfect for.

Rocket will eventually have support for web sockets out of the box (#90). Until then, you can use one of the available Rust web socket libraries. I've included information about how to use them alongside Rocket in #90 (comment).

@iddm
Copy link
Author

iddm commented Jul 31, 2017

@SergioBenitez thanks for the answer! Close the issue if you think it is needed to be closed.

@iddm
Copy link
Author

iddm commented Jul 25, 2018

Hello again! Has anything been changed since our last discussion? I see there is a rocket::Request::remote() method which returns the SocketAddr:

Returns the address of the remote connection that initiated this request if the address is known. If the address is not known, None is returned.

So I think it would be not very difficult to add a socket_handle() there since we already have something which returns the address of the client socket. It will be helpful also for custom implementations of Connection: keep-alive so that, for example, I could implement my own responder which took &Request as function argument and I could take a socket handle from it and then do something with it.

If you still think that it is very bad to do this in rocket itself, could you give me any hint how I could use rocket and get a socket handle, just any method please, because the framework is very simple to use and useful and I don't want to waste my time on rewriting my server in other frameworks or even from scratch using linux's headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question (converts to discussion)
Projects
None yet
Development

No branches or pull requests

2 participants