-1

Here is my code in javascript

    <script type="text/javascript">
startingListener();
function startingListener() {
  setInterval(function(){
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "http://localhost:32081/", false);
    xhr.send();           
       var result = xhr.response;
       console.log(result);
     }
  }, 100);
}
</script>

And my server on Java

public static void main (String... args) throws IOException, Exception {
String clientSentence;
     ServerSocket welcomeSocket = new ServerSocket(32081);

     while(true)
     {
        Socket connectionSocket = welcomeSocket.accept();
        BufferedReader inFromClient =
           new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
        PrintWriter outToClient = new PrintWriter(connectionSocket.getOutputStream(), true);

        clientSentence = inFromClient.readLine();
        System.out.println("Received: " + clientSentence);
             System.out.println("begin");   
             outToClient.println("fu");
             System.out.println("Received: done");

     }
}

I just want to receive answer string in JS and work with it further.

So when I run server and run JS, they connected and I received in IDE send() from JS

After that I saw in console of Java that begin and done received, but in browser console I didn't see any answer and script just handled. What do I do wrong?

Thank you for your help

warorc
  • 31
  • 8
  • you get no alerts at all in the browser? do you get any browser developer tools console errors at all? are you sure you want to send 10 SYNCHRONOUS requests per second? is `http://localhost:32081/` the same origin as the web page? seriously, any errors in the browser console? – Jaromanda X Aug 26 '16 at 06:19
  • I do not see how your `server` is creating a Http response that the browser requires. – Scary Wombat Aug 26 '16 at 06:21
  • @ScaryWombat - isn't that what `outToClient.println` would do? (I assumed, I have no clue about java) - although, re-reading the code, I see what you mean ... there's no http in that at all, is there – Jaromanda X Aug 26 '16 at 06:22
  • No erros, nothing. Console just clean. I think is just because I send Synchronous request. – warorc Aug 26 '16 at 06:25
  • No it plain vanilla sockets. No Http Response headers or anything. How can it have a status of 200? – Scary Wombat Aug 26 '16 at 06:25
  • to see this question and answer:http://stackoverflow.com/questions/21607664/read-ajax-post-content-using-java-socket-server – Ying Yi Aug 26 '16 at 06:26
  • see http://stackoverflow.com/a/3732328/2310289 – Scary Wombat Aug 26 '16 at 06:34
  • No, I don't need Http server, I need simple TCP server, where I received string in browser. But seems I have to HTTP server. Thank you. – warorc Aug 26 '16 at 06:46

1 Answers1

0

Your client code is missing a state change handler and using setInterval with 100 ms it is very heavy on the server. Here is a better JS. Your JAVA issue is another problem. Downvoters: Do comment

function listener() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "http://localhost:32081/", false);
  xhr.onreadystatechange = function () {
    if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        console.log(xhr.responseText);
    }
  }
}

listener();

If you want to call listener repeatedly, you can do

function listener() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "http://localhost:32081/", false);
  xhr.onreadystatechange = function () {
    if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        if (xhr.responseText != "done") {
          console.log("server still busy");
          setTimeout(listener,1000); // repeat the call
        }
        else {
          console.log("finally done");
        }
    }
  }
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • Not DV but, where in the server code is any Http being generated? How can it have a status of 200? – Scary Wombat Aug 26 '16 at 06:28
  • That is another question. I only handled the JS :) – mplungjan Aug 26 '16 at 06:29
  • thank you, but I really interested in server side. Cause JS side was written not by me and works perfectly for C# code, which I can't see. – warorc Aug 26 '16 at 06:31
  • Sure. But you have two problems. I fixed one – mplungjan Aug 26 '16 at 06:32
  • What's with the downvote? Comment if you have something to say. OP will not see any result unless the readystatechange handler is set up – mplungjan Aug 26 '16 at 06:35
  • I can't upvote and downvote comments. My reputation less than 15. So it's not me. I tried to upvote your comment. – warorc Aug 26 '16 at 06:39
  • I know. It is possibly because you are asking two questions - why does my server not send http response and why does my browser not show it. I only answered one – mplungjan Aug 26 '16 at 06:40