/
GWTChatSocketServlet.java
138 lines (124 loc) · 4.47 KB
/
GWTChatSocketServlet.java
1
2
3
4
5
6
7
8
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* The MIT License
* Copyright (c) 2010 Tad Glines
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.glines.socketio.sample.gwtchat;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.util.ajax.JSON;
import org.eclipse.jetty.util.log.Log;
import com.glines.socketio.common.DisconnectReason;
import com.glines.socketio.common.SocketIOException;
import com.glines.socketio.server.SocketIOFrame;
import com.glines.socketio.server.SocketIOInbound;
import com.glines.socketio.server.SocketIOServlet;
public class GWTChatSocketServlet extends SocketIOServlet {
private static final long serialVersionUID = 1L;
private AtomicInteger ids = new AtomicInteger(1);
private Set<GWTChatConnection> connections = new HashSet<GWTChatConnection>();
private class GWTChatConnection implements SocketIOInbound {
private SocketIOOutbound outbound = null;
private Integer sessionId = ids.getAndIncrement();
@Override
public String getProtocol() {
return null;
}
@Override
public void onConnect(SocketIOOutbound outbound) {
this.outbound = outbound;
synchronized (connections) {
connections.add(this);
}
try {
outbound.sendMessage(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
Collections.singletonMap("welcome", "Welcome to GWT Chat!")));
} catch (SocketIOException e) {
outbound.disconnect();
}
broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
Collections.singletonMap("announcement", sessionId + " connected")));
}
@Override
public void onDisconnect(DisconnectReason reason, String errorMessage) {
synchronized(this) {
this.outbound = null;
}
synchronized (connections) {
connections.remove(this);
}
broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
Collections.singletonMap("announcement", sessionId + " disconnected")));
}
@Override
public void onMessage(int messageType, String message) {
Log.debug("Recieved: " + message);
if (message.equals("/rclose")) {
outbound.close();
} else if (message.equals("/rdisconnect")) {
outbound.disconnect();
} else if (message.startsWith("/sleep")) {
int sleepTime = 1;
String parts[] = message.split("\\s+");
if (parts.length == 2) {
sleepTime = Integer.parseInt(parts[1]);
}
try {
Thread.sleep(sleepTime * 1000);
} catch (InterruptedException e) {
// Ignore
}
try {
outbound.sendMessage(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
Collections.singletonMap("message","Slept for " + sleepTime + " seconds.")));
} catch (SocketIOException e) {
outbound.disconnect();
}
} else {
broadcast(SocketIOFrame.JSON_MESSAGE_TYPE, JSON.toString(
Collections.singletonMap("message",
new String[]{sessionId.toString(), (String)message})));
}
}
private void broadcast(int messageType, String message) {
Log.debug("Broadcasting: " + message);
synchronized (connections) {
for(GWTChatConnection c: connections) {
if (c != this) {
try {
c.outbound.sendMessage(messageType, message);
} catch (IOException e) {
c.outbound.disconnect();
}
}
}
}
}
}
@Override
protected SocketIOInbound doSocketIOConnect(HttpServletRequest request, String[] protocols) {
return new GWTChatConnection();
}
}