简体   繁体   English

在JavaScript中使用WebSocket时出错

[英]Error Using WebSocket in JavaScript

I am currently working on a mobile app created with Cordova. 我目前正在使用Cordova创建一个移动应用程序。 It uses sockets to connect to another device of ours to communicate with it. 它使用套接字连接到我们的另一设备以与其通信。 We currently use chrome.sockets.tcp with this plugin . 目前,我们将此插件与chrome.sockets.tcp一起使用。

It connects fine, however on Android devices has a problem disconnecting. 可以正常连接,但是在Android设备上无法连接。 It's not a timeout issue as the disconnect can happen anywhere from 20 to 30 seconds up to 10 to 15 minutes. 这不是超时问题,因为断开连接可能在20到30秒到10到15分钟的任何时间发生。 It will even disconnect while I am downloading data making 2 to 3 requests every second. 每秒下载2到3个请求的数据时,它甚至会断开连接。 There are no errors and nothing in the log to indicate what happened. 没有错误,日志中也没有任何内容可以指示发生了什么。 It appears that chrome.sockets.tcp does not have much in the way of connection status reporting so I can figure out why its dropping the connection. 似乎chrome.sockets.tcp的连接状态报告方式不多,因此我可以弄清楚为什么它会断开连接。

So I tried to make the switch to JavaScript's WebSocket to see if it helps with the disconnect issue. 因此,我尝试切换到JavaScript的WebSocket来查看它是否有助于解决断开连接问题。 Or at least it appears to provide more connection status information. 或者至少它似乎提供了更多的连接状态信息。 But I can't get it to connect. 但是我无法连接它。 I get Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE . Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE Here is my code: 这是我的代码:

var socket = new WebSocket("ws://" + IP + ":" + port);
socket.protocol = "soap";

socket.onclose = function(event) {
    console.log(event);
};
socket.onerror = function(event) {
    console.log(event);
};
socket.onmessage = function(event) {
    console.log(event);
};
socket.onopen = function(event) {
    console.log(event);
};

I've done some research and it seems that I might have to set a protocol using socket.protocol = "aProtocol"; 我已经进行了一些研究,似乎我可能必须使用socket.protocol = "aProtocol";来设置协议socket.protocol = "aProtocol"; but after I set the protocol and set a break point socket.protocol is still "" even after calling socket.protocol = "soap"; 但是在我设置了协议并设置了断点之后,即使调用socket.protocol = "soap"; ;,套接字仍然仍然是socket.protocol = "soap"; . It also does not work to set the protocol using the constructor. 使用构造函数设置协议也不起作用。

Can anyone provide me with either some reasons why chrome.sockets.tcp has such a disconnect problem on Android but not iOS OR any reasons why using JavaScript's native WebSocket fails to connect? 有人可以为我提供chrome.sockets.tcp在Android上出现这种断开连接问题的原因,而在iOS上却没有,或者使用JavaScript的本地WebSocket无法连接的原因是什么? Any help is greatly appreciated. 任何帮助是极大的赞赏。

尝试在构造函数中传递子协议:

var socket = new WebSocket("ws://" + SSID + ":" + port, "soap");

Turns out it was due to our socket server being a TCP socket server. 原来是由于我们的套接字服务器是TCP套接字服务器。 TCP and WebSockets do not at all work together as there is a completely different process for making the connection. TCP和WebSocket根本无法协同工作,因为建立连接的过程完全不同。 So I ended up crating a TCP socket Cordova plugin instead. 因此,我最终创建了一个TCP套接字Cordova插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM