forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketConnectionManager.java
More file actions
216 lines (190 loc) · 6.61 KB
/
WebSocketConnectionManager.java
File metadata and controls
216 lines (190 loc) · 6.61 KB
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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package act.ws;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.ActionContext;
import act.app.App;
import act.app.AppServiceBase;
import act.util.Stateless;
import act.xio.WebSocketConnection;
import com.alibaba.fastjson.JSON;
import org.osgl.$;
import org.osgl.http.H;
import org.osgl.logging.LogManager;
import org.osgl.logging.Logger;
import java.util.Collection;
/**
* Manage {@link WebSocketConnection} through {@link WebSocketConnectionRegistry}
*/
@Stateless
public class WebSocketConnectionManager extends AppServiceBase<WebSocketConnectionManager> {
private static final Logger logger = LogManager.get(WebSocketConnectionManager.class);
private final WebSocketConnectionRegistry bySessionId = new WebSocketConnectionRegistry();
private final WebSocketConnectionRegistry byUsername = new WebSocketConnectionRegistry();
private final WebSocketConnectionRegistry byUrl = new WebSocketConnectionRegistry();
private final WebSocketConnectionRegistry byTag = new WebSocketConnectionRegistry();
private String wsTicketKey;
public WebSocketConnectionManager(App app) {
super(app);
wsTicketKey = app.config().wsTicketKey();
}
public WebSocketConnectionRegistry sessionRegistry() {
return bySessionId;
}
public WebSocketConnectionRegistry usernameRegistry() {
return byUsername;
}
public WebSocketConnectionRegistry urlRegistry() {
return byUrl;
}
public WebSocketConnectionRegistry tagRegistry() {
return byTag;
}
/**
* Add tag to any websocket connection linked to the session specified
* @param session the session used to find websocket connections
* @param tag the tag to subscribe
*/
public void subscribe(H.Session session, final String tag) {
sessionRegistry().accept(session.id(), new $.Visitor<WebSocketConnection>() {
@Override
public void visit(WebSocketConnection connection) throws $.Break {
byTag.register(tag, connection);
}
});
}
/**
* Send message to all connections connected to give URL
* @param message the message
* @param url the url
*/
public void sendToUrl(String message, String url) {
sendToConnections(message, urlRegistry(), url);
}
/**
* Send JSON representation of given data object to all connections
* connected to given URL
*
* @param data the data object
* @param url the url
*/
public void sendJsonToUrl(Object data, String url) {
sendToUrl(JSON.toJSONString(data), url);
}
/**
* Send message to all connections tagged with given label
* @param message the message
* @param label the tag label
*/
public void sendToTagged(String message, String label) {
sendToConnections(message, tagRegistry(), label);
}
/**
* Send message to all connections tagged with all given tags
* @param message the message
* @param labels the tag labels
*/
public void sendToTagged(String message, String ... labels) {
for (String label : labels) {
sendToTagged(message, label);
}
}
/**
* Send message to all connections tagged with all given tags
* @param message the message
* @param labels the tag labels
*/
public void sendToTagged(String message, Collection<String> labels) {
for (String label : labels) {
sendToTagged(message, label);
}
}
/**
* Send JSON representation of given data object to all connections tagged with
* given label
* @param data the data object
* @param label the tag label
*/
public void sendJsonToTagged(Object data, String label) {
sendToTagged(JSON.toJSONString(data), label);
}
/**
* Send JSON representation of given data object to all connections tagged with all give tag labels
* @param data the data object
* @param labels the tag labels
*/
public void sendJsonToTagged(Object data, String ... labels) {
for (String label : labels) {
sendJsonToTagged(data, label);
}
}
/**
* Send JSON representation of given data object to all connections tagged with all give tag labels
* @param data the data object
* @param labels the tag labels
*/
public void sendJsonToTagged(Object data, Collection<String> labels) {
for (String label : labels) {
sendJsonToTagged(data, label);
}
}
/**
* Send message to all connections of a user
* @param message the message
* @param username the username
*/
public void sendToUser(String message, String username) {
sendToConnections(message, usernameRegistry(), username);
}
/**
* Send JSON representation of given data object to all connections of a user
* @param data the data object
* @param username the username
*/
public void sendJsonToUser(Object data, String username) {
sendToUser(JSON.toJSONString(data), username);
}
public void registerNewConnection(WebSocketConnection connection, ActionContext context) {
bySessionId.register(context.session().id(), connection);
String username = context.username();
if (null == username) {
username = context.paramVal(wsTicketKey);
}
if (null != username) {
byUsername.register(username, connection);
}
String url = context.req().url();
byUrl.register(url, connection);
}
@Override
protected void releaseResources() {
bySessionId.destroy();
byUsername.destroy();
byUrl.destroy();
byTag.destroy();
}
private void sendToConnections(String message, WebSocketConnectionRegistry registry, String key) {
for (WebSocketConnection conn : registry.get(key)) {
if (logger.isTraceEnabled()) {
logger.trace("send to websocket connection by key: %s", key);
}
conn.send(message);
}
}
}