Blame view

src/com/fh/plugin/websocketVideo/VideoServerPool.java 1.02 KB
ad5081d3   孙向锦   初始化项目
1
2
3
4
5
6
7
8
  package com.fh.plugin.websocketVideo;
  
  import java.util.HashMap;
  import java.util.Map;
  import java.util.Set;
  
  import org.java_websocket.WebSocket;
  
ad5081d3   孙向锦   初始化项目
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  public class VideoServerPool {
  
  	private static final Map<WebSocket,String> userconnections = new HashMap<WebSocket,String>();
  	
  	/**
  	 * 向连接池中添加连接
  	 * @param inbound
  	 */
  	public static void addUser(String user, WebSocket conn){
  		userconnections.put(conn,user);	//添加连接
  	}
  	
  	/**
  	 * 移除连接池中的连接
  	 * @param inbound
  	 */
  	public static boolean removeUser(WebSocket conn){
  		if(userconnections.containsKey(conn)){
  			userconnections.remove(conn);	//移除连接
  			return true;
  		}else{
  			return false;
  		}
  	}
  	
  	/**
  	 * 向所有的用户发送消息
  	 * @param message
  	 */
  	public static void sendMessage(String message){
  		Set<WebSocket> keySet = userconnections.keySet();
  		synchronized (keySet) {
  			for (WebSocket conn : keySet) {
  				String user = userconnections.get(conn);
  				if(user != null){
  					conn.send(message);
  				}
  			}
  		}
  	}
  }