Javascript Websocket.onOpen在Websocket实际打开之前运行
在我的Javascript Websocket.onOpen在Websocket实际打开之前运行,javascript,websocket,Javascript,Websocket,在我的socket.onopen函数中有一行写着socket.send(昵称),但由于某种原因,我收到一个错误-未能在“WebSocket”上执行“send”:仍处于连接状态。 以下是我的代码和一个错误的屏幕截图: // создать подключение var socket = null; var nickname; // отправить сообщение из формы publish document.forms.publish.onsubmit = function()
socket.onopen
函数中有一行写着socket.send(昵称)
,但由于某种原因,我收到一个错误-未能在“WebSocket”上执行“send”:仍处于连接状态。
以下是我的代码和一个错误的屏幕截图:
// создать подключение
var socket = null;
var nickname;
// отправить сообщение из формы publish
document.forms.publish.onsubmit = function() {
var outgoingMessage = this.message.value;
socket.send(outgoingMessage);
return false;
};
const connect = document.getElementById('connect');
const disconnect = document.getElementById('disconnect');
connect.addEventListener('click', function(){
start();
});
function start() {
socket = new WebSocket("ws://localhost:8081");
socket.onmessage = function(event) {
var incomingMessage = event.data;
showMessage(incomingMessage);
};
socket.onopen = authorizate();
}
function authorizate(){
nickname = document.getElementById('nickname').value;
document.getElementById('nickname').style.display = "none";
document.getElementById('subm').style.display = "inline-block";
disconnect.style.display = "inline-block";
connect.style.display = "none";
console.log(nickname);
socket.send(nickname);
}
应该是
socket.onopen=authorizate代码>-您的代码正在调用该函数并将返回值分配给onopen
@Pointy哦,天哪,我太不体谅您了,对不起。非常感谢。应该是socket.onopen=authorizate代码>-您的代码正在调用该函数并将返回值分配给onopen
@Pointy哦,天哪,我太不体谅您了,对不起。非常感谢。