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