【发布时间】:2019-03-28 07:25:45
【问题描述】:
我正在尝试从网络套接字获取数据。我正在将 websocket 对象初始化为全局对象。
socket =new WebSocket('ws://xx.xx.xx.xx:xxxx/socket');
在 ngOnInit 上,我正在打开连接:
this.socket.onopen = function(){
console.log("websocket is open now");
}
现在我需要使用从 UI 触发的函数发送消息,并且来自 websocket 的回复应该将消息存储在全局变量中。
sampleFunction(val) {
this.socket.send(val);
this.socket.onmessage=function(event){
console.log("got message");
this.dummy=event.data;
}.bind(this)
}
所以在页面加载时它正在连接,但是当我触发该功能时,消息没有发送,因此我没有收到任何回复
【问题讨论】:
-
你有什么问题?
-
"global" = 角度服务
标签: angular websocket angular6