cocosCreator webSocket工具


const webSocketHelper = cc.Class({
    extends: cc.Component,

    createWs(){
        this.ws = new WebSocket("ws://127.0.0.1:8888");
        this.ws.onopen = function (event) {
            console.log("已連接.");
        }.bind(this);
        this.ws.onmessage = function (event) {
            let data = JSON.parse(event.data);
            console.log("收到消息: " + event.data);
        }.bind(this);
        this.ws.onerror = function (event) {
            console.log("發生錯誤");
        }.bind(this);
        this.ws.onclose = function (event) {
            console.log("已關閉");
        }.bind(this);
    },

    send(msg){
        this.ws.send(msg);
    } 
});
window.webSocketHelper = new webSocketHelper();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM