码迷,mamicode.com
首页 > Web开发 > 详细

cocosCreator webSocket工具

时间:2020-04-14 10:41:44      阅读:343      评论:0      收藏:0      [点我收藏+]

标签:component   osc   ESS   web   nop   extends   sock   连接   fun   

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();

cocosCreator webSocket工具

标签:component   osc   ESS   web   nop   extends   sock   连接   fun   

原文地址:https://www.cnblogs.com/jianxiaopo/p/12696059.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!