📄 clientreceivethread.java
字号:
package com.gamvan.club.users.im.client;import com.gamvan.club.users.im.tools.formatdatetime.FormatDateTime;import java.awt.Color;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.net.Socket;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JOptionPane;import javax.swing.JTextField;import com.gamvan.club.users.im.client.personalchat.PersonalChat;import com.gamvan.club.users.im.client.personalchat.mylist.IconListItem;import com.gamvan.club.users.im.client.personalchat.mylist.MyList;import com.gamvan.club.users.im.tools.MyTextPane;/** * @会员的接收线 * by 幽梦还乡 from gamvan 2006/4/4 */public class ClientReceiveThread extends Thread { private JComboBox userOnlineList = null; private MyTextPane receiveMeg = null; private JTextField sendMegTextPane = null; private JButton sendMeg = null; private Socket clientSocket = null; private ObjectInputStream dataIn = null; private ObjectOutputStream dataOut = null; private MessageNote mn = null; private Color color = null; private ImageIcon icon = null; public static String chatMeg = null; public static boolean isStop; private String applyUserName ,userName; private PersonalChat PC = null; private JCheckBox statusCheckBox = null; private JComboBox actionList = null; private MyList jList = null; public static boolean isOver = false; public void run(){ if(!clientSocket.isClosed()&&clientSocket!=null) recevThread(); } public void recevThread(){ while(!isStop && !clientSocket.isClosed()&&clientSocket!=null){ try{ //读取服务器发送过来的聊天信息 String megType = (String)dataIn.readObject(); if(megType.equalsIgnoreCase("系统信息")){//处理系统信息 String systemMeg = (String)dataIn.readObject(); receiveMeg.append(Color.orange,"[系统信息]"); receiveMeg.append(Color.PINK,systemMeg); mn.getMegNote().append(Color.RED,"[系统信息]" + systemMeg); //自动下滚 receiveMeg.setCaretPosition(receiveMeg.getDocument().getLength()); }else if(megType.equalsIgnoreCase("服务器关闭")){//处理服务器关�? //关闭流和套接�字 dataOut.close(); dataIn.close(); clientSocket.close(); //即使是私聊也要停� receiveMeg.append(Color.red,"服务器已经关闭…………" + "\n"); mn.getMegNote().append(Color.RED,"服务器已经关闭…………" + "\n"); //自动下滚 receiveMeg.setCaretPosition( receiveMeg.getDocument().getLength()); break; }else if(megType.equalsIgnoreCase("聊天信息")){//处理聊天信息 receiveMsg(); //自动下滚 receiveMeg.setCaretPosition( receiveMeg.getDocument().getLength()); }else if(megType.equalsIgnoreCase("用户列表")){//处理用户列表 int testEnd = 0; String userList = (String)dataIn.readObject(); String userNames[] = userList.split("\n"); userOnlineList.removeAllItems(); userOnlineList.addItem("所有人"); while(testEnd < userNames.length){ userOnlineList.addItem(userNames[testEnd]); testEnd++; } }else if(megType.equalsIgnoreCase("kicked!")){ //踢人 String systemMeg = (String)dataIn.readObject(); receiveMeg.append(Color.RED,"[踢人信息]" + systemMeg + "\n"); mn.getMegNote().append(Color.RED,"[踢人信息]" + systemMeg + "\n"); //自动下滚 receiveMeg.setCaretPosition(receiveMeg.getDocument().getLength()); JOptionPane.showMessageDialog(null,"你被踢出了聊天大厅!!" + " 发言已经被屏蔽!"+ "如果有问题请与系统最高级管理员联系!", "系统警告",JOptionPane.WARNING_MESSAGE); //屏蔽不法分子:) ClientReceiveThread.isStop = true; //即使是私聊也要 sendMeg.setEnabled(false); sendMegTextPane.setEditable(false); }else if(megType.equalsIgnoreCase("下线")){ String leave = (String)dataIn.readObject(); String userName = (String)dataIn.readObject(); for(int i =0;i <= jList.Length();i++){ jList.setSelectedIndex(i); int m = jList.getSelectedIndex(); jList.iconListItem = (IconListItem)jList.getModel().getElementAt(m); String name = jList.iconListItem.getText(); if(userName.equalsIgnoreCase(name)){ jList.removeAllElements(); jList.addString("秘密会谈中:)"); break; } } System.out.println(userName); receiveMeg.append(Color.LIGHT_GRAY,leave); mn.getMegNote().append(Color.LIGHT_GRAY,leave); }else if(megType.equalsIgnoreCase("警告")){ String warning = (String)dataIn.readObject(); receiveMeg.append(Color.RED,warning); mn.getMegNote().append(Color.red,warning); }else if(megType.equalsIgnoreCase("图片")){ receiveIcon();//接收图片 }else if(megType.equalsIgnoreCase("被私聊")){ applyUserName = (String)dataIn.readObject(); int result = JOptionPane.showConfirmDialog(null, applyUserName + "想和您私聊," + "你有一分钟的时间考虑,否则自动拒绝!" ,"请求信息",JOptionPane.YES_NO_OPTION, JOptionPane.INFORMATION_MESSAGE); String add = FormatDateTime.dateAdd("s", 10, "yyyy-MM-dd HH:mm:ss"); boolean isTime = FormatDateTime.secondCompare(add); if(isTime==true){ System.out.println("时间到了"); } if(result == JOptionPane.YES_OPTION){ jList.addString(applyUserName); PC = new PersonalChat(); PC.sendObjects(clientSocket, dataIn, dataOut, userOnlineList,statusCheckBox,actionList,jList); PC.setVisible(true); IMPanelClient.isClick=true; }else if(isTime==false||result == JOptionPane.NO_OPTION) { dataOut.writeObject("拒绝私聊"); dataOut.flush(); dataOut.writeObject(applyUserName); dataOut.flush(); } }else if(megType.equalsIgnoreCase("私聊信息")){ recevMsg(); //自动下滚 PC.recevTextPane.setCaretPosition( receiveMeg.getDocument().getLength()); }else if(megType.equalsIgnoreCase("表情")){ ImageIcon action =(ImageIcon)dataIn.readObject(); PC.recevTextPane.setCaretPosition(//设定光标的位 PC.recevTextPane.getDocument().getLength()); PC.chatNotesTextPane.setCaretPosition( PC.chatNotesTextPane.getDocument().getLength()); PC.recevTextPane.insertIcon(action); PC.chatNotesTextPane.insertIcon(action); PC.recevTextPane.append("\n"); PC.chatNotesTextPane.append("\n"); }else if(megType.equalsIgnoreCase("退出私聊")){ if(PC.recevTextPane!=null&&PC.chatNotesTextPane!=null){ PC.recevTextPane.append(Color.lightGray, "\n"+applyUserName + "已经退出了私聊!!\n"); PC.chatNotesTextPane.append(Color.lightGray, "\n"+applyUserName + "已经退出了私聊!!\n"); } jList.removeAllElements(); jList.addString("秘密会谈中:)"); }else if(megType.equalsIgnoreCase("对方拒绝了你的请求")){ JOptionPane.showConfirmDialog(null, "很遗憾,对方拒绝了您的请求~~" ,"系统信息",JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE); }else if(megType.equalsIgnoreCase("语音聊天")){ String request = applyUserName+"请求语音聊天…………\n"; PC.getVoiceTextPane().append(Color.PINK,request); }else if(megType.equalsIgnoreCase("语音数据")){ System.out.println("2222"); //播放声音 //playVoice(); } } catch (Exception e) { e.printStackTrace(); } } } private void recevMsg() throws IOException, ClassNotFoundException{ //接受用户名 userName = (String)dataIn.readObject(); PC.recevTextPane.append(Color.PINK,userName); PC.chatNotesTextPane.append(Color.PINK,userName); //接受表情 String sendAction =(String)dataIn.readObject(); PC.recevTextPane.append(Color.BLACK, sendAction); PC.chatNotesTextPane.append(Color.black, sendAction); String to = (String)dataIn.readObject(); PC.recevTextPane.append(Color.BLACK,to); PC.chatNotesTextPane.append(Color.black,to); //接收聊天对象 String toSomebody = (String)dataIn.readObject(); PC.recevTextPane.append(Color.blue,toSomebody); PC.chatNotesTextPane.append(Color.blue,toSomebody); String say = (String)dataIn.readObject(); PC.recevTextPane.append(Color.black,say); PC.chatNotesTextPane.append(Color.black,say); //接收信息发送的日期 String date = (String)dataIn.readObject(); PC.recevTextPane.append(Color.LIGHT_GRAY,date); PC.chatNotesTextPane.append(Color.LIGHT_GRAY,date); //是否是悄悄话 String sendStatus = (String)dataIn.readObject(); PC.recevTextPane.append(Color.LIGHT_GRAY,sendStatus); PC.chatNotesTextPane.append(Color.LIGHT_GRAY,sendStatus); //聊天信息内容 String message = (String) dataIn.readObject(); //聊天信息颜色 color = (Color)dataIn.readObject(); PC.recevTextPane.append(color,message+"\n"); PC.chatNotesTextPane.append(color, message+"\n"); } /*********************************************** * 接收从IMPanelClient.class中传过来的对象以便在这个类中使用 * */ public void sendObject(Socket clientSocket, ObjectOutputStream dataOut,ObjectInputStream dataIn,MyTextPane receiveMeg, JComboBox userOnlineList,JButton sendMeg,MessageNote mn, JTextField sendMegTextPane,Color color,ImageIcon icon,JCheckBox statusCheckBox, JComboBox actionList,MyList jList){ this.clientSocket = clientSocket; this.dataOut = dataOut; this.dataIn = dataIn; this.receiveMeg = receiveMeg; this.userOnlineList = userOnlineList; this.sendMeg = sendMeg; this.sendMegTextPane = sendMegTextPane; this.statusCheckBox = statusCheckBox; this.actionList = actionList; this.jList = jList; this.mn = mn; ClientReceiveThread.isStop = false; } private void receiveMsg() throws IOException, ClassNotFoundException{ //接受用户名 String userName = (String)dataIn.readObject(); receiveMeg.append(Color.PINK,userName); mn.getMegNote().append(Color.BLUE,userName); //接受表情 String sendAction =(String)dataIn.readObject(); receiveMeg.append(Color.BLACK,sendAction); mn.getMegNote().append(Color.BLACK,sendAction); String to = (String)dataIn.readObject(); receiveMeg.append(Color.BLACK,to); mn.getMegNote().append(Color.BLACK,to); //接收聊天对象 String toSomebody = (String)dataIn.readObject(); receiveMeg.append(Color.BLUE,toSomebody); mn.getMegNote().append(Color.BLUE,toSomebody); String say = (String)dataIn.readObject(); receiveMeg.append(Color.BLACK,say+":"); mn.getMegNote().append(Color.BLACK,say+":"); //信息发送时间 String date = (String)dataIn.readObject(); receiveMeg.append(Color.LIGHT_GRAY,date+"\n"); mn.getMegNote().append(Color.LIGHT_GRAY,date+"\n"); //是否是悄悄话 String sendStatus = (String)dataIn.readObject(); receiveMeg.append(Color.lightGray,sendStatus); mn.getMegNote().append(Color.lightGray,sendStatus); //聊天信息内容 String message = (String) dataIn.readObject(); //聊天信息颜色 color = (Color)dataIn.readObject(); receiveMeg.append(color,message+"\n\n"); mn.getMegNote().append(color,message+"\n\n"); } /** * 接收图片 * */ private void receiveIcon() throws IOException,ClassNotFoundException{ receiveMeg.setCaretPosition(//设定光标的位置 receiveMeg.getDocument().getLength()); icon = (ImageIcon)dataIn.readObject(); receiveMeg.insertIcon(icon); mn.getMegNote().insertIcon(icon); String userName = (String)dataIn.readObject(); receiveMeg.append(Color.blue,userName + "\n\n"); mn.getMegNote().append(Color.blue, userName + "\n\n"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -