首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 互联网 >

java 兑现长链接Comet

2013-07-04 
java 实现长链接Comet?2. 同步消息缓存??4. 通过jsp建立长链接http://localhost:8080/Comet/CometServlet?

java 实现长链接Comet

?

2. 同步消息缓存

?

?

4. 通过jsp建立长链接

http://localhost:8080/Comet/CometServlet?content=&type=wait

?

5. 通过jsp响应长链接

http://localhost:8080/Comet/json/home!SendMsg.do

?

6. 通过java程式模拟http建立长链接

?

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import java.util.ArrayList;import java.util.List;import java.util.Random;import java.util.ResourceBundle;import net.sf.json.JSONObject;public class HttpCometUtils {    // private static String host = "http://localhost:8080/Comet/";    private static String action = "json/home!SendMsg.do?msgType=ALL";    protected static ResourceBundle projectBundle = ResourceBundle            .getBundle("DataBaseServer");    public static Thread CometThread = null;    private static String host = projectBundle.getString("web_server");    public static boolean isLogin = false;    static {        if (!host.endsWith("/")) {            host += "/";        }    }    public HttpCometUtils() {        // host = "http://localhost:8080/Comet/";        if (!host.endsWith("/")) {            host += "/";        }    }    /**     * @param urlString     * @param method     * @param type     * @param msg     * @throws IOException     */    public static void connectServer() {        new Thread() {            @Override            public void run() {                int errorCount = 0;                while (isLogin) {                    if (errorCount >= 10) {                        try {                            Thread.sleep(15000);                        } catch (InterruptedException e) {                            // TODO Auto-generated catch block                            e.printStackTrace();                            break;                        }                    }                    URL url = null;                    HttpURLConnection urlConnection = null;                    try {                        String serverURL = host                                + "CometServlet?content=&type=wait&msgType=lott";                        System.out.println(serverURL);                        url = new URL(serverURL);                        urlConnection = (HttpURLConnection) url                                .openConnection();                        urlConnection.setRequestMethod("GET");                        urlConnection.setDoOutput(true);                        urlConnection.setDoInput(true);                        urlConnection.setUseCaches(false);                        InputStream is = urlConnection.getInputStream();                        byte[] b = new byte[is.available()];                        is.read(b);                        String jsmsg = new String(b, "utf-8");                        System.out.println(jsmsg);                        if(jsmsg.equals("")){                            jsmsg = "{}";                        }                        JSONObject json = JSONObject.fromObject(jsmsg);                        Object obj = null;                        if (json.containsKey("type")) {                            obj = json.get("type");                            if (obj != null && "lott".equals(obj)) {                                json = (JSONObject) json.get("msg");                                if (json.containsKey("chatMsg")) {                                                                        System.out.println(json.get("chatMsg"));                                }                            }                        }                        errorCount = 0;                    } catch (MalformedURLException e) {                        errorCount++;                        // TODO Auto-generated catch block                        e.printStackTrace();                    } catch (IOException e) {                        errorCount++;                        // TODO Auto-generated catch block                        e.printStackTrace();                    } catch (Exception e) {                        errorCount++;                        e.printStackTrace();                    } finally {                        if (urlConnection != null) {                            urlConnection.disconnect();                        }                    }                }            }        }.start();    }    public static void main(String[] args) {        int x = new Random(System.currentTimeMillis()).nextInt(100);        HttpCometUtils.isLogin = true;        HttpCometUtils.connectServer();    }}

?

7. 通过java程式模拟http响应长链接

?

import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLEncoder;import java.util.Random;import java.util.ResourceBundle;public class HttpUtils {    // private static String host = "http://localhost:8080/Comet/";    private static String action = "json/home!SendMsg.do?msgType=ALL";    protected static ResourceBundle projectBundle = ResourceBundle            .getBundle("DataBaseServer");    private static String host = "http://localhost:8080/Comet";//projectBundle.getString("web_server");    static {        if (!host.endsWith("/")) {            host += "/";        }    }    public HttpUtils() {        if (!host.endsWith("/")) {            host += "/";        }    }    /**     * @param urlString     * @param method     * @param type     * @param msg     * @throws IOException     */    public static void send(final String type, final String asynMsg) {        new Thread() {            @Override            public void run() {                URL url = null;                HttpURLConnection urlConnection = null;                try {                    StringBuffer param = new StringBuffer();                    param.append("&type=").append(type);                    param.append("&asynMsg=").append(URLEncoder.encode(asynMsg, "UTF-8"));                    String urlString = param.toString();                                        String serverURL = host + action + urlString;                    System.out.println(serverURL);                    url = new URL(serverURL);                                        urlConnection = (HttpURLConnection) url.openConnection();                    urlConnection.setRequestMethod("GET");                    // urlConnection.setDoOutput(true);                    // urlConnection.setDoInput(true);                    urlConnection.setUseCaches(false);                    System.out.println(urlConnection.getResponseMessage());                } catch (MalformedURLException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (IOException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                } catch (Exception e) {                    e.printStackTrace();                } finally {                    if (urlConnection != null) {                        urlConnection.disconnect();                    }                }            }        }.start();    }    public static void main(String[] args) {        int x = new Random(System.currentTimeMillis()).nextInt(100);        HttpUtils.send("chat", "恭喜您中奖了.");    }}

?

热点排行