How To Send Request Header To Jetty WebSocket From

2019-08-31 08:40发布

问题:

How To Send Request Header To Jetty WebSocket From JavaScript Client ?


What I am doing ?


At Client Side.

  1. Need To add service name and service version in request header at client side
  2. Send to server

At Server Side.

  1. Identify the service from the request header
  2. Using that create corresponding input translator (using GSON)
  3. Create service input using that translator
  4. Call the service.

I can use org.eclipse.jetty.websocket.api.annotations.WebSocket as to get service name and version at server side. But..... How can I set the header information at client side?

@OnWebSocketMessage
public void onText(Session session, String message) {
    if (session.isOpen()) {
        System.out.println(session.getUpgradeRequest().getHeader("service"));
        System.out.println(session.getUpgradeRequest().getHeader("version"));

        System.out.printf("Echoing back message [%s]%n", message);
        session.getRemote().sendString(message, null);
    }
}

Thanks for your support...

回答1:

Unfortunately, you can't. The standard WebSocket JavaScript API does not provide means to customize HTTP headers. See also this document at heroku dev center.