Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access client hostname, http headers etc from a Java websocket server?

I've followed many tutorials and sample sample code but I am yet to see a way to access client's HTTP header, hostname etc like the way we can in Servlet's request object.

How do I go about it?

Let's say I've onOpen defined as-

@OnOpen
    public void onOpen(Session session) {

} 

In above method, is there a way I can access underlying HTTP connection details using session field? I am okay even if I can get to underlying Servlet (if there is any)

like image 562
gauravphoenix Avatar asked Jan 28 '15 09:01

gauravphoenix


People also ask

Can WebSockets have headers?

Warning: The server can't send more than one Sec-Websocket-Protocol header. If the server doesn't want to use any subprotocol, it shouldn't send any Sec-WebSocket-Protocol header. Sending a blank header is incorrect.

How do I connect to WebSocket server?

All you have to do is call the WebSocket constructor and pass in the URL of your server. // Create a new WebSocket. var socket = new WebSocket('ws://echo.websocket.org'); Once the connection has been established, the open event will be fired on your Web Socket instance.

What is WebSocket client Java?

WebSocket is a two-way communication protocol that lets clients send and receive messages over a single connection to a server endpoint. Download a PDF of this article. WebSocket is a two-way communication protocol that lets clients send and receive messages over a single connection to a server endpoint.

What is Ping and Pong in WebSocket?

Ping, pong, and close messages are control frames which may be sent at any time by either peer on an established WebSocket connection. They are sent using the functions ping , pong . and close .


1 Answers

see chapter 4.1.1.5 at Tyrus user guide. It requires some amount of work to get the info from ServerEnpointConfig.Configurator to endpoint instance, but it can be done. (see ModifyRequestResponseHeadersTest.java)

like image 54
Pavel Bucek Avatar answered Nov 15 '22 00:11

Pavel Bucek