Etablish DDP connection between Meteor Server and

2019-05-07 14:50发布

I'm developing a Meteor application with two client, one is in JavaScript and the other one is in C. I'm actually trying to connect my C app to the server using websocket. I'm using the library nopoll for the websocket (http://www.aspl.es/nopoll/html/index.html) and jansson for the JSON serialization (http://www.digip.org/jansson/).

I read the DDP Specification (https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md) and this brief (but good) explanation (https://meteorhacks.com/introduction-to-ddp.html).

Here is the code is the websocket initialization

int main(int ac, char** av)
{
  // Create noPoll context
    noPollCtx* ctx = nopoll_ctx_new();
    if (! ctx)
  {
  puts("Error creating nopoll context");
  return EXIT_FAILURE;
  }
  puts("Context created");

  // Create connection
  noPollConn* conn = nopoll_conn_new(ctx, "localhost", "3000", NULL, "/websocket", NULL, NULL);
  if (! nopoll_conn_is_ok(conn))
  {
    puts("Error creating new connection");
    return EXIT_FAILURE;
  }
  puts("Connection created");

  // Wait until connection is ready
  if (! nopoll_conn_wait_until_connection_ready(conn, 5))
  {
     puts("Connection timeout");
     return EXIT_FAILURE;
  }
  puts("Connection ready");
  connection_to_DDP_server(conn);
  send_msg_loop(conn);
  nopoll_ctx_unref(ctx);
  return EXIT_SUCCESS;
}

And the connection to the Meteor server

void connection_to_DDP_server(noPollConn* conn)
{
  int ret = 0;
  json_t* connect = json_pack("{s:s,s:s,s:[s]}",
        "msg", "connect",
        "version", "1",
        "support", "1");
  char* content = json_dumps(connect, JSON_COMPACT);
  printf("DDP Connect - JSON string = %s\n", content);
  ret = nopoll_conn_send_text(conn, content, strlen(content) + 1);
  if (ret == -1)
  {
    puts("DDP Connect fail");
    exit(EXIT_FAILURE);
  }
  printf("%i bytes written\n", ret);
}

I have this error on the server console :

I20141201-08:54:13.498(1)? Discarding message with invalid JSON
{"msg":"connect","support":["1"],"version":"1"}

I don't understand why... I am sending valid JSON and referring to the DDP doc I am doing things well (at least I think so...).

1条回答
Deceive 欺骗
2楼-- · 2019-05-07 15:24

The problem was I was sending 1 character too much than normally expected. Now, I get a :

{"msg":"connected","session":"HupHMhcFK4avy4vwg"}

to tell me that i'm connected.

I was the sending the '\0' and the JSON parser don't recognize it.

查看更多
登录 后发表回答