Skip to content

Instantly share code, notes, and snippets.

@antic183
Last active March 24, 2022 18:01
Show Gist options
  • Save antic183/ff7cf756bc7d3ae95aeeb8a4878a41fc to your computer and use it in GitHub Desktop.
Save antic183/ff7cf756bc7d3ae95aeeb8a4878a41fc to your computer and use it in GitHub Desktop.
J2EE Websockets example. Use it with javascript client.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Websockets with javascript</title>
</head>
<body>
<p>Use Websockets with javascript. On server side work J2EE</p>
<script>
window.onload = function () {
var uri = "ws://localhost:8080/myEndpoint"; // ws -> not secure, wss (ssl)
var websocket = new WebSocket(uri);
// override Methode: when recive msg from server.
websocket.onmessage = function (msg) {
alert("msg recived from server: " + msg.data);
}
// override Methode: when server throw an error.
websocket.onerror = function (evt) {
alert("error from server: Can't establish a connection to the server!");
console.info(evt);
}
// override Methode: when server close the connection.
websocket.onclose = function () {
alert("Server close connection.");
}
// override Methode: when websocket connection was established
websocket.onopen = function () {
// send a msg to server
websocket.send("a msg from client side");
}
}
</script>
</body>
</html>
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>app</groupId>
<artifactId>websockets_example</artifactId>
<packaging>war</packaging>
<version>0.0.1</version>
<name>websockets_example Maven Webapp</name>
<properties>
<version.wildfly.swarm>2017.7.0</version.wildfly.swarm>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>bom-all</artifactId>
<version>${version.wildfly.swarm}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Java EE 7 dependency -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<!-- WildFly Swarm Fractions -->
<!-- ... -->
<!--jUnit-->
<!--<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>-->
<!--hamcrast-->
<!--<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>-->
<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-server -->
<!--<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/com.sun.jersey.contribs/jersey-multipart -->
<!--<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>1.9</version>
</dependency>-->
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<!--<dependency>-->
<!--<groupId>mysql</groupId>-->
<!--<artifactId>mysql-connector-java</artifactId>-->
<!--<version>5.1.39</version>-->
<!--</dependency>-->
<!-- https://mvnrepository.com/artifact/javax.websocket/javax.websocket-api -->
<!--<dependency>-->
<!--<groupId>javax.websocket</groupId>-->
<!--<artifactId>javax.websocket-api</artifactId>-->
<!--<version>1.1</version>-->
<!--</dependency>-->
</dependencies>
<build>
<finalName>websockets_example</finalName>
<plugins>
<plugin>
<groupId>org.wildfly.swarm</groupId>
<artifactId>wildfly-swarm-plugin</artifactId>
<version>${version.wildfly.swarm}</version>
<!-- favor IPv4Stack as IP6 can cause trobule on some operating systems and WildFly-Swarm -->
<!--ONLY FOR DOCKER-->
<!--<configuration>-->
<!--<properties>-->
<!--<java.net.preferIPv4Stack>true</java.net.preferIPv4Stack>-->
<!--</properties>-->
<!--</configuration>-->
<!--ONLY FOR DOCKER-->
<executions>
<execution>
<goals>
<goal>package</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package app;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
*
* J2EE Websockets example
*
*/
@ServerEndpoint("/myEndpoint")
public class MySocketEndpoint {
private static Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());
@OnOpen
public void onOpen(Session session) {
sessions.add(session);
System.out.println("on open");
}
@OnClose
public void onClose(Session session) {
sessions.remove(session);
System.out.println("on close");
}
@OnMessage
public void onReciveMsg(Session clientSession, String msg) throws InterruptedException {
System.out.println("on recive msg.");
Thread.sleep(5000);
for (Session session : sessions) {
session.getAsyncRemote().sendText("msg for all clients: " + msg);
}
Thread.sleep(5000);
clientSession.getAsyncRemote().sendText("your personal message from server");
Thread.sleep(5000);
clientSession.getAsyncRemote().sendText("your msg was: " + msg);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment