I have a websocket server deployed on localhost in the user PC. Some website in foo.domain.com is calling the websocket this way: wss://localhost/sign, the websocket server processes the information and returns the data to the website. Everything is working fine, but I want to know if I can read the URL of the website who is calling the websocket. I want to obtain foo.domain.com in the websocket server. Right know I read a variable session.getRequestURI().getHost() and I always get 127.0.0.1, I think this is because the website code is running locally in Javascript in the same machine of the user and where is installed the websocket server. The websocket server is developed with Java and this is part of my code.
package com.foo.service;
import org.eclipse.jetty.websocket.core.exception.WebSocketTimeoutException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.swing.*;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CountDownLatch;
@ServerEndpoint(value = "/sign", decoders = SignatureRequestDecoder.class, encoders = SignatureResponseEncoder.class)
public class SignEndpoint {
// Some variables here
@OnMessage
public void onSignSocketRequest(Session session, SignatureRequest request) throws IOException, EncodeException {
logger.info("Signature request received.");
String test = session.getRequestURI().getHost();
String test1 = session.getRequestURI().getPath();
String test2 = session.getRequestURI().getFragment();
String test3 = session.getRequestURI().getQuery();
String test4 = session.getRequestURI().getScheme();
logger.info("Test = " + test); // Prints 127.0.0.1
logger.info("Test1 = " + test1); // Prints /sign
logger.info("Test2 = " + test2); // Prints null
logger.info("Test3 = " + test3); // Prints null
logger.info("Test4 = " + test4); // Prints wss
// More code here
}
// More methods here
}
I don’t know if I can obtaint the URL I want from this Session parameter or another way. If the client is a desktop app then can’t be a URL and maybe what I want doesn’t make sense. I don’t know if I can get the URL on the first protocol redirection of the websocket protocol from HTTP to WS.