| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// |
| 5 |
// This program and the accompanying materials are made available under the |
| 6 |
// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 |
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 |
// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 |
// |
| 10 |
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 |
// ======================================================================== |
| 12 |
// |
| 13 |
|
| 14 |
package org.example; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
import java.util.List; |
| 18 |
import java.util.ListIterator; |
| 19 |
import java.util.concurrent.CopyOnWriteArrayList; |
| 20 |
|
| 21 |
import jakarta.servlet.ServletException; |
| 22 |
import jakarta.servlet.http.HttpServletRequest; |
| 23 |
import jakarta.servlet.http.HttpServletResponse; |
| 24 |
import org.eclipse.jetty.ee9.websocket.api.RemoteEndpoint; |
| 25 |
import org.eclipse.jetty.ee9.websocket.api.Session; |
| 26 |
import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketClose; |
| 27 |
import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketConnect; |
| 28 |
import org.eclipse.jetty.ee9.websocket.api.annotations.OnWebSocketMessage; |
| 29 |
import org.eclipse.jetty.ee9.websocket.api.annotations.WebSocket; |
| 30 |
import org.eclipse.jetty.ee9.websocket.server.JettyServerUpgradeRequest; |
| 31 |
import org.eclipse.jetty.ee9.websocket.server.JettyServerUpgradeResponse; |
| 32 |
import org.eclipse.jetty.ee9.websocket.server.JettyWebSocketCreator; |
| 33 |
import org.eclipse.jetty.ee9.websocket.server.JettyWebSocketServlet; |
| 34 |
import org.eclipse.jetty.ee9.websocket.server.JettyWebSocketServletFactory; |
| 35 |
|
| 36 |
@SuppressWarnings("serial") |
| 37 |
public class WebSocketChatServlet extends JettyWebSocketServlet implements JettyWebSocketCreator |
| 38 |
{ |
| 39 |
/** |
| 40 |
* Holds active sockets to other members of the chat |
| 41 |
*/ |
| 42 |
private final List<ChatWebSocket> members = new CopyOnWriteArrayList<ChatWebSocket>(); |
| 43 |
|
| 44 |
@Override |
| 45 |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException |
| 46 |
{ |
| 47 |
getServletContext().getNamedDispatcher("default").forward(request, response); |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public Object createWebSocket(JettyServerUpgradeRequest req, JettyServerUpgradeResponse resp) |
| 52 |
{ |
| 53 |
if (req.hasSubProtocol("chat")) |
| 54 |
{ |
| 55 |
resp.setAcceptedSubProtocol("chat"); |
| 56 |
return new ChatWebSocket(); |
| 57 |
} |
| 58 |
return null; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public void configure(JettyWebSocketServletFactory factory) |
| 63 |
{ |
| 64 |
factory.addMapping("/", this); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Create a WebSocket that echo's back the message to all other members of the servlet. |
| 69 |
*/ |
| 70 |
@WebSocket |
| 71 |
public class ChatWebSocket |
| 72 |
{ |
| 73 |
volatile Session session; |
| 74 |
volatile RemoteEndpoint remote; |
| 75 |
|
| 76 |
@OnWebSocketConnect |
| 77 |
public void onOpen(Session sess) |
| 78 |
{ |
| 79 |
this.session = sess; |
| 80 |
this.remote = sess.getRemote(); |
| 81 |
members.add(this); |
| 82 |
} |
| 83 |
|
| 84 |
@OnWebSocketMessage |
| 85 |
public void onMessage(String data) |
| 86 |
{ |
| 87 |
if (data.contains("disconnect")) |
| 88 |
{ |
| 89 |
session.close(); |
| 90 |
return; |
| 91 |
} |
| 92 |
|
| 93 |
ListIterator<ChatWebSocket> iter = members.listIterator(); |
| 94 |
while (iter.hasNext()) |
| 95 |
{ |
| 96 |
ChatWebSocket member = iter.next(); |
| 97 |
|
| 98 |
// Test if member is now disconnected |
| 99 |
if (!member.session.isOpen()) |
| 100 |
{ |
| 101 |
iter.remove(); |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
// Async write the message back. |
| 106 |
member.remote.sendString(data, null); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
@OnWebSocketClose |
| 111 |
public void onClose(int code, String message) |
| 112 |
{ |
| 113 |
members.remove(this); |
| 114 |
} |
| 115 |
} |
| 116 |
} |