4

我正在开发一个带有两个客户端的 Meteor 应用程序,一个在 JavaScript 中,另一个在 C 中。我实际上是在尝试使用 websocket 将我的 C 应用程序连接到服务器。我将库 nopoll 用于 websocket ( http://www.aspl.es/nopoll/html/index.html ) 和 jansson 用于 JSON 序列化 ( http://www.digip.org/jansson/ )。

我阅读了 DDP 规范(https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md)和这个简短(但很好)的解释(https://meteorhacks.com/introduction-to -ddp.html )。

这是代码是websocket初始化

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;
}

以及与 Meteor 服务器的连接

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);
}

我在服务器控制台上有这个错误:

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

我不明白为什么......我正在发送有效的 JSON 并参考 DDP 文档我做得很好(至少我认为是这样......)。

4

1 回答 1

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.

于 2014-12-01T15:08:46.267 回答