Firefox OS 中的 TCP 客户端.服务器无响应 [英] TCP client in Firefox OS. No response from the server

查看:38
本文介绍了Firefox OS 中的 TCP 客户端.服务器无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 Firefox OS 开发一个应用程序,它应该通过 TCP 连接与服务器进行通信.你可以看到下面的代码(唯一的区别是我用变量名替换了实际的 ip 地址和端口,并排除了 loginButes 的内容(实际上,那里正好有 24 个字节)).问题是我只能在控制台中看到 "Sent successfully" .所以我根本没有从服务器获得任何响应.据我了解,这个问题可能有两个可能的原因:要么是我为了连接而使用的数据不正确,要么是我试图从服务器接收响应的方式是错误的.假设数据一切正常.我的代码是否应该收到响应?(即,如果服务器向我发送响应,应该执行 socket.ondata)

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. 感谢@DavidHoldeman 的回答,我摆脱了最初的问题并解决了另一个问题.我现在发送数据时收到 未捕获的异常:内存不足".您能否提出该错误的可能原因?

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?

推荐答案

传递给 mozTCPSocket.open 的 'options' 参数应该是一个对象:

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!

这篇关于Firefox OS 中的 TCP 客户端.服务器无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆