package com.sohu.common.server.webbit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.webbitserver.EventSourceConnection;
import org.webbitserver.EventSourceHandler;
import org.webbitserver.EventSourceMessage;
import org.webbitserver.WebServer;
import org.webbitserver.WebServers;
import org.webbitserver.handler.EmbeddedResourceHandler;
/**
* EventSource server端
* @author shaojieyue
* @date 2013-08-28 14:53:23
*/
public class EventSourceServer {
public static class Pusher {
private List<EventSourceConnection> connections = new ArrayList<EventSourceConnection>();
private int count = 1;
public void addConnection(EventSourceConnection connection) {
connection.data("id", count++);
connections.add(connection);
broadcast("Client " + connection.data("id") + " joined");
}
public void removeConnection(EventSourceConnection connection) {
connections.remove(connection);
broadcast("Client " + connection.data("id") + " left");
}
public void pushPeriodicallyOn(ExecutorService webThread)
throws InterruptedException, ExecutionException {
while (true) {
Thread.sleep(1000);
webThread.submit(new Runnable() {
@Override
public void run() {
broadcast(new Date().toString());
}
}).get();
}
}
/**
* 廣播消息
* @param message
*/
private void broadcast(String message) {
for (EventSourceConnection connection : connections) {
connection.send(new EventSourceMessage(message));
}
}
}
public static void main(String[] args) throws Exception {
ExecutorService webThread = Executors.newSingleThreadExecutor();
final Pusher pusher = new Pusher();
WebServer webServer = WebServers
.createWebServer(webThread, 8080)
.add("/events", new EventSourceHandler() {
@Override
public void onOpen(EventSourceConnection connection)
throws Exception {
System.out.println("--->add conn "+connection);
pusher.addConnection(connection);
}
@Override
public void onClose(EventSourceConnection connection)
throws Exception {
System.out.println("--->remove conn "+connection);
pusher.removeConnection(connection);
}
})
/*內置資源例如com/sohu/common/server/webbit/index.html 的訪問方式爲
* http://localhost:8080/index.html
*/
.add(new EmbeddedResourceHandler("com/sohu/common/server/webbit"))
.start().get();
System.out.println("EventSource demo running on: " + webServer.getUri());
pusher.pushPeriodicallyOn(webThread);
}
}
<!DOCTYPE html>
<html>
<head>
<title>EventSource Demo</title>
<script type="text/javascript">
function logText(msg) {
var textArea = document.getElementById('data');
textArea.value = textArea.value + msg + '\n';
textArea.scrollTop = textArea.scrollHeight; // scroll into view
}
var es = new EventSource('http://localhost:8080/events');
es.onopen = function() {
logText('OPEN');
};
es.onmessage = function(event) {
logText(event.data);
};
es.onerror = function() {
logText('ERROR');
};
</script>
</head>
<body>
<textarea id="data" rows="20" cols="30"></textarea>
</body>
</html>