Skip to content

Commit

Permalink
Merge pull request payara#37 from smillidge/master
Browse files Browse the repository at this point in the history
Add a Websocket example
  • Loading branch information
smillidge authored Aug 9, 2016
2 parents 50f8fe3 + 673bf51 commit 7b0a100
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Java-EE/WebSockets/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#Simple Websocket example

This example shows a simple echo web socket endpoint.

Messages sent from the browser will be echoed back by the server.

After building deploy the war file and browse to the root context.
19 changes: 19 additions & 0 deletions Java-EE/WebSockets/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project-shared-configuration>
<!--
This file contains additional configuration written by modules in the NetBeans IDE.
The configuration is intended to be shared among all the users of project and
therefore it is assumed to be part of version control checkout.
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
-->
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
<!--
Properties that influence various parts of the IDE, especially code formatting and the like.
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
That way multiple projects can share the same settings (useful for formatting rules for example).
Any value defined here will override the pom.xml file value but is only applicable to the current project.
-->
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>1.7-web</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_j2eeVersion>
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
</properties>
</project-shared-configuration>
39 changes: 39 additions & 0 deletions Java-EE/WebSockets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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>
<parent>
<artifactId>Java-EE</artifactId>
<groupId>fish.payara.examples</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

<groupId>fish.payara.examples</groupId>
<artifactId>WebSockets</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>WebSockets</name>

<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fish.payara.examples.websockets;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.ApplicationScoped;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

/**
*
* @author steve
*/
@ApplicationScoped
@ServerEndpoint("/echo")
public class EchoWebSocketServer {

private static final Logger logger = Logger.getLogger(EchoWebSocketServer.class.getName());

@OnOpen
public void onOpen(Session session) {
logger.log(Level.INFO, "Opening Session {0}", session.getId());
}

@OnClose
public void onClose(Session session) {
logger.log(Level.INFO, "Closing Session {0}", session.getId());
}

@OnError
public void onError(Throwable t) {
logger.log(Level.INFO,"onError",t);
}

@OnMessage
public void onMessage(String message, Session session) {
try {
logger.log(Level.INFO, "Received Message on Session {0}", session.getId());
session.getBasicRemote().sendText(message);
} catch (IOException ex) {
logger.log(Level.SEVERE, null, ex);
}
}

}
46 changes: 46 additions & 0 deletions Java-EE/WebSockets/src/main/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript">

var ws = new WebSocket("ws://"+window.location.host+"/WebSockets-1.0-SNAPSHOT/echo");

ws.onopen = function() {
var messageArea = document.getElementById("messages");
messageArea.value += "Socket Opened\r\n";
}

ws.onmessage = function(evt) {
var messageArea = document.getElementById("messages");
messageArea.value += evt.data + "\r\n";
}

ws.onclose = function() {
var messageArea = document.getElementById("messages");
messageArea.value += "Socket Closed\r\n";
}

function sendMessage() {
var message = document.getElementById("wsmessage").value;
ws.send(message);
}

</script>

</head>
<body>

<h1>Web Sockets Example</h1>
Send a simple message to the server and see it echoed back
<div>
<input type="text" id="wsmessage"/>
<button type="button" onclick="sendMessage();">Send</button>
</div>
<div>
<textarea id="messages" cols="60" rows="10"></textarea>
</div>

</body>
</html>
3 changes: 2 additions & 1 deletion Java-EE/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ file and include the License file at packager/legal/LICENSE.txt.
<module>simple-jms-example</module>
<module>simple-rest-example</module>
<module>jbatch-schedule</module>
<module>WebSockets</module>
</modules>
<dependencies>
<dependency>
Expand All @@ -44,4 +45,4 @@ file and include the License file at packager/legal/LICENSE.txt.
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>

0 comments on commit 7b0a100

Please sign in to comment.