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

📄 chipchatapplet.java

📁 无刷新功能的聊天室工具 java实现,本人课程设计的作业,附源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}
		return src;
	}

	/*
	 *
	 * Functions that will be called by outside script.
	 *
	 */

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

		URL url;
		try {
			url = new URL(getDocumentBase(), "communicator.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();

			// Acknowledge Thread...
			Thread t2 = new Thread() {
				public void run() {
					try {
						while (sock != null) {
							if (sock.isClosed()) {
								break;
							}
							outStream.write(("ACK:\r\n").getBytes());
							try {
								Thread.sleep(9500);
							} catch (InterruptedException e) {
								logMsg("Error.", e);
								break;
							}
						}
					} catch (IOException e) {
						logMsg("Error.", e);
					}
				}
			};
			t2.setDaemon(true);
			t2.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Send message.
	 * @param who
	 *   Id number that will be received this message. '-1' if it send for all.
	 * @param msg Message.
	 */
	public void sendMsg(final String who, final String msg) {
		try {
			// Check of punishment.
			if (keepQuietTime != 0) {
				long diff = System.currentTimeMillis() - keepQuietTime;
				if (diff < 60000) {
					wincall(
						"chipchat_beQuit",
						new Object[] { new Long(60 - diff / 1000)});
					return;
				} else {
					keepQuietTime = 0;
				}
			}

			// Change to htmlSpecialChars
			String newMsg = htmlSpecialChars(msg);
			int to = Integer.parseInt(who);

			// Send Message...
			if (to == -1) {
				sendToServer("MSG:" + newMsg + "\r\n");
				return;
			} else {
				sendToServer("WSP:" + to + ":" + newMsg + "\r\n");
			}
		} catch (Exception e1) {
			logMsg("Error in sendMsg(..).", e1);
		}
	}

	/**
	 * Send custom message.
	 * @param who
	 *   Id number that will be received this message. '-1' if it send for all.
	 * @param cmd Command name.
	 * @param msg Message.
	 */
	public void sendCustomMsg(
		final String who,
		final String cmd,
		final String msg) {
		try {
			// Check of punishment.
			if (keepQuietTime != 0) {
				long diff = System.currentTimeMillis() - keepQuietTime;
				if (diff < 60000) {
					wincall(
						"chipchat_beQuit",
						new Object[] { new Long(60 - diff / 1000)});
					return;
				} else {
					keepQuietTime = 0;
				}
			}

			// Change to htmlSpecialChars
			String newCmd = htmlSpecialChars(cmd);
			int to = Integer.parseInt(who);

			// Send Message...
			sendToServer("CUSTOM:" + to + ":" + newCmd + ">" + msg + "\r\n");
		} catch (Exception e1) {
			logMsg("Error in sendMsg(..).", e1);
		}
	}

	/**
	 * Send message that 'I will get out."
	 */
	public void getOut() {
		try {
			sendToServer("GETOUT:\r\n");
			closeConnect();
			wincall("chipchat_getout_forward", null);
		} catch (Exception e) {
			logMsg("Error in getout().", e);
		}
	}

	/**
	 * Make someone to keep quiet. It is administrator's funtion.
	 * @param to Id number that will be received this message.
	 *   '-1' if it send for all.
	 */
	public void keepQuiet(final String to) {
		try {
			int num = Integer.parseInt(to);
			sendToServer("KEEPQUIET:" + num + "\r\n");
		} catch (Exception e) {
			logMsg("Error in keepQuiet(.).", e);
		}
	}

	/**
	 * Kick someone out. It is administrator's funtion.
	 * @param to
	 *   Id number that will be received this message. '-1' if it send for all.
	 */
	public void kickOut(final String to) {
		try {
			int num = Integer.parseInt(to);
			sendToServer("KICKOUT:" + num + "\r\n");
		} catch (Exception e) {
			logMsg("Error in kickOut(.).", e);
		}
	}

	/**
	 * Entrust someone with administrator role. It is administrator's funtion.
	 * @param to
	 *   Id number that will be received this message. '-1' if it send for all.
	 */
	public void entrust(final String to) {
		try {
			int num = Integer.parseInt(to);
			sendToServer("ENTRUST:" + num + "\r\n");
		} catch (Exception e) {
			logMsg("Error in entrust(.).", e);
		}
	}

	public void changeRoomPassword(final String to) {
		sendToServer("CHGPASSWORD:" + to + "\r\n");
	}

	public void changeRoomName(final String to) {
		sendToServer("CHGROOMNAME:" + to + "\r\n");
	}

	public void changeMax(final String to) {
		sendToServer("CHGMAX:" + to + "\r\n");
	}

	/**
	 * Getter of conected.
	 * @return is connected?
	 */
	public boolean isConnected() {
		return connected;
	}

	/**
	 * Check whether user is host or not.
	 * @return Is host?
	 */
	public boolean isHost() {
		return userid == hostid;
	}

	public static class SessionKeeper extends Thread {
		ChipChatApplet parent;
		int minute;

		public SessionKeeper(ChipChatApplet parent, int minute) {
			this.parent = parent;
			this.minute = minute;
		}

		public void run() {
			URL url;
			try {
				url = new URL(parent.getDocumentBase(), "sessionkeeper.jsp");
			} catch (MalformedURLException e) {
				parent.logMsg("Error SessionKeeper.run(.).", e);
				return;
			}

			int port;
			{
				// make port value...
				port = url.getPort();
				if (port < 0) {
					port = 80;
				}
			}

			String cookie = (String) parent.getDoc().getMember("cookie");
			if (cookie == null) {
				parent.logMsg("Warm : cookie is null.");
			}

			String sContent =
				"GET "
					+ url.getFile()
					+ " HTTP/1.1\r\n"
					+ "Accept: */*\r\n"
					+ "Accept-Language: utf-8\r\n"
					+ "User-Agent: ChipChat agent.\r\n"
					+ ((cookie != null) ? ("Cookie: " + cookie + "\r\n") : "")
					+ "Host: "
					+ ((port == 80) ? url.getHost() : url.getHost() + ":" + port)
					+ "\r\n"
					+ "Connection: Keep-Alive\r\n"
					+ "Cache-Control: no-cache\r\n"
					+ "\r\n";

			while (true) {
				try {
					Thread.sleep(minute * 60000);
				} catch (InterruptedException e) {
					parent.logMsg("Error..", e);
					break;
				}
				try {
					// Conect to server..
					parent.logMsg("Connecting sessionkeeper.jsp page..");
					Socket sock = new Socket(url.getHost(), port);
					BufferedReader inStream =
						new BufferedReader(
							new InputStreamReader(sock.getInputStream()));
					DataOutputStream outStream =
						new DataOutputStream(sock.getOutputStream());
					// Send message like when browser upoad file.
					outStream.write(sContent.getBytes());
					outStream.flush();
					for (String l = inStream.readLine();
						l.length() > 0;
						l = inStream.readLine());
					outStream.close();
					inStream.close();
					sock.close();
				} catch (IOException e) {
					parent.logMsg("Error..", e);
					break;
				}
			}
		}
	}
}

⌨️ 快捷键说明

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