fanux / lhttp

go websocket, a better way to buid your IM server
MIT License
691 stars 141 forks source link

如何关闭这个socket #28

Open nejinn opened 4 years ago

nejinn commented 4 years ago

如何关闭socket

    initLHTTP() {
      this.lHttpClient = new Lhttp("ws://127.0.0.1:9527");
      console.log(this.lHttpClient);
    },
    lHTTPOnOpen() {
      if (this.visible === true) {
        this.lHttpClient.on_open = function(context) {
          context.subscribe("test", "", null, "");
        };
      } else {
        this.lHttpClient.conn.close();
      }
    },
    lHTTPOnMessage() {
      this.lHttpClient.on_message = function(context) {
        if (context.getBody() !== "") {
          const lhttpData = JSON.parse(context.getBody().split("\r\n")[0]);
          console.log(lhttpData);
        }
      };
    },
    lHTTPOnError() {
      this.lHttpClient.on_error = function() {
        this.initLHTTP();
      };
    },
    lHTTPOnClose() {
      this.lHttpClient.on_close = function(context) {
        console.log("onclose:" + this.lHttpClient.conn + context);
        this.lHttpClient.conn.close();
      };
    },

这样写lHTTPOnClose不生效,我只能把close写到

    lHTTPOnOpen() {
      if (this.visible === true) {
        this.lHttpClient.on_open = function(context) {
          context.subscribe("test", "", null, "");
        };
      } else {
        this.lHttpClient.conn.close();
      }
    },