Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Supports Server Sent Events (SSE) #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xfeep opened this issue Aug 22, 2014 · 2 comments
Closed

Supports Server Sent Events (SSE) #41

xfeep opened this issue Aug 22, 2014 · 2 comments
Labels
Milestone

Comments

@xfeep
Copy link
Member

xfeep commented Aug 22, 2014

More details can be found from http://www.w3.org/TR/eventsource/ .
We'll provide related APIs to support server side SSE.

@xfeep xfeep added this to the 0.2.5 milestone Aug 22, 2014
@xfeep xfeep added the feature label Aug 22, 2014
@xfeep
Copy link
Member Author

xfeep commented Aug 25, 2014

Learn from Go lang Hijack, the APIs of long polling (#36) & Server Sent Events (SSE) will have the consistent form.

For Clojure

(defn hijack! 
  "Hijack a nginx request and return a server channel.
   After being hijacked, the ring handler's result will be ignored.
   If ignore-nginx-filter? is true all data output to channel won't be filtered
   by any nginx HTTP header/body filters such as gzip filter, chucked filter, etc.
   We can use this function to implement long poll / Server Sent Events (SSE) easily."
  [^NginxRequest req ignore-nginx-filter?])

(defprotocol HttpServerChannel
  (close! [ch] 
    "Asynchronously close the channel. If there's remaining data to send it won't block 
     current thread  and will flush data on the background asynchronously and later close the channel safely")
  (send! [ch data flush? last?]
    "Asynchronously send data to channel without blocking current thread. 
     data can be byte[], String, or ByteBuffer
     If flush? is false it will put data into buffers chain eitherwise it will write data to network.
     If close? is true it will close channel after all data are sent.
    ")
  (send-header! [ch status headers flush? last?] 
    "Asynchronously send HTTP status & headers to channel.
     status is a integer for HTTP status code, headers is a HTTP headers map.
     If flush? is false it will put data into buffers chain eitherwise it will write data to network.
     If close? is true it will close channel after all data are sent.")
  (send-response! [ch resp]
    "Asynchronously send a complete HTTP response to channel and close channel after all data are sent.
     resp is a ring Response Map, e.g. {:status 200, headers {\"Content-Type\" \"text/html\"}, :body \"Hello, Nginx-Clojure!\" } .
     ")
  (on-close! [ch attachment listener]
    "Add a close event listener.
     attachement is a  object which will be passed to listener when close event happens
     listener is a function like (fn[attachement] ... )")
  )

For Java/Groovy

public interface NginxHandler {
        /*
         * If ignoreFilter is true all data output to channel won't be filtered
   by any nginx HTTP header/body filters such as gzip filter, chucked filter, etc.
         */
    public NginxHttpServerChannel hijack(NginxRequest req, boolean ignoreFilter);

}
public class NginxHttpServerChannel  {

    public <T> void addListener(ChannelListener<T> listener, T data) ;

    /**
     * If message is null when flush is true it will do flush, when last is true it will close channel.
     */
    public void send(byte[] message, int off, int len, boolean flush, boolean last) ;

    public void send(String message, boolean flush, boolean last) ;

    public void send(ByteBuffer message, boolean flush, boolean last) ;

    public <K, V> void sendHeader(long status, Collection<Map.Entry<K, V>> headers, boolean flush, boolean last) ;

    public void sendResponse(Object resp) ;

    public void close() ;

@xfeep
Copy link
Member Author

xfeep commented Aug 27, 2014

more comments about on-close!

 A close event will happen immediately when channel is closed by either of these three cases:
 (1) channel close function/method is invoked on this channel, e.g. (close! ch)
 (2) inner unrecoverable error happens with this channel, e.g. not enough memory to read/write
 (3) remote client connection is closed or broken.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant