1 回答

TA貢獻1890條經(jīng)驗 獲得超9個贊
Serverfunctionality打開事件偵聽器未觸發(fā),因為在我的實例中已經(jīng)使用 this.socket = new WebSocket('ws://localhost:8080');對象藍圖內(nèi)的聲明建立了連接。它在這里起作用的原因:<button id="signupbutton" onclick="new Serverfunctionality().sign_up()" type="button">Sign up</button>是因為我們在創(chuàng)建 實例時建立連接Serverfunctionality,因此我們能夠處理“打開”事件。
而不是嘗試在“打開”事件中運行代碼塊。檢查連接是否打開,然后運行,因為在按下按鈕之前連接將打開。
代碼:
this.sign_up = function (){
if (this.socket.readyState == WebSocket.OPEN ) {
console.log('works')
this.socket.send("works")
this.send_data();
}
}
添加回答
舉報