Skip to content

Commit e5253dc

Browse files
committed
Add example of using WebSocketServer with systemd socket activation
1 parent 8eae452 commit e5253dc

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2010-2020 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
import java.io.IOException;
27+
import java.nio.ByteBuffer;
28+
import java.nio.channels.ServerSocketChannel;
29+
import java.util.Collections;
30+
import java.util.concurrent.atomic.AtomicInteger;
31+
import org.java_websocket.WebSocket;
32+
import org.java_websocket.drafts.Draft;
33+
import org.java_websocket.drafts.Draft_6455;
34+
import org.java_websocket.handshake.ClientHandshake;
35+
import org.java_websocket.server.WebSocketServer;
36+
37+
/**
38+
* This is a "smart" chat server which will exit when no more clients are left, in order to demonstrate socket activation
39+
*/
40+
public class SocketActivation extends WebSocketServer {
41+
42+
AtomicInteger clients = new AtomicInteger(0);
43+
44+
public SocketActivation(ServerSocketChannel chan) {
45+
super(chan);
46+
}
47+
48+
@Override
49+
public void onOpen(WebSocket conn, ClientHandshake handshake) {
50+
conn.send("Welcome to the server!"); //This method sends a message to the new client
51+
broadcast("new connection: " + handshake.getResourceDescriptor()); //This method sends a message to all clients connected
52+
if(clients.get() == 0) {
53+
broadcast("You are the first client to join");
54+
}
55+
System.out.println(conn.getRemoteSocketAddress().getAddress().getHostAddress() + " entered the room!");
56+
clients.incrementAndGet();
57+
}
58+
59+
@Override
60+
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
61+
broadcast(conn + " has left the room!");
62+
System.out.println(conn + " has left the room!");
63+
if(clients.decrementAndGet() <= 0) {
64+
System.out.println("No more clients left, exiting");
65+
System.exit(0);
66+
}
67+
}
68+
69+
@Override
70+
public void onMessage(WebSocket conn, String message) {
71+
broadcast(message);
72+
System.out.println(conn + ": " + message);
73+
}
74+
75+
@Override
76+
public void onMessage(WebSocket conn, ByteBuffer message) {
77+
broadcast(message.array());
78+
System.out.println(conn + ": " + message);
79+
}
80+
81+
82+
public static void main(String[] args) throws InterruptedException, IOException {
83+
if(System.inheritedChannel() == null) {
84+
System.err.println("System.inheritedChannel() is null, make sure this program is started with file descriptor zero being a listening socket");
85+
System.exit(1);
86+
}
87+
SocketActivation s = new SocketActivation((ServerSocketChannel)System.inheritedChannel());
88+
s.start();
89+
System.out.println(">>>> SocketActivation started on port: " + s.getPort() + " <<<<");
90+
}
91+
92+
@Override
93+
public void onError(WebSocket conn, Exception ex) {
94+
ex.printStackTrace();
95+
}
96+
97+
@Override
98+
public void onStart() {
99+
System.out.println("Server started!");
100+
}
101+
102+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[Unit]
2+
Description=Java-WebSocket systemd activation demo service
3+
After=network.target jws-activation.socket
4+
Requires=jws-activation.socket
5+
6+
[Service]
7+
Type=simple
8+
# Place the command for running SocketActivation.java in file "$HOME"/jws_activation_command:
9+
ExecStart=/bin/sh %h/jws_activation_run
10+
TimeoutStopSec=5
11+
StandardError=journal
12+
StandardOutput=journal
13+
# This is very important - systemd will pass the socket as file descriptor zero, which is what Java expects
14+
StandardInput=socket
15+
16+
[Install]
17+
WantedBy=default.target
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Unit]
2+
Description=Java-WebSocket systemd activation demo socket
3+
PartOf=jws-activation.service
4+
5+
[Socket]
6+
ListenStream=127.0.0.1:9999
7+
8+
[Install]
9+
WantedBy=sockets.target

0 commit comments

Comments
 (0)