【问题标题】:Send Events in JSON format via websockets通过 websockets 以 JSON 格式发送事件
【发布时间】:2014-04-29 10:45:22
【问题描述】:

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

【问题讨论】:

    标签: javascript json websocket jasmine dom-events


    【解决方案1】:

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

    这就是我使用 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');
            });
        });
    });
    

    【讨论】:

    • 该链接很有帮助。谢谢 !!你刚刚说对了。我正在实施项目的服务器端。客户端,正如您所提到的那样,会建立一个 websocket 连接并向我发送您所看到的请求。我以 json 形式解析并回溯响应。现在我的问题是,发送预期的事件名称。我如何发送它?在函数 validate() 中的 [] 括号中是必需的
    • 哦,你想知道服务端如何实现吗?
    • 是的。我想从服务器发送响应。是的,我正在实现服务器端
    猜你喜欢
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-17
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    相关资源
    最近更新 更多