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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>WebSocket Chat</title> | |
<script type="text/javascript"> | |
var ws = new WebSocket("ws://localhost:8080/WebSocketEndpoint"); | |
ws.onmessage = function(msg) { | |
console.log("msg received" + msg.data); | |
var log = document.getElementById("log"); | |
log.innerHTML += msg.data; | |
}; | |
function send(){ | |
var msg = document.getElementById("msg"); | |
ws.send(msg.value + "<br>"); | |
msg.value = ""; | |
} | |
</script> | |
</head> | |
<body> | |
<h1>WebSocket Chat</h1> | |
<div id="log"></div> | |
<input id="msg" type="text"/> | |
<button id="send" onclick="send();">send</button> | |
</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
<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>com.sanofc</groupId> | |
<artifactId>JettyWebSocket</artifactId> | |
<packaging>war</packaging> | |
<version>1.0</version> | |
<name>JettyWebSocket</name> | |
<properties> | |
<jettyVersion>9.1.3.v20140225</jettyVersion> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-maven-plugin</artifactId> | |
<version>${jettyVersion}</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>JettyTest</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.7</source> | |
<target>1.7</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.4</version> | |
<configuration> | |
<failOnMissingWebXml>false</failOnMissingWebXml> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.eclipse.jetty</groupId> | |
<artifactId>jetty-maven-plugin</artifactId> | |
<version>${jettyVersion}</version> | |
<configuration> | |
<scanIntervalSeconds>1</scanIntervalSeconds> | |
<webApp> | |
<contextPath>/</contextPath> | |
</webApp> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
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
package com.sanofc; | |
import java.io.IOException; | |
import java.util.Collections; | |
import java.util.HashSet; | |
import java.util.Set; | |
import javax.websocket.CloseReason; | |
import javax.websocket.OnClose; | |
import javax.websocket.OnMessage; | |
import javax.websocket.OnOpen; | |
import javax.websocket.Session; | |
import javax.websocket.server.ServerEndpoint; | |
@ServerEndpoint("/WebSocketEndpoint") | |
public class WebSocketEndpoint { | |
static Set<Session> sessions = Collections.synchronizedSet(new HashSet()); | |
@OnOpen | |
public void onOpen(Session session) { | |
System.out.println("WebSocket opened:" + session.getId()); | |
sessions.add(session); | |
} | |
@OnMessage | |
public void onMessage(String message) { | |
System.out.println("WebSocket received message:" + message); | |
for (Session session : sessions) { | |
try { | |
session.getBasicRemote().sendText(message); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@OnClose | |
public void onClose(CloseReason reason) { | |
System.out.println("Closing a WebSocket due to " + reason.getReasonPhrase()); | |
} | |
} |