📄 chatservice.java
字号:
package chatrmio;import java.rmi.*;import java.rmi.server.*;import java.util.*;/** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2009</p> * <p>Company: </p> * @author not attributable * @version 1.0 */public class ChatService extends UnicastRemoteObject implements IChatService{//vector是注册加入的聊天者的数据结构 Vector clients; public ChatService() throws RemoteException { super(); clients=new Vector(); } //向所有已进入的聊天者广播新加入者, 并注册新加入聊天者。 public void addClient(IChatClient client,String msg) throws RemoteException { if(!clients.contains(client)) { for(int i=0;i<clients.size();i++) { sendMessage((IChatClient)clients.elementAt(i),msg); } clients.addElement(client); } } //向所有已进入的聊天者广播有人退出, 并撤消该聊天者。 public void removeClient(IChatClient client,String msg) throws RemoteException { if(clients.contains(client)) { for(int i=0;i<clients.size();i++) { //sendMessage((IChatClient)clients.elementAt(i),msg); ( (IChatClient) clients.elementAt(i)).sendMessage(msg); } clients.removeElement(client); } } public void sendMessage(IChatClient client,String msg) throws RemoteException { if(!clients.contains(client)) return; // 向所有聊天者发送消息 for(int i=0;i<clients.size();i++) { String user=client.getName(); if(user==null || user=="") user = "anonymous"; ( (IChatClient) clients.elementAt(i)).sendMessage(user + " : " + msg); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -