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

📄 serverreceivethread.java

📁 注:语音聊天部分还未完成
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.gamvan.club.users.im.server;import java.awt.Color;import java.io.IOException;import javax.swing.ImageIcon;import javax.swing.JComboBox;import com.gamvan.club.users.im.tools.MyTextPane;public class ServerReceiveThread extends Thread {	/**	 * 此类需要接收客户端发来的信息,并对该信息进行分析,执行相应的请求	 * 服务器接受的消息有两种,即聊天信息和下线请求	 */	private MyTextPane chatMeg = null;//聊天信息框	private SingleUserMegData clientData = null;//用户数据	private UserInfoList userInfoList = null;//在线用户信息列表	private SingleUserMegData friendNode = null;	private JComboBox userOnlineList = null;	private String messageType = null;	private String toSomebody,date,sendStatus,message,sendAction;	private Color color = null;	private ImageIcon icon = null;	public boolean isStop = false;	public ServerReceiveThread(){}//构造函数		public void run(){//服务器接收线程执行体		megManage();	}	public void megManage(){//处理用户发来的信息		            sendUserListToAll();//向所有在线用户发送用户信息列表		            while(!isStop&&!clientData.socket.isClosed()){//如果套接字是打开的			                                    try{								                              //读取信息类型                messageType = (String)clientData.dataIn.readObject();                if(messageType.equalsIgnoreCase("聊天信息")){//如果是聊天信息                	receiveMeg();                  	String msg =" <" + clientData.userName +"> "+" ["                  		 + sendAction +"] "+              		"对" + " <"+toSomebody+"> " + "说: "+              			date + " \n\n ";                  	String str = message + "\n";                  	if(toSomebody.equalsIgnoreCase("所有人")){                  		sendChatMeg();                  	}//end                  	if(sendStatus.equalsIgnoreCase("<悄悄话>")){//判断发送状态                  		msg = "[悄悄话]" + msg;                  		chatMeg.append(Color.RED,msg);                  		chatMeg.append(Color.DARK_GRAY,str);                  		//把信息发送给自己                  		smallChatToYou();                  		//把信息发送给别人                  		smallChat();                  		//与某个人对话                  		}else if(!toSomebody.equalsIgnoreCase("所有人")                                && !sendStatus.equalsIgnoreCase("<悄悄话>")){                  			sendChatMeg();                               		}else if(sendStatus.equalsIgnoreCase("personal")){                  			//把信息发送给自己                      		smallChatToYouP();                      		//把信息发送给别人                      		smallChatP();                  		}                }else if(messageType.equalsIgnoreCase("用户下线")){//处理用户下线请求                    //在用户链表中查找该用户的节点                     SingleUserMegData delNode = userInfoList.find(                                      clientData.userName);                     //删除该用户节点                     userInfoList.delete(delNode);                                        String leaveUser = "用户" + clientData.userName +"下线\n";                     int userCount = userInfoList.getUserCount();                                    int testEnd = 0;                     userOnlineList.removeAllItems();                                          userOnlineList.addItem("所有人");                                          while(testEnd < userCount){//重新组合在线用户列表                         delNode = userInfoList.find(testEnd);                         if(delNode == null){                            testEnd++;                            continue;                          }                         userOnlineList.addItem(delNode.userName);                                              testEnd++;                       }                                                         chatMeg.append(Color.LIGHT_GRAY,leaveUser);                                           sendLeaveToAll(leaveUser,clientData.userName);//向所有用户发送下线通知                        sendUserListToAll();//向所有在线用户发送最新用户列表                        break;				}else if(messageType.equalsIgnoreCase("用户离线")){					 //在用户链表中查找该用户的节点                    SingleUserMegData delNode = userInfoList.find(                                     clientData.userName);                    //删除该用户节点                    userInfoList.delete(delNode);                     String leaveUser = "用户" + clientData.userName +"离线\n";                    int userCount = userInfoList.getUserCount();                                   int testEnd = 0;                    userOnlineList.removeAllItems();                                       userOnlineList.addItem("所有人");                                 while(testEnd < userCount){//重新组合在线用户列表                        delNode = userInfoList.find(testEnd);                        if(delNode == null){                           testEnd++;                           continue;                         }                        userOnlineList.addItem(delNode.userName);                                            testEnd++;                      }                                                       chatMeg.append(Color.LIGHT_GRAY,leaveUser);                                         sendLeaveToAll(leaveUser,clientData.userName);//向所有用户发送下线通知                       sendUserListToAll();//向所有在线用户发送最新用户列表                       break;                    				}else if(messageType.equalsIgnoreCase("kicked!")){									//处理管理员发送过来的踢人信息					String toSomebody = (String)clientData.dataIn.readObject();					SingleUserMegData delUser = userInfoList.find(toSomebody);					delUser.dataOut.writeObject("kicked!");					delUser.dataOut.flush();					delUser.dataOut.writeObject("你被踢出了大厅!!!");					delUser.dataOut.flush();					DelUser(delUser);//删除节点					sendUserListToAll();//把用户列表发送给所有人				}else if(messageType.equalsIgnoreCase("警告")){					//获得要警告的对象					String toSomebody = (String)clientData.dataIn.readObject();					String warningMeg = toSomebody + "被严重警告!!" + "\n";					sendWarningToAll(warningMeg);//向所有用户发送警告信息				}else if(messageType.equalsIgnoreCase("发送图片")){										icon =(ImageIcon)clientData.dataIn.readObject();					chatMeg.insertIcon(icon);					sendIconToAll();								}else if(messageType.equalsIgnoreCase("私聊")){					/*					 * 首先去用户列表中去查找节点,找到以后发送给被加为好友的用户一个请求					 * */										int userIndex = clientData.dataIn.readInt();					//找到用户列表中的这个人,应该是从数据库中找到这个人					friendNode = userInfoList.findID(userIndex);										if(friendNode != null){						//向被私聊的人发送请求						friendNode.dataOut.writeObject("被私聊");						friendNode.dataOut.flush();							friendNode.dataOut.writeObject(clientData.userName);						friendNode.dataOut.flush();					}							}else if(messageType.equalsIgnoreCase("私聊信息")){                                    					receiveMeg();					if(sendStatus.equalsIgnoreCase("<personal>" + "\n\n")){						//把信息发送给自己						smallChatToYouP();						//把信息发送给别人						smallChatP();					}            						}else if(messageType.equalsIgnoreCase("表情")){					recevAction();//接收私聊的表情				}else if(messageType.equalsIgnoreCase("退出私聊")){					if(friendNode != null){					                                            friendNode.dataOut.writeObject("退出私聊");			                                            friendNode.dataOut.flush();                       					}                                	                        	                               				}else if(messageType.equalsIgnoreCase("拒绝私聊")){					String applyUserName = (String)clientData.dataIn.readObject();					SingleUserMegData node = userInfoList.find(applyUserName);					if(node != null){						node.dataOut.writeObject("对方拒绝了你的请求");						node.dataOut.flush();					}				}else if(messageType.equalsIgnoreCase("语音聊天")){                                    friendNode.dataOut.writeObject("语音聊天");                                    friendNode.dataOut.flush();                                                                   }else if(messageType.equalsIgnoreCase("语音数据")){                                    System.out.println("111111111111");                                   byte[] voice = new byte[1024];                                                                    int b = clientData.dataIn.read(voice);                                   //发送给自己                                   clientData.dataOut.writeObject("语音数据");                                   clientData.dataOut.flush();                                   clientData.dataOut.write(b);                                   clientData.dataOut.flush();                                   //发送给别人                                   friendNode.dataOut.writeObject("语音数据");                                   friendNode.dataOut.flush();                                   friendNode.dataOut.write(b);                                   friendNode.dataOut.flush();                                }                             			}catch(Exception e){                            e.printStackTrace();                            System.exit(0);                //这里会抛出异常			}		}	}        private void recevAction() throws IOException, ClassNotFoundException{                        ImageIcon action = (ImageIcon)clientData.dataIn.readObject();            //先把表情发送给自己            clientData.dataOut.writeObject("表情");            clientData.dataOut.flush();            clientData.dataOut.writeObject(action);            clientData.dataOut.flush();            //再把表情发送给好友            friendNode.dataOut.writeObject("表情");            friendNode.dataOut.flush();            friendNode.dataOut.writeObject(action);            friendNode.dataOut.flush();        }	private void smallChatToYouP() throws IOException{		//发送聊天信息标识		clientData.dataOut.writeObject("私聊信息");		clientData.dataOut.flush();		//发送用户名		clientData.dataOut.writeObject(clientData.userName);		clientData.dataOut.flush();		//发送动作		clientData.dataOut.writeObject(sendAction);		clientData.dataOut.flush();		clientData.dataOut.writeObject("对");		clientData.dataOut.flush();			//发送接受信息的对象			clientData.dataOut.writeObject(toSomebody);			clientData.dataOut.flush();			clientData.dataOut.writeObject("说");			clientData.dataOut.flush();			//发送信息时间			clientData.dataOut.writeObject(date);			clientData.dataOut.flush();			//发送状态			clientData.dataOut.writeObject(sendStatus);			clientData.dataOut.flush();			//发送信息内容			clientData.dataOut.writeObject(message);			clientData.dataOut.flush();			if(color != null){				//发送聊天信息内容的字体颜色				clientData.dataOut.writeObject(color);				clientData.dataOut.flush();			}			}	private void smallChatP() throws IOException{		SingleUserMegData  node =			userInfoList.find(toSomebody);		if(node != null){			//发送聊天信息标识			node.dataOut.writeObject("私聊信息");			node.dataOut.flush();			//发送用户名			node.dataOut.writeObject(clientData.userName);			node.dataOut.flush();			//发送动作			node.dataOut.writeObject(sendAction);			node.dataOut.flush();			node.dataOut.writeObject("对");			node.dataOut.flush();			//发送接受信息的对象			node.dataOut.writeObject(toSomebody);			node.dataOut.flush();			node.dataOut.writeObject("说");			node.dataOut.flush();			//发送信息时间			node.dataOut.writeObject(date);			node.dataOut.flush();			//发送状态			node.dataOut.writeObject(sendStatus);			node.dataOut.flush();			//发送信息内容			node.dataOut.writeObject(message);			node.dataOut.flush();			if(color != null){				//发送聊天信息内容的字体颜色				node.dataOut.writeObject(color);				node.dataOut.flush();			}		}	}	private void sendIconToAll() throws IOException{

⌨️ 快捷键说明

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