订阅websocket时无法执行功能

时间:2020-06-04 17:22:20

标签: angular

实际上,在我的项目中,我正在尝试制作一个白板,在该白板中,单击某个功能后,我将其预订为Web套接字端点。

每当我尝试在订阅中调用函数“ newDrawing()”时,我就无法这样做...

这是我的交易代码

  connect() {
    let socket = new WebSocket('ws://localhost:8080/whiteBoard');
    this.ws = Stomp.over(socket);
    let that = this;
    this.ws.connect(
      {},
      function (frame) {
        that.ws.subscribe('/errors', function (message) {
          alert('Error ' + message.body);
        });
        that.ws.subscribe('/meeting/coordinates', function (message) {
          var res = JSON.parse(message.body);
          console.log(res);
          this.newDrawing(res.x, res.y);
        });
        that.disabled = true;
      },
      function (error) {
        alert('STOMP error ' + error);
      }
    );
  }

  newDrawing(x, y) {
    this.cx.moveTo(x, y); // from
    this.cx.lineTo(x, y);
    this.cx.stroke();
  }

我被显示为错误

enter image description here

1 个答案:

答案 0 :(得分:0)

将您的连接方法更改为:-

connect() {
    let socket = new WebSocket('ws://localhost:8080/whiteBoard');
    this.ws = Stomp.over(socket);
    let that = this;
    this.ws.connect(
      {},
      (frame) => {
        that.ws.subscribe('/errors', (message) => {
          alert('Error ' + message.body);
        });
        that.ws.subscribe('/meeting/coordinates', (message) => {
          var res = JSON.parse(message.body);
          console.log(res);
          this.newDrawing(res.x, res.y);
        });
        that.disabled = true;
      },
      (error) => {
        alert('STOMP error ' + error);
      }
    );
  }