forked from payara/Payara-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
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 payara#37 from smillidge/master
Add a Websocket example
- Loading branch information
Showing
6 changed files
with
167 additions
and
1 deletion.
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
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. |
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,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> |
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,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> |
54 changes: 54 additions & 0 deletions
54
Java-EE/WebSockets/src/main/java/fish/payara/examples/websockets/EchoWebSocketServer.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,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); | ||
} | ||
} | ||
|
||
} |
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,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> |
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