-
Notifications
You must be signed in to change notification settings - Fork 138
/
ChatApplication.java
74 lines (65 loc) · 2.55 KB
/
ChatApplication.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
/*
* Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.grizzly.samples.websockets;
import java.util.logging.Level;
import org.glassfish.grizzly.http.HttpRequestPacket;
import org.glassfish.grizzly.websockets.DataFrame;
import org.glassfish.grizzly.websockets.ProtocolHandler;
import org.glassfish.grizzly.websockets.WebSocket;
import org.glassfish.grizzly.websockets.WebSocketApplication;
import org.glassfish.grizzly.websockets.WebSocketListener;
public class ChatApplication extends WebSocketApplication {
@Override
public boolean isApplicationRequest(HttpRequestPacket request) {
return "/chat".equals(request.getRequestURI());
}
@Override
public WebSocket createSocket(ProtocolHandler handler,
HttpRequestPacket requestPacket,
WebSocketListener... listeners) {
final ChatWebSocket ws
= new ChatWebSocket(handler, requestPacket, listeners);
return ws;
}
@Override
public void onMessage(WebSocket socket, String data) {
if (data.startsWith("login:")) {
login((ChatWebSocket) socket, data);
} else {
broadcast(((ChatWebSocket) socket).getUser(), data);
}
}
@Override
public void onClose(WebSocket websocket, DataFrame frame) {
broadcast("system", ((ChatWebSocket)websocket).getUser() + " left the chat");
}
/**
* Broadcasts the text message from the user.
*
* @param user the user name
* @param text the text message
*/
private void broadcast(String user, String text) {
WebSocketsServlet.logger.log(Level.INFO, "Broadcasting: {0} from: {1}", new Object[]{text, user});
for (WebSocket websocket : getWebSockets()) {
final ChatWebSocket chat = (ChatWebSocket) websocket;
if (chat.getUser() != null) { // it may happen some websocket is on the list, but not logged in to the chat
chat.sendJson(user, text);
}
}
}
private void login(ChatWebSocket socket, String data) {
if (socket.getUser() == null) {
WebSocketsServlet.logger.info("ChatApplication.login");
socket.setUser(data.split(":")[1].trim());
broadcast(socket.getUser(), " has joined the chat.");
}
}
}