• 2013-10-06

    Build a web chat app with icomet

    Views: 23584 | No Comments

    icomet(https://github.com/ideawu/icomet) is HTTP a server-push comet solution, it supports all popular browsers and OSes, including Safari/iOS(iPhone, iPad, iPod), Safari/Mac, IE/Windows, Chrome, Firefox/Mac, Windows.

    icomet is written in C++, built with libevent, and is totally open sourced.

    Building a web chat with icomet is very easy, the provided JavaScript client library is easy to use:

    var comet = new iComet({
        channel: 'abc',
        signUrl: 'http://127.0.0.1:8000/sign',
        subUrl: 'http://127.0.0.1:8100/sub',
        callback: function(msg){
            // on server push
            alert(msg.content);
        }
    });
    

    You can see the Live Demo here: http://www.ideawu.com/icomet/chat.html

    Posted by ideawu at 2013-10-06 14:41:02 Tags: , ,
  • 2013-09-17

    Notify libevent HTTP server on client close

    Views: 18658 | No Comments

    Although we can set a close callback on HTTP connection, but the HTTP server is not notified if client close unexpetedly, such as CTRL+C.

     evhttp_connection_set_closecb(req->evcon, on_close, NULL);
    

    Because libevent called bufferevent_disable(evon, EV_READ) to stop read on the connection, so the server would not know the client close by read() returns 0, or send() causes SIGPIPE.

    All you need to is to re-enable EV_READ on that connection:

    struct bufferevent *bev = evhttp_connection_get_bufferevent(req->evcon);
    bufferevent_enable(bev, EV_READ);
    
    Posted by ideawu at 2013-09-17 13:45:33 Tags:
  • 2013-09-11

    Libevent HTTP client example

    Views: 30296 | 4 Comments

    This is a simple HTTP client written in C with libevent2, the client makes a HTTP request to a web server, and print out the response HTML text. Here’s the codes:

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
    #include <evhttp.h>
    #include <event2/event.h>
    #include <event2/http.h>
    #include <event2/bufferevent.h>
    
    void http_request_done(struct evhttp_request *req, void *arg){
        char buf[1024];
        int s = evbuffer_remove(req->input_buffer, &buf, sizeof(buf) - 1);
        buf[s] = '\0';
        printf("%s", buf);
        // terminate event_base_dispatch()
        event_base_loopbreak((struct event_base *)arg);
    }
    
    int main(int argc, char **argv){
        struct event_base *base;
        struct evhttp_connection *conn;
        struct evhttp_request *req;
    
        base = event_base_new();
        conn = evhttp_connection_base_new(base, NULL, "127.0.0.1", 8080);
        req = evhttp_request_new(http_request_done, base);
    
        evhttp_add_header(req->output_headers, "Host", "localhost");
        //evhttp_add_header(req->output_headers, "Connection", "close");
    
        evhttp_make_request(conn, req, EVHTTP_REQ_GET, "/index.php?id=1");
        evhttp_connection_set_timeout(req->evcon, 600);
        event_base_dispatch(base);
    
        return 0;
    }
    
    Posted by ideawu at 2013-09-11 21:55:59 Tags:
|<<<1>>>| 1/1 Pages, 3 Results.