babl-ws / babl

Low-latency WebSocket Server
https://babl.ws
Apache License 2.0
68 stars 22 forks source link

How to create websocket server endpoints? #113

Open ccnlui opened 2 years ago

ccnlui commented 2 years ago

How would you implement a server endpoint?

For example, client would connect to ws://localhost:8080/myendpoint, instead of just ws://localhost:8080.

I assumed I'd have to implement this function myself inside onSessionConnected, but the Session object doesn't seem to provide any information on the client's request, I was hoping to be able to get the request's path/endpoint/query etc...

How can this be done?

Thank you.

epickrram commented 2 years ago

Hi @ccnlui babl will accept web-socket connections to any URI on the port. So your client should be able to connect to

ws://localhost:8080/myendpoint

or indeed

ws://localhost:8080/anything_else_you_like

however, the URI part of the request is not parsed or surfaced by the library in any way.

Header parsing is done in KeyDecoder, which would be a reasonable place to detect/assign the request URI:

https://github.com/babl-ws/babl/blob/b09f52f10ae90e562b70b9983fee55e87803659e/src/main/java/com/aitusoftware/babl/websocket/KeyDecoder.java#L59

I would consider a PR that added the request URI to the session object.

ccnlui commented 1 year ago

Here's my attempt: https://github.com/babl-ws/babl/pull/122 I'd appreciate any feeback.