我正在尝试使用服务器端的jansson lib形成一个json Response 以下是用于对客户端发出的请求进行json响应的代码片段(用js编写)
json_object_set(root,"response",msgType);
json_object_set_new(msgType,"reqID",json_string(reqId));
json_object_set_new(msgType,"method",json_string(metName));
json_object_set_new(msgType,"speakID",json_string(reqId));
json_object_set_new(msgType,"numSegments",json_integer(1));
char * jsonStringResponse = json_dumps(root, 0 );
mg_websocket_write(connecion, 1, jsonStringResponse, strlen(jsonStringResponse));
在变量jsonStringResponse
中形成这个{"response":{"method":"Speak","reqID":"30","speakID":"30","numSegments":"1"}}
现在在客户端实现,这是验证它的方式,我没有通过此验证。
// test the Speak method
it('Speak', function(done) {
var id = "123";
var method = "Speak";
WsTestBase.send({
"request":
{
"method": method,
"reqID": id,
"parameters":
{
"text" : ttsText
}
}
});
WsTestBase.validate({
"method": method,
"reqID":id,
"speakID":id,
"numSegments":1
},[
{ eventName : 'SpeechEnd', speakID : id }
], done);
});
请告诉我如何发送预期的eventName以及我的响应正文中缺少的eventName
答案 0 :(得分:0)
我不完全确定你要做什么,但WebSockets是异步的。
这是我使用asynchronous unit tests使用WebSockets进行测试的方法:
var ws = new WebSocket("wss://localhost:8006/", "text");
describe("Connects: ", function () {
var connected = false;
it("Connected", function () {
runs(function () {
ws.onopen = function (event) {
connected = true;
};
});
waitsFor(function () {
return connected;
}, "Error", 1000);
runs(function () {
expect(connected).toBe(true);
});
});
});
describe("Small Strings: ", function () {
var result = '';
it("Must reverse the 'Hi' string", function () {
ws.onmessage = function (event) {
result = event.data;
};
runs(function () {
ws.send("Hi");
});
waitsFor(function () {
return result == 'iH';
}, "Error", 1000);
runs(function () {
expect(result).toBe('iH');
});
});
});