作ったのは これ。
作ったサーバでシンプルなEchoサーバとクライアントも作ってみたのがこちら。。
まだWebSocketを対応してるのがChromeしかないので、当然Chromeしか動作しない。
EchoServer.java
package websocket.echoserver;
import java.io.IOException;
import com.babukuma.commons.websocket.WebSocketServer;
public class EchoServer {
public static void main(String[] args) throws IOException {
new WebSocketServer(8888, "/EchoServer", new EchoHandlerFactory()).start();
}
}
EchoHandlerFactory.java
package websocket.echoserver;
import com.babukuma.commons.websocket.WebSocketHandler;
import com.babukuma.commons.websocket.WebSocketHandlerFactory;
public class EchoHandlerFactory implements WebSocketHandlerFactory {
@Override
public WebSocketHandler createHandler(String urlPath) {
return new EchoHandler();
}
@Override
public void removeHandler(String urlPath, WebSocketHandler handler) {
// TODO Auto-generated method stub
}
}
EchoHandler.java
package websocket.echoserver;
import com.babukuma.commons.websocket.SimpleWebSocketHandler;
public class EchoHandler extends SimpleWebSocketHandler {
@Override
public void onMessage(String message) {
send(message);
}
@Override
public void onClose() {
System.out.println("close");
}
@Override
public void onError(Throwable e) {
e.printStackTrace();
}
@Override
public void onOpen() {
System.out.println("open");
}
}
EchoClient.html
省略。。。
<script>
var ws = new WebSocket("ws://localhost:8888/EchoServer/echo");
ws.onclose = function(event) {
alert("websocket " + event + " is closed");
};
ws.onopen = function(event) {
alert("websocket " + event + " is opened");
};
ws.onmessage = function(event) {
var chatarea = document.getElementById("chatarea");
chatarea.value = chatarea.value + "\n" + event.data;
};
function send() {
var msg = document.getElementById("msg");
ws.send(msg.value);
msg.value = '';
return false;
}
</script>
省略。。。