Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ideawu committed May 13, 2016
1 parent ad58735 commit 1f015f6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
13 changes: 7 additions & 6 deletions demo/web/js/icomet.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ function iComet(config){
if(msg instanceof Array){
self.log('batch response', msg.length);
for(var i in msg){
if(msg[i] && msg[i].type == 'data'){
if(i == msg.length - 1){
window[self.cb](msg[i]);
}else{
window[self.cb](msg[i], true);
}
if(!msg){
continue;
}
if(i == msg.length - 1){
window[self.cb](msg[i]);
}else{
window[self.cb](msg[i], true);
}
}
return;
Expand Down
24 changes: 15 additions & 9 deletions src/comet/subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,21 @@ void Subscriber::sync_next_seq(){
this->send_chunk(seq_next, "next_seq", NULL);
}

void Subscriber::poll_send_start(){
void Subscriber::poll_send_start(bool array){
struct evbuffer *buf = evhttp_request_get_output_buffer(this->req);
if(!this->callback.empty()){
evbuffer_add_printf(buf, "%s(", this->callback.c_str());
}
evbuffer_add_printf(buf, "[");
if(array){
evbuffer_add_printf(buf, "[");
}
}

void Subscriber::poll_send_end(){
void Subscriber::poll_send_end(bool array){
struct evbuffer *buf = evhttp_request_get_output_buffer(this->req);
evbuffer_add_printf(buf, "null]");
if(array){
evbuffer_add_printf(buf, "]");
}
if(!this->callback.empty()){
evbuffer_add_printf(buf, ");");
}
Expand All @@ -122,7 +126,7 @@ void Subscriber::poll_send_end(){
this->close();
}

void Subscriber::poll_send(int seq, const char *type, const char *content){
void Subscriber::poll_send(int seq, const char *type, const char *content, bool array){
this->idle = 0;
if(strcmp(type, "data") == 0 || strcmp(type, "broadcast") == 0){
this->seq_next = seq + 1;
Expand All @@ -138,7 +142,9 @@ void Subscriber::poll_send(int seq, const char *type, const char *content){
this->channel->name.c_str(),
seq,
content);
evbuffer_add(buf, ",", 1);
if(array){
evbuffer_add(buf, ",", 1);
}

if(strcmp(type, "broadcast") == 0){
this->poll_send(this->seq_next, "next_seq", "");
Expand All @@ -147,9 +153,9 @@ void Subscriber::poll_send(int seq, const char *type, const char *content){

void Subscriber::send_chunk(int seq, const char *type, const char *content){
if(this->type == POLL){
this->poll_send_start();
this->poll_send(seq, type, content);
this->poll_send_end();
this->poll_send_start(false);
this->poll_send(seq, type, content, false);
this->poll_send_end(false);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/comet/subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ class Subscriber{
static void send_error_reply(int sub_type, struct evhttp_request *req, const char *cb, const std::string &cname, const char *type, const char *content);

private:
void poll_send_start();
void poll_send_end();
void poll_send(int seq, const char *type, const char *content);
void poll_send_start(bool array=true);
void poll_send_end(bool array=true);
void poll_send(int seq, const char *type, const char *content, bool array=true);
};

#endif

0 comments on commit 1f015f6

Please sign in to comment.