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

相关文章:

  • 2021-10-29
  • 2021-12-21
  • 2021-09-30
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2021-10-19
  • 2021-04-08
猜你喜欢
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2022-02-24
  • 2021-08-11
  • 2022-12-23
相关资源
相似解决方案