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

📄 conferenceapplet.java

📁 基于Java实现的聊天室功能
💻 JAVA
字号:
package conferenceapplet;

/*
 * Created on 2007.12.25.
 */

import java.applet.Applet;
import java.awt.Color;
import java.io.*;
import java.net.*;

import netscape.javascript.*;

/**
 * Conference Applet.
 * @author 刘佳
 */
public final class ConferenceApplet extends Applet {
	/** Is debug mode? */
	private final boolean debug = true;

	/** Socket */
	private Socket sock = null;
	/** Input Stream */
	private BufferedReader inStream = null;
	/** Output Stream */
	private DataOutputStream outStream = null;

	/** win JSObject */
	private JSObject win = null;
	/** doc JSObject */
	private JSObject doc = null;

	/** Is connected? */
	private boolean connected = false;

	/** Id number of user */
	private int userid = -1;
	/** Id number of master */
	private int hostid = -2;

	public String getAppletInfo() {
		return "Conference Applet v1.0\r\nCopyright by 刘佳";
	}

	public void init() {
		logMsg("Init..");
		this.setBackground(Color.white);
	}

	public void destroy() {
		closeConnect();
	}

	private JSObject getWin() {
		if (win == null)
			win = JSObject.getWindow(this);
		return win;
	}

	private JSObject getDoc() {
		if (doc == null)
			doc = (JSObject) getWin().getMember("document");
		return doc;
	}

	void processMessage(final String msg) {
		logMsg("PMSG:" + msg);
		try {
			// Parsing msg....
			int index = msg.indexOf(':');
			String cmd;

			if (index < 0) {
				// ignore.
				logMsg("NOT_CMD:" + msg);
				return;
			} else {
				cmd = msg.substring(0, index);
			}
		} catch (Exception e) {
			System.out.println("## Parsing Error... of [" + msg + "]");
			e.printStackTrace();
		}
	}

	void sendToServer(final String msg) {
		if (connected && outStream != null) {
			try {
				outStream.write((msg).getBytes());
			} catch (IOException e) {
				logMsg("Stream write error.", e);
			}
		} else {
			wincall("chipchat_notconnected", null);
		}
	}

	void closeConnect() {
		connected = false;
		if (sock != null) {
			try {
				sock.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			sock = null;
		}
	}

	String[] spliteString(final String src, final char ch, final int from) {
		int index2 = src.indexOf(ch, from);
		if (index2 < 0) {
			throw new IllegalArgumentException(
				"Input source can not split by '"
					+ ch
					+ "'. ["
					+ src
					+ ":"
					+ from
					+ "]");
		} else {
			return new String[] {
				src.substring(from, index2),
				src.substring(index2 + 1)};
		}
	}

	void wincall(final String function, final Object[] param) {
		Object[] realParam;
		if (param == null) {
			realParam = new Object[0];
		} else {
			realParam = param;
		}
		try {
			getWin().call(function, realParam);
		} catch (JSException e) {
			callAlert(
				"function : "
					+ function
					+ "(arg*"
					+ realParam.length
					+ ") is exist?");
		}
	}

	void callAlert(final String msg) {
		try {
			getWin().call("alert", new String[] { msg });
		} catch (JSException e1) {
			e1.printStackTrace();
		}
	}

	void logMsg(final String msg) {
		if (debug) {
			System.out.println(msg);
		}
	}

	void logMsg(final String msg, final Throwable e) {
		if (debug) {
			System.out.println(msg + "\r\nCause : " + e.getMessage());
			e.printStackTrace();
		}
	}

	public static String htmlSpecialChars(final String src) {
		return htmlSpecialChars(new StringBuffer(src)).toString();
	}

	static StringBuffer htmlSpecialChars(final StringBuffer src) {
		if (src == null) {
			return null;
		}
		int length = src.length();
		StringBuffer result = new StringBuffer();

		char c2 = 'x';

		for (int i = 0; i < length; i++) {
			char c1 = src.charAt(i);
			if (c1 == '<') {
				result.append("&lt;");
			} else if (c1 == '>') {
				result.append("&gt;");
			} else if (c1 == '&') {
				result.append("&amp;");
			} else if (c1 == '"') {
				result.append("&quot;");
			} else if (c1 == '\'') {
				result.append("&#039;");
			} else if (c1 == ' ' && c2 == ' ') {
				result.append("&nbsp;");
			} else {
				result.append(c1);
			}
			c2 = c1;
		}
		return src;
	}

	public void connect() {
		if (connected) {
			return;
		}
		logMsg("Connect to server..."); ///////////////////////

		URL url;
		try {
			url = new URL(getDocumentBase(),"chat.jsp");
		} catch (MalformedURLException e) {
			logMsg("Error in connect(.).", e);
			return;
		}

		int port;
		{
			// make port value...
			String sPort = getParameter("port");
			if (sPort == null) {
				port = url.getPort();
				if (port < 0) {
					port = 80;
				}
			} else {
				try {
					port = Integer.parseInt(sPort);
				} catch (NumberFormatException e) {
					logMsg("Pasing error of 'port'.", e);
					port = 80;
				}
			}
		}

		try {
			logMsg(
				"Host:["
					+ url.getHost()
					+ "],Port:["
					+ port
					+ "], File:["
					+ url.getFile()
					+ "]");

			// Conect to server..
			sock = new Socket(url.getHost(), port);
			inStream =
				new BufferedReader(
					new InputStreamReader(sock.getInputStream()));
			outStream = new DataOutputStream(sock.getOutputStream());

			// Send message like when browser upoad file.
			outStream.write(
				("GET " + url.getFile() + " HTTP/1.1\r\n").getBytes());
			outStream.write("Accept: */*\r\n".getBytes());
			outStream.write("Accept-Language: utf-8\r\n".getBytes());
			outStream.write(
				("Content-Type: multipart/form-data; boundary=---------------------------ASD5345435\r\n")
					.getBytes());
			outStream.write("User-Agent: ChipChat agent.\r\n".getBytes());
			outStream.write(
				("INUM: " + getParameter("inum") + "\r\n").getBytes());
			String cookie = (String) getDoc().getMember("cookie");
			if (cookie != null) {
				outStream.write(("Cookie: " + cookie + "\r\n").getBytes());
				logMsg("Cookie:[" + cookie + "]"); ////////////////////
			} else {
				logMsg("Cookie Not Exist..");
			}
			outStream.write(
				("Host: "
					+ ((port == 80) ? url.getHost() : url.getHost() + ":" + port)
					+ "\r\n")
					.getBytes());
			outStream.write(
				("Content-Length: " + Integer.MAX_VALUE + "\r\n").getBytes());
			outStream.write("Connection: Keep-Alive\r\n".getBytes());
			outStream.write("Cache-Control: no-cache\r\n".getBytes());
			outStream.write("\r\n".getBytes()); 
			outStream.flush();

			// Thread that receive messages.
			Thread t1 = new Thread() {
				public void run() {
					try {
						while (sock != null) {
							String r = inStream.readLine();
							if (r == null) {
								break;
							}
							processMessage(r);
						}
					} catch (IOException e) {
						logMsg("Error..", e);
					} finally {
						if (connected) {
							closeConnect();
							wincall("chipchat_connectionBroken", null);
						}
					}
				}
			};
			t1.setDaemon(true);
			t1.start();

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public boolean isConnected() {
		return connected;
	}

	public boolean isHost() {
		return userid == hostid;
	}
}

⌨️ 快捷键说明

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