TCP client in Firefox OS. No response from the ser

2019-09-09 09:10发布

问题:

I'm developing an app for Firefox OS which should communicate with the server via TCP connection. You can see the code below (the only difference is that I substituted actual ip address and port with variable names and excluded the content of loginButes (actually, there is exactly 24 bytes there)). The problem is that I can see only "Sent successfully" in console. So I don't obtain any response from the server at all. As far as I understand, there might be two possible reasons of this issue: either the data, I'm using in order to connect, is incorrect or the way, I'm trying to receive the response from the server, is wrong. Let's suppose that everything is fine with the data. Should my code receive a response or not? (i.e. should socket.ondata be executed if server sends me smth in response)

(function() {
    var options = {binaryType='arraybuffer'};    
    var socket = navigator.mozTCPSocket.open(ip, port, options);

    sendButton.addEventListener('click', function() {
        var loginButes = [];
        var Int8View = new Uint8Array(loginBytes);

        socket.ondata = function(event) {
            console.log(event.data);
            console.log("Received successfully");
        }
        socket.onerror = function(event) {
            console.log("Everything is bad");
        }

        socket.send(Int8View);
        console.log("Sent successfully");
    });
})();

P.S. Thanks to the @DavidHoldeman's answer, I got rid of the initial issue and came to another one. I get "uncaught exception: out of memory" when sending the data now. Could you please suggest what might be the reason of that error?

回答1:

The 'options' argument passed to mozTCPSocket.open should be an object:

var options = { binaryType: 'arraybuffer' };
var socket = navigator.mozTCPSocket.open(ip, port, options);

This tripped me up the first time too. Good luck and happy coding!