ad5081d3
孙向锦
初始化项目
|
1
2
|
package com.fh.plugin.websocketInstantMsg;
|
e7d09ea7
孙向锦
修改bug
|
3
|
import java.io.UnsupportedEncodingException;
|
ad5081d3
孙向锦
初始化项目
|
4
|
import java.net.InetSocketAddress;
|
e7d09ea7
孙向锦
修改bug
|
5
|
import java.net.URI;
|
ad5081d3
孙向锦
初始化项目
|
6
|
import java.net.UnknownHostException;
|
e7d09ea7
孙向锦
修改bug
|
7
|
import java.nio.ByteBuffer;
|
ed19bdb6
孙向锦
修改密码逻辑
|
8
|
import java.util.ArrayList;
|
ad5081d3
孙向锦
初始化项目
|
9
|
import java.util.Date;
|
ed19bdb6
孙向锦
修改密码逻辑
|
10
|
import java.util.List;
|
e7d09ea7
孙向锦
修改bug
|
11
|
import java.util.Scanner;
|
ad5081d3
孙向锦
初始化项目
|
12
13
14
15
|
import net.sf.json.JSONObject;
import org.java_websocket.WebSocket;
|
e7d09ea7
孙向锦
修改bug
|
16
17
|
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_17;
|
ad5081d3
孙向锦
初始化项目
|
18
19
|
import org.java_websocket.framing.Framedata;
import org.java_websocket.handshake.ClientHandshake;
|
e7d09ea7
孙向锦
修改bug
|
20
|
import org.java_websocket.handshake.ServerHandshake;
|
ad5081d3
孙向锦
初始化项目
|
21
22
23
|
import org.java_websocket.server.WebSocketServer;
|
ad5081d3
孙向锦
初始化项目
|
24
|
public class ChatServer extends WebSocketServer{
|
ed19bdb6
孙向锦
修改密码逻辑
|
25
26
|
private List<WebSocket> list = new ArrayList<WebSocket>();
|
ad5081d3
孙向锦
初始化项目
|
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
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
孙向锦
修改密码逻辑
|
43
|
list.add(conn);
|
ad5081d3
孙向锦
初始化项目
|
44
45
46
47
48
49
50
|
}
/**
* 触发关闭事件
*/
@Override
public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
|
ed19bdb6
孙向锦
修改密码逻辑
|
51
|
list.remove(conn);
|
ad5081d3
孙向锦
初始化项目
|
52
53
54
55
56
57
58
|
}
/**
* 客户端发送消息到服务器时触发事件
*/
@Override
public void onMessage(WebSocket conn, String message){
|
ed19bdb6
孙向锦
修改密码逻辑
|
59
60
61
62
|
for(WebSocket w:list){
if(w != conn){
w.send(message);
}
|
ad5081d3
孙向锦
初始化项目
|
63
64
65
66
67
68
69
70
71
72
73
|
}
}
public void onFragment( WebSocket conn, Framedata fragment ) {
}
/**
* 触发异常事件
*/
@Override
public void onError( WebSocket conn, Exception ex ) {
|
ed19bdb6
孙向锦
修改密码逻辑
|
74
75
76
77
|
try{
conn.close(0);
}catch(Exception e){}
list.remove(conn);
|
ad5081d3
孙向锦
初始化项目
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
}
/**
* 用户加入处理
* @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
孙向锦
修改密码逻辑
|
104
|
// String user = ChatServerPool.getUserByKey(conn);
|
ad5081d3
孙向锦
初始化项目
|
105
|
boolean b = ChatServerPool.removeUser(conn); //在连接池中移除连接
|
ed19bdb6
孙向锦
修改密码逻辑
|
106
107
108
109
110
111
112
113
|
// 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
孙向锦
初始化项目
|
114
|
}
|
e7d09ea7
孙向锦
修改bug
|
115
116
117
118
119
|
public static void main( String[] args ) throws Exception {
// WebSocketImpl.DEBUG = false;
// int port = 8887; //端口
// ChatServer s = new ChatServer(port);
// s.start();
|
ad5081d3
孙向锦
初始化项目
|
120
|
//System.out.println( "服务器的端口" + s.getPort() );
|
e7d09ea7
孙向锦
修改bug
|
121
122
123
124
125
126
127
128
|
testClient();
Scanner scanner = new Scanner(System.in);
while(true){
String text = scanner.next();
if(!"exit".equals(text.toLowerCase())){
client.send(text);
}
}
|
ad5081d3
孙向锦
初始化项目
|
129
|
}
|
e7d09ea7
孙向锦
修改bug
|
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
public static WebSocketClient client;
public static void testClient() throws Exception{
client = new WebSocketClient(new URI("ws://127.0.0.1:8080/echo"),new Draft_17()) {
@Override
public void onOpen(ServerHandshake arg0) {
System.out.println("打开链接");
client.send("hello world");
}
@Override
public void onMessage(String arg0) {
System.out.println(arg0);
}
@Override
public void onError(Exception arg0) {
arg0.printStackTrace();
System.out.println("发生错误已关闭");
}
@Override
public void onClose(int arg0, String arg1, boolean arg2) {
System.out.println("链接已关闭");
}
@Override
public void onMessage(ByteBuffer bytes) {
try {
System.out.println(new String(bytes.array(),"utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
client.connect();
}
public static void send(byte[] bytes){
client.send(bytes);
}
|
ad5081d3
孙向锦
初始化项目
|
174
175
|
}
|