How to interact with Web server using JavaScript

前提是你 提交于 2020-01-03 17:32:47

问题


I am trying to develop an Interactive chat application using AppWeb open source web Server.
I need to have some machanism that will enable Web server to send updated messages to client, so that when remote usre sends messages that will get updated automaticaly at the client end.

There are some methods to do this using HTML5 Web Sockets and Server sent events.
But we need to implement it in HTML and JavaScript only not HTML5.

So I need some pooling machanism that will keep pooling my Web Server for New events.

So how should I write pooling machanism in Javascript using Sockets.
How it should be implemented at server end?

Thanks!


回答1:


there are already some examples out there... depending on the server-side, you could go for java-hello-world or php-hello-world or ...

if you can't use websocket, you have to can go the old way, create an interval by window.setInterval and pull data from the server with eg. $.ajax(). i don't know any other alternative to bidirectional connection (websocket)... see kayahrs answer

as you've asked for it:
$.ajax() is the jQuery way to do xhr. basically, it fires an asynchronous request to the server, which returns xml or json or text or ... (whatever). when this request comes back, the supported eventHandler gets fired and you can react to the response. you could also use the plain xhr, but it's a bit awkward to handle the original xhr.
jQuery supports some shorthand overloads for $.ajax(), eg. $.getJSON(), $.get(), ...

sample implementation:

$.get("test.cgi", function(data){
    alert("Data Loaded: " + data);
});



回答2:


There is another technique for sending messages from the server to the client. You have to use an iframe for this which connects to a PHP script (Or whatever technique you are using on the server side) which does not close the connection. The PHP script then sends JavaScript messages whenever the client must be informed about something. After each message the server flushes the output stream to enforce that the data really finds its way to the client and is not cached by some output buffer. Here is a small example code of the PHP script loaded in the iframe (not tested and not complete, just to show the basics):

<html>
  <body>
    <script type="text/javascript">

      function receiveMsg(data)
      {
          // Do something with the data, for example send it to some function
          // in the parent frame (Where your chat application lives)
      }

      <?php

      while (true) // You may also implement some abort state which should
                   // be checked here
      {
          $data = waitForData(); // This is your magic function on the server
                                 // which waits for data to be send to the client
          echo "receiveMsg('" . $data . "');"; // Let's say data is just a string.
                                             // You may want to use JSON instead
          flush();
      }
      ?>
    </script>
  </body>
</html>

Advantage of this method is that it doesn't rely on polling. So you don't have to send requests to the server every x seconds. And when you do things right on the server side then messages sent by one user are received as fast as possible by the other users and not x seconds later. Disadvantage is that you have a permanent HTTP connection for each chat user. But this may need less resources on the server then having dozens of complete HTTP requests per minute per chat user.



来源:https://stackoverflow.com/questions/5740226/how-to-interact-with-web-server-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!