Loading [MathJax]/extensions/tex2jax.js

2014年4月13日日曜日

Jetty9.1でWebSocket(JSR365)を使ってみる

一年ぐらい前に書いたJetty9でWebSocketを使ってみる。だとWebSocketのJavaEE仕様であるJSR365には対応できていませんでした。Jetty9.1ではJSR365に対応したようでしたので、それに準拠するように書き直しました。あまりソースはあまり変わりませんが少しシンプルになった感じがします。pom.xmlはgradleを使おうとしたのですがgradleのjetty pluginがバージョン7以降に対応できていないようです。(update jetty plugin to current jetty)

<!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>
view raw index.html hosted with ❤ by GitHub
<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>
view raw pom.xml hosted with ❤ by GitHub
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());
}
}