服务器端每隔2秒不断的响应数据,客户端不间断的得到数据。怎么实现呀。各位帮帮忙啊

解决方案 »

  1.   

    这样的话,你需要客户端不停地向服务器端发送请求,用ajax可以实现的了,加上javascript的计时器,可以实现这样的功能
    我之前做过一个这样的项目,就是跟你的要求一样的。
    用ajax+JavaScript实现对话功能。
      

  2.   

    刚写的Ajax聊天功能,和你的需求差不多,每隔800毫秒向服务器发送请求!页面代码如下:
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript">
    var xmlHttpRequest;

    //创建xml对象
    function createXmlHttpRequest(){
    if(window.XMLHttpRequest){
    xmlHttpRequest = new XMLHttpRequest;
    } else if(window.ActiveXObject){
     xmlHttpRequest = new ActiveXObject("MSXML2.XMLHTTP.3.0");
    }
    if(!xmlHttpRequest){
    alert("create XMLHttpRequest Object fail.");
    }
    }

    //回调函数
    function callback(){
    var oldValue = document.getElementById("chatArea").value;
    if(xmlHttpRequest.readyState==4){
    if(xmlHttpRequest.status==200){
    //处理逻辑
    document.getElementById("chatArea").value = xmlHttpRequest.responseText;

    }else{
    alert("服务器"+xmlHttpRequest.status+"错误");
    }
    }
    }

    //和服务器建立连接发送请求
    function sendRequest(){
    var context = document.getElementById("context").value;
    createXmlHttpRequest();
    xmlHttpRequest.onreadystatechange=callback;
    xmlHttpRequest.open("post","<%=path%>/chat.do",true);
    xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttpRequest.send("context="+context);
    }


    //定时向服务器发送空消息
    function sendEmptyRequest(){
    var context = document.getElementById("context").value;
    createXmlHttpRequest();
    xmlHttpRequest.onreadystatechange=callback;
    xmlHttpRequest.open("post","<%=path%>/chat.do",true);
    xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    xmlHttpRequest.send(null);
    setTimeout("sendEmptyRequest()",800);
    }
    </script>
      </head>
      
      <body onload="sendEmptyRequest()">
         <textarea name="chatArea" id="chatArea"  rows="30" cols="100" readonly="readonly" ></textarea>
         <p>
         <input type="text" size="80" name="context" id="context"><input type="button" value="发送" onclick="sendRequest()">
        
      </body>
    </html>
      

  3.   

    线程?那就一直连接,一直读取数据,一直发送数据好了,没必要每次都连接,接收,断开连接,太浪费了。数据是发送方控制的,可以用定时器向OutputStream输出数据,也可以每个循环都Thread.sleep(2000),休眠2秒。如果真的很固定,建议同定时器。Timer和TimerTask详解
      

  4.   

    你这个客户端和服务器端是个说明样子的啊
    b/s吗要是的话ajax+js就可以
    不过我还没有用
    现在用的是dwr应该也可以
    这个还好整点
    呵呵
    然后用js的定时任务
      

  5.   

    客户端其实对于我来说就是手机端,而服务器端其实就是j2ee端,webserver端,手机端不停的从服务器得到数据,而webserver每隔2秒发送数据。