我想连接到我的web套接字,该套接字放在带有一些ip的amazone实例上。我可以使用google rest客户端应用程序连接我的网络套接字和一些ip和端口,并且它的工作非常好。
屏幕截图:
但是,如果我想用java脚本连接它,它无法连接。这在2-3个月之前工作正常。我没有改变和事情,但它现在不起作用。 如果我想与Firefox连接,则会产生错误。 这是我的代码: -
function init() {
var host = "ws://XX.XX.XXX.XXX:XXXX"; // SET THIS TO YOUR SERVER
try {
var socket = new WebSocket(host);
// alert('WebSocket - status ' + socket.readyState);
log('WebSocket - status ' + socket.readyState);
socket.onopen = function (msg) {
alert('open');
alert("Welcome - status " + this.readyState);
log("Welcome - status " + this.readyState);
if (this.readyState != 1)
{
reconnect();
}
};
socket.onmessage = function (msg) {
// alert("Received: " + msg.data);
log("Received: " + msg.data);
};
socket.onclose = function (msg) {
// alert("Disconnected - status " + this.readyState);
log("Disconnected - status " + this.readyState);
};
} catch (ex) {
alert(ex);
log(ex);
}
$("msg").focus();
}
这是警报状态0并在控制台中显示错误: -
Firefox can't establish a connection to the server at ws://XX.XX.XXX.XXX:XXXX.
var socket = new WebSocket(host);
答案 0 :(得分:0)
我会尝试使用您的代码,对我来说工作正常,我会使用此网页对其进行测试:https://www.websocket.org/echo.html,可能对测试有帮助。但我也发现了这个问题:websocket-rails, websocket handshake error,也许也有帮助。 但是,我只需将代码中的主机更改为:“ws://echo.websocket.org”,一切都可以正常运行。 希望你找到一个解决方案,这个信息有任何帮助。这是我用于测试的代码:
function init() {
var host = "ws://echo.websocket.org";
try {
var socket = new WebSocket(host);
alert('WebSocket - status ' + socket.readyState);
socket.onopen = function (msg) {
alert('open');
alert("Welcome - status " + this.readyState);
if (this.readyState != 1)
{
reconnect();
}
};
socket.onmessage = function (msg) {
alert("Received: " + msg.data);
};
socket.onclose = function (msg) {
alert("Disconnected - status " + this.readyState);
};
} catch (ex) {
alert(ex);
}
$("msg").focus();
}
*抱歉我的英语不好。