Skip to content

Instantly share code, notes, and snippets.

@ybiquitous
Last active June 16, 2021 06:49
Show Gist options
  • Save ybiquitous/8129008 to your computer and use it in GitHub Desktop.
Save ybiquitous/8129008 to your computer and use it in GitHub Desktop.
JSR 356 (Java API for WebSocket) Example
# Project Directory Tree #
/
├── pom.xml
└── src
└── main
├── java
│   └── jsr356
│   └── example
│   └── EchoEndPoint.java
└── webapp
└── index.html
# Start Server #
Run command 'mvn jetty:run'
package jsr356.example;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Logger;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint(value = "/echo")
public class EchoEndPoint {
private static final Logger log = Logger.getLogger(EchoEndPoint.class.getName());
private static final Set<Session> sessions = new CopyOnWriteArraySet<>();
@OnOpen
public void onOpen(Session session) {
log.info("{" + session.getId() + "} connected");
sessions.add(session);
}
@OnClose
public void onClose(Session session) {
log.info("{" + session.getId() + "} closed");
sessions.remove(session);
}
@OnMessage
public void echo(String message) throws IOException {
for (Session session : sessions) {
if (session.isOpen()) {
log.info("{" + session.getId() + "} echo '" + message + "'");
session.getBasicRemote().sendText(message);
}
}
}
}
<!DOCTYPE html>
<html>
<head>
<title>Echo</title>
</head>
<body>
<input id="msg" type="text" />
<button id="echo">Echo</button>
<button id="close">Close</button>
<ul id="area"></ul>
<script>
var area = document.getElementById("area");
var msg = document.getElementById("msg");
var ws = new WebSocket("ws://localhost:8080/echo");
ws.onopen = function(event) {
console.log(event);
};
ws.onclose = function(event) {
console.log(event);
};
ws.onmessage = function(event) {
console.log(event);
var entry = document.createElement("li");
entry.textContent = event.data;
area.appendChild(entry);
}
ws.onerror = function(event) {
console.log(event);
}
document.getElementById("echo").onclick = function() {
ws.send(msg.value);
};
document.getElementById("close").onclick = function() {
ws.close();
};
</script>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jsr356-example</groupId>
<artifactId>jsr356-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<encoding>UTF-8</encoding>
<javaVersion>1.7</javaVersion>
<jettyVersion>9.1.0.v20131115</jettyVersion><!-- 9.1.0+ -->
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>javax.websocket</groupId>
<artifactId>javax.websocket-api</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${javaVersion}</source>
<target>${javaVersion}</target>
<compilerVersion>${javaVersion}</compilerVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jettyVersion}</version>
<configuration>
<reload>manual</reload>
<systemProperties>
<systemProperty>
<name>java.util.logging.SimpleFormatter.format</name>
<value>%1$tF %1$tT,%1$tL %4$s - %5$s%6$s%n</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment