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/WebSocketServlet"); | |
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>JettyWebSocketChat</artifactId> | |
<packaging>war</packaging> | |
<version>0.0.1-SNAPSHOT</version> | |
<name>JettyWebsocketChat</name> | |
<url>http://maven.apache.org</url> | |
<properties> | |
<jettyVersion>9.0.3.v20130506</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> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-war-plugin</artifactId> | |
<version>2.1.1</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.util.Collections; | |
import java.util.HashSet; | |
import java.util.Set; | |
import org.eclipse.jetty.websocket.api.Session; | |
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; | |
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; | |
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; | |
import org.eclipse.jetty.websocket.api.annotations.WebSocket; | |
@WebSocket | |
public class WebSocketListener { | |
static Set<Session> sessions = Collections.synchronizedSet(new HashSet()); | |
@OnWebSocketConnect | |
public void onConnect(Session session) { | |
System.out.println("MyWebSocket.onConnect()"); | |
sessions.add(session); | |
} | |
@OnWebSocketMessage | |
public void onText(String msg) { | |
System.out.println("MyWebSocket.onText()"); | |
System.out.println(msg); | |
for(Session session : sessions){ | |
session.getRemote().sendStringByFuture(msg); | |
} | |
} | |
@OnWebSocketClose | |
public void onClose(Session session, int statusCode, String reason) { | |
System.out.println("MyWebSocket.onClose()"); | |
sessions.remove(session); | |
} | |
} |
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 javax.servlet.annotation.WebServlet; | |
import org.eclipse.jetty.websocket.servlet.WebSocketServlet; | |
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; | |
@WebServlet("/WebSocketServlet") | |
public class WebSocketServletImpl extends WebSocketServlet{ | |
private static final long serialVersionUID = 1L; | |
@Override | |
public void configure(WebSocketServletFactory factory) { | |
factory.register(WebSocketListener.class); | |
} | |
} |