import { Component, OnInit } from '@angular/core';
import { webSocket } from 'rxjs/webSocket';
@Component({
selector: 'app-open-orders',
templateUrl: './open-orders.component.html',
styleUrls: ['./open-orders.component.css']
})
export class OpenOrdersComponent implements OnInit {
ws: WebSocket;//定义websocket
constructor(){}
ngOnInit() {
this.connectWs();
}
//socket连接
connectWs() {
if (this.ws != null) { this.ws.close() };
this.ws = new WebSocket("wss://172.20.201.148:1804/ws");
let that = this;
this.ws.onopen = function (event) {
//socket 开启后执行,可以向后端传递信息
that.ws.send('sonmething');
}
this.ws.onmessage = function (event) {
//socket 获取后端传递到前端的信息
that.ws.send('sonmething');
}
this.ws.onerror = function (event) {
//socket error信息
}
this.ws.onclose = function (event) {
//socket 关闭后执行
}
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论