⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 urlchatwatcher.java

📁 java3D game engine design of the source [three-dimensionalvirtualrealitynetworkprogram] - "virtual
💻 JAVA
字号:

// URLChatWatcher.java
// Andrew Davison, June 2003, dandrew@ratree.psu.ac.th

/* A threaded URLChatWatcher object periodically sends a "read" 
   message to the ChatServlet:
      ChatServlet?cmd=read&name=??  + uid cookie

  The response is all the visible messages that have not
  already been read, or "no".

  The messages are displayed in the top-level client's 
  text area by calling its showMsg() method.
*/

import java.io.*;
import java.net.*;


public class URLChatWatcher extends Thread
{
  private static final int SLEEP_TIME = 2000;    // 2 secs between pollings
  private static final String SERVER = "http://localhost:8080/servlet/ChatServlet";

  private URLChat client;
  private String userName;
  private String cookieStr = null;


  public URLChatWatcher(URLChat c, String nm, String cs)
  {  client = c; 
     userName = nm;
     cookieStr = cs;
  }


  public void run()
  // Keep polling forever
  { URL url;
    URLConnection conn;
    BufferedReader br;
    String line, response;
    StringBuffer resp;

    try {
      String readRequest = SERVER + "?cmd=read&name=" +  
							URLEncoder.encode(userName, "UTF-8") ;
      while(true) {
        Thread.sleep(SLEEP_TIME);    
 
        url  = new URL(readRequest);   // send a "read" message
        conn = url.openConnection();
    
        // Set the cookie value to send
        conn.setRequestProperty("Cookie", cookieStr);
    
	    br = new BufferedReader(
              new InputStreamReader( conn.getInputStream() ));	
        resp = new StringBuffer();    // build up the response
        while ((line = br.readLine()) != null) {
          if (!fromClient(line))   // if not from client
            resp.append(line+"\n"); 
        }
        br.close();

        response = resp.toString();
        if ((response != null) && !response.equals("\n"))
          client.showMsg(response);    // show the response
      }
    }
    catch(Exception e)
    { client.showMsg("Servlet Error: watching terminated\n");
      System.out.println(e);  
    }
  } // end of run()


  private boolean fromClient(String line)
  // A line (message) is from a client if it begins with (Name)
  {
    if (line.startsWith("("+userName))
      return true;
    return false;
  }  // end of fromClient()


}  // end of URLChatWatcher class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -