
io.jsync.sockjs.impl.EventSourceTransport Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsync.io Show documentation
Show all versions of jsync.io Show documentation
jsync.io is a non-blocking, event-driven networking framework for Java
/*
* Copyright (c) 2011-2013 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package io.jsync.sockjs.impl;
import io.jsync.Handler;
import io.jsync.buffer.Buffer;
import io.jsync.http.HttpServerRequest;
import io.jsync.http.RouteMatcher;
import io.jsync.impl.AsyncInternal;
import io.jsync.json.JsonObject;
import io.jsync.logging.Logger;
import io.jsync.logging.impl.LoggerFactory;
import io.jsync.sockjs.SockJSSocket;
import java.util.Map;
/**
* @author Tim Fox
*/
class EventSourceTransport extends BaseTransport {
private static final Logger log = LoggerFactory.getLogger(EventSourceTransport.class);
EventSourceTransport(AsyncInternal async, RouteMatcher rm, String basePath, Map sessions, final JsonObject config,
final Handler sockHandler) {
super(async, sessions, config);
String eventSourceRE = basePath + COMMON_PATH_ELEMENT_RE + "eventsource";
rm.getWithRegEx(eventSourceRE, new Handler() {
public void handle(final HttpServerRequest req) {
if (log.isTraceEnabled()) log.trace("EventSource transport, get: " + req.uri());
String sessionID = req.params().get("param0");
Session session = getSession(config.getLong("session_timeout"), config.getLong("heartbeat_period"), sessionID, sockHandler);
session.setInfo(req.localAddress(), req.remoteAddress(), req.uri(), req.headers());
session.register(new EventSourceListener(config.getInteger("max_bytes_streaming"), req, session));
}
});
}
private class EventSourceListener extends BaseListener {
final int maxBytesStreaming;
final HttpServerRequest req;
final Session session;
boolean headersWritten;
int bytesSent;
boolean closed;
EventSourceListener(int maxBytesStreaming, HttpServerRequest req, Session session) {
this.maxBytesStreaming = maxBytesStreaming;
this.req = req;
this.session = session;
addCloseHandler(req.response(), session);
}
public void sendFrame(String body) {
if (log.isTraceEnabled()) log.trace("EventSource, sending frame");
if (!headersWritten) {
req.response().headers().set("Content-Type", "text/event-stream; charset=UTF-8");
setNoCacheHeaders(req);
setJSESSIONID(config, req);
req.response().setChunked(true);
req.response().write("\r\n");
headersWritten = true;
}
StringBuilder sb = new StringBuilder();
sb.append("data: ");
sb.append(body);
sb.append("\r\n\r\n");
Buffer buff = new Buffer(sb.toString());
req.response().write(buff);
bytesSent += buff.length();
if (bytesSent >= maxBytesStreaming) {
if (log.isTraceEnabled()) log.trace("More than maxBytes sent so closing connection");
// Reset and close the connection
close();
}
}
public void close() {
if (!closed) {
try {
session.resetListener();
req.response().end();
req.response().close();
} catch (IllegalStateException e) {
// Underlying connection might already be closed - that's fine
}
closed = true;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy