通过 websocket 以 JSON 格式发送事件



我正在尝试在服务器端使用 jansson lib 形成 JSON 响应下面是代码片段,用于构建对客户端发出的请求的 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

我不完全确定你想做什么,但 WebSocket 是异步的。

这就是我如何使用异步单元测试使用 WebSocket 进行测试的方式:

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

相关内容

  • 没有找到相关文章

最新更新