-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from 12urenloop/ws-endpoint
WebSocket endpoint
- Loading branch information
Showing
5 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package telraam.websocket; | ||
|
||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
import org.eclipse.jetty.websocket.api.Session; | ||
import org.eclipse.jetty.websocket.api.WebSocketAdapter; | ||
|
||
public class WebSocketConnection extends WebSocketAdapter { | ||
private static final Logger logger = Logger.getLogger(WebSocketConnection.class.getName()); | ||
|
||
@Override | ||
public void onWebSocketConnect(Session session) { | ||
super.onWebSocketConnect(session); | ||
WebSocketMessageSingleton.getInstance().registerConnection(this); | ||
logger.info("Instance with remote \"%s\" connected".formatted(getRemote().getRemoteAddress())); | ||
} | ||
|
||
@Override | ||
public void onWebSocketClose(int statusCode, String reason) { | ||
super.onWebSocketClose(statusCode, reason); | ||
WebSocketMessageSingleton.getInstance().unregisterConnection(this); | ||
logger.info("Instance with remote \"%s\" closed: [%s] %s".formatted(getRemote().getRemoteAddress(), statusCode, reason)); | ||
} | ||
|
||
@Override | ||
public void onWebSocketError(Throwable cause) { | ||
super.onWebSocketError(cause); | ||
logger.severe("WebSocket error in instance with remote \"%s\": %s".formatted(getRemote().getRemoteAddress(), cause)); | ||
} | ||
|
||
public void send(String s) { | ||
try { | ||
getRemote().sendString(s); | ||
} catch (IOException e) { | ||
logger.severe("Sending \"%s\" through instance with remote \"%s\" failed with %s".formatted(s, getRemote().getRemoteAddress(), e)); | ||
return; | ||
} | ||
logger.finest("Sent \"%s\" through instance with remote \"%s\"".formatted(s, getRemote().getRemoteAddress())); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/telraam/websocket/WebSocketMessageSingleton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package telraam.websocket; | ||
|
||
import lombok.Getter; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
|
||
public class WebSocketMessageSingleton { | ||
private static final Logger logger = Logger.getLogger(WebSocketMessageSingleton.class.getName()); | ||
|
||
@Getter | ||
private static final WebSocketMessageSingleton instance = new WebSocketMessageSingleton(); | ||
private static final Set<WebSocketConnection> registeredConnections = new HashSet<>(); | ||
|
||
private WebSocketMessageSingleton() { | ||
} | ||
|
||
public void registerConnection(WebSocketConnection conn) { | ||
boolean modified = registeredConnections.add(conn); | ||
if (modified) { | ||
logger.info("Registered WebSocketConnection %s".formatted(conn)); | ||
} | ||
} | ||
|
||
public void unregisterConnection(WebSocketConnection conn) { | ||
boolean modified = registeredConnections.remove(conn); | ||
if (modified) { | ||
logger.info("Unregistered WebSocketConnection %s".formatted(conn)); | ||
} | ||
} | ||
|
||
public void sendToAll(String s) { | ||
logger.finest("Sending \"%s\" to all registered WebSocketConnection instances".formatted(s)); | ||
registeredConnections.forEach(conn -> conn.send(s)); | ||
} | ||
} |