问题描述
我发展了两个客户端应用程序流星,一个是在JavaScript中,另一种是C.
实际上,我想我的C应用程序连接到使用的WebSocket服务器。我使用该库nopoll对于WebSocket的()和杨松的JSON序列化()。
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/).
我读了DDP规范(),这简短的(但好)的解释(的)。
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).
下面是code是WebSocket的初始化
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;
}
和连接到服务器的流星
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"}
我不明白为什么...我发送有效JSON和参照DDP DOC我做的事情做好(至少我是这么认为的......)。
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个字符太多比正常预期。现在,我得到一个:
The problem was I was sending 1 character too much than normally expected. Now, I get a :
{"msg":"connected","session":"HupHMhcFK4avy4vwg"}
要告诉我,我连接。
我发送的'\\ 0'和JSON解析器不承认它。
I was the sending the '\0' and the JSON parser don't recognize it.
这篇关于流星服务器和C之间的应用程序DDP Etablish连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!