错误TypeError:无法读取未定义的属性'readyState'

时间:2019-11-21 02:48:59

标签: angularjs typescript websocket connection readystate

如果这种情况if(this.sock.readyState === WebSocket.OPEN)已删除

错误将是:

  

TypeError:无法读取未定义的属性“发送”。

如果没有错误,请输入TypeError:

  

无法读取未定义的属性“ readyState”。

请帮助我。

我用了角型打字稿。 如果未连接, 我想检查不要send(this.sock.send(baseRequest.serializeBinary())

这是我的代码:

export class LoggerService {private sock: WebSocket;
 constructor(private setting: SettingProvider, private router: Router) {}
 public connectLoggerServer$(): Promise<void> {
 const protocol = location.protocol === "https:" ? "wss" : "ws";
 const url = `${protocol}://${location.hostname}/${this.setting.loggerUri}/`;
 this.sock = new WebSocket(url);

return new Promise((resolve, reject) => {
  this.sock.onopen = () => {

    resolve();
  };


  this.sock.onclose = e => {
    setTimeout(() => {
      this.connectLoggerServer$();
    }, 5000);
  };

  this.sock.onerror = e => {
    reject();
  };
});}

private sendLog(name: string, request: Uint8Array): void {
const baseRequest: BaseRequest = new BaseRequest();
baseRequest.setName(name);
baseRequest.setData(request);

try {

  if (this.sock.readyState === WebSocket.OPEN) {
    this.sock.send(baseRequest.serializeBinary());
  } else {
    console.log("Connection Failed");
  }
} catch (error) {
  console.log(error);
}}

public writeSend(message: string): void {
const request: CommunicationRequest = new CommunicationRequest();

request.setDatetime(this.getNowDate());
request.setProcessname("AAA");
request.setDirection("SEND");
request.setIpaddress(address);
request.setPort(Number(portNumber));
request.setMessage(message);
request.setResult("");

this.sendLog("CommunicationRequest", request.serializeBinary());}

0 个答案:

没有答案