| ad5081d3  孙向锦
 
初始化项目 | 1
2
3
4
5 |   package com.fh.plugin.websocketInstantMsg;
  
  import java.io.IOException;
  import java.net.InetSocketAddress;
  import java.net.UnknownHostException;
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 6 |   import java.util.ArrayList;
 | 
| ad5081d3  孙向锦
 
初始化项目 | 7 |   import java.util.Date;
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 8 |   import java.util.List;
 | 
| ad5081d3  孙向锦
 
初始化项目 | 9
10
11
12
13
14
15
16
17
18 |   
  import net.sf.json.JSONObject;
  
  import org.java_websocket.WebSocket;
  import org.java_websocket.WebSocketImpl;
  import org.java_websocket.framing.Framedata;
  import org.java_websocket.handshake.ClientHandshake;
  import org.java_websocket.server.WebSocketServer;
  
  
 | 
| ad5081d3  孙向锦
 
初始化项目 | 19 |   public class ChatServer extends WebSocketServer{
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 20
21 |   	
  	private List<WebSocket> list = new ArrayList<WebSocket>();
 | 
| ad5081d3  孙向锦
 
初始化项目 | 22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 |   
  	public ChatServer(int port) throws UnknownHostException {
  		super(new InetSocketAddress(port));
  	}
  
  	public ChatServer(InetSocketAddress address) {
  		super(address);
  	}
  
  	/**
  	 * 触发连接事件
  	 */
  	@Override
  	public void onOpen( WebSocket conn, ClientHandshake handshake ) {
  		//this.sendToAll( "new connection: " + handshake.getResourceDescriptor() );
  		//System.out.println("===" + conn.getRemoteSocketAddress().getAddress().getHostAddress());
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 38 |   		list.add(conn);
 | 
| ad5081d3  孙向锦
 
初始化项目 | 39
40
41
42
43
44
45 |   	}
  
  	/**
  	 * 触发关闭事件
  	 */
  	@Override
  	public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 46 |   		list.remove(conn);
 | 
| ad5081d3  孙向锦
 
初始化项目 | 47
48
49
50
51
52
53 |   	}
  
  	/**
  	 * 客户端发送消息到服务器时触发事件
  	 */
  	@Override
  	public void onMessage(WebSocket conn, String message){
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 54
55
56
57 |   		for(WebSocket w:list){
  			if(w != conn){
  				w.send(message);
  			}
 | 
| ad5081d3  孙向锦
 
初始化项目 | 58
59
60
61
62
63
64
65
66
67
68 |   		}
  	}
  
  	public void onFragment( WebSocket conn, Framedata fragment ) {
  	}
  
  	/**
  	 * 触发异常事件
  	 */
  	@Override
  	public void onError( WebSocket conn, Exception ex ) {
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 69
70
71
72 |   		try{
  			conn.close(0);
  		}catch(Exception e){}
  		list.remove(conn);
 | 
| ad5081d3  孙向锦
 
初始化项目 | 73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 |   	}
  
  	
  	/**
  	 * 用户加入处理
  	 * @param user
  	 */
  	public void userjoin(String user, WebSocket conn){
  		JSONObject result = new JSONObject();
  		result.element("type", "user_join");
  		result.element("user", "<a onclick=\"toUserMsg('"+user+"');\">"+user+"</a>");
  		ChatServerPool.sendMessage(result.toString());				//把当前用户加入到所有在线用户列表中
  		String joinMsg = "{\"from\":\"[系统]\",\"content\":\""+user+"上线了\",\"timestamp\":"+new Date().getTime()+",\"type\":\"message\"}";
  		ChatServerPool.sendMessage(joinMsg);						//向所有在线用户推送当前用户上线的消息
  		result = new JSONObject();
  		result.element("type", "get_online_user");
  		ChatServerPool.addUser(user,conn);							//向连接池添加当前的连接对象
  		result.element("list", ChatServerPool.getOnlineUser());
  		ChatServerPool.sendMessageToUser(conn, result.toString());	//向当前连接发送当前在线用户的列表
  	}
  	
  	/**
  	 * 用户下线处理
  	 * @param user
  	 */
  	public void userLeave(WebSocket conn){
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 99 |   //		String user = ChatServerPool.getUserByKey(conn);
 | 
| ad5081d3  孙向锦
 
初始化项目 | 100 |   		boolean b = ChatServerPool.removeUser(conn);				//在连接池中移除连接
 | 
| ed19bdb6  孙向锦
 
修改密码逻辑 | 101
102
103
104
105
106
107
108 |   //		if(b){
  //			JSONObject result = new JSONObject();
  //			result.element("type", "user_leave");
  //			result.element("user", "<a onclick=\"toUserMsg('"+user+"');\">"+user+"</a>");
  //			ChatServerPool.sendMessage(result.toString());			//把当前用户从所有在线用户列表中删除
  //			String joinMsg = "{\"from\":\"[系统]\",\"content\":\""+user+"下线了\",\"timestamp\":"+new Date().getTime()+",\"type\":\"message\"}";
  //			ChatServerPool.sendMessage(joinMsg);					//向在线用户发送当前用户退出的消息
  //		}
 | 
| ad5081d3  孙向锦
 
初始化项目 | 109
110
111
112
113
114
115
116
117
118 |   	}
  	public static void main( String[] args ) throws InterruptedException , IOException {
  		WebSocketImpl.DEBUG = false;
  		int port = 8887; //端口
  		ChatServer s = new ChatServer(port);
  		s.start();
  		//System.out.println( "服务器的端口" + s.getPort() );
  	}
  
  }
 |