Javascript 如何在typescript websocket上发送消息
我有一个angular应用程序需要订阅websocket以获取传入数据 在角度方面,在我的服务中Javascript 如何在typescript websocket上发送消息,javascript,node.js,angular,websocket,Javascript,Node.js,Angular,Websocket,我有一个angular应用程序需要订阅websocket以获取传入数据 在角度方面,在我的服务中 setContextChannel(channel) { this.contextWS = new WebSocket(channel); that = this; this.contextWS.onmessage = function(event) { console.log('ws', event.data); that.patientID
setContextChannel(channel) {
this.contextWS = new WebSocket(channel);
that = this;
this.contextWS.onmessage = function(event) {
console.log('ws', event.data);
that.patientID = event.data;
};
this.contextWS.onerror = function(event) {
console.log('ws error', event);
}
}
在模拟服务器端,我有一个typescript节点服务器,它创建套接字,如下所示:
import {Server} from "ws";
var wsServer: Server = new Server({port: 8085});
console.log('ws on 8085');
wsServer.on('connection',websocket => websocket.send('first pushed message'));//////
我的问题是如何使用wsServer发送消息?我不知道你在问什么。这一行是正确的:
wsServer.on('connection',websocket => websocket.send('first pushed message'));
如果要继续向所有连接的客户端发送消息,则需要使用属性wsServer.clients向每个连接的客户端发送消息,或者将对每个连接的客户端的引用存储在代码中的websocket变量数组中,然后使用forEach将消息发送给每个客户端
看看这个代码示例:谢谢,我要找的内容已在您的链接中列出。我一直在寻找的是“信息”上的websocket