4

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...).

c.censier
  • 781
  • 7
  • 23
  • 1
    It looks like this error is thrown only if JSON.parse fails to parse your string: https://github.com/meteor/meteor/blob/50e6d3143db8fc6e1fc3fb74da74b40c8dc7f3a4/packages/ddp/livedata_common.js#L105-L110. I also have no idea what is wrong with your message to be honest. – imslavko Dec 01 '14 at 09:54
  • It drive me crazy... I watch all DDP requests between my JS client and my server with the DDP Analyzer (https://meteorhacks.com/discover-meteor-ddp-in-realtime.html). The "connect" request is exactly the SAME as the one in C app. I wonder if it can be a header issue ? I don't know if I had to add header in the request ? – c.censier Dec 01 '14 at 10:22
  • If you are sending frames on a websocket connection, I too am confused. – imslavko Dec 01 '14 at 10:53
  • Okay... It's a shame but... the problem was the +1 added to the strlen(content). – c.censier Dec 01 '14 at 14:48

1 Answers1

1

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.

c.censier
  • 781
  • 7
  • 23
  • oh, that's really a shame, I thought about it for a moment but I would think the '\0' char would be printed to your console somehow. – imslavko Dec 01 '14 at 19:04