RXJS Websockets发送' undefined'消息
我使用RXJS Websockets和node.js后端使用“ WS”软件包具有Angular前端。
我遇到的问题是,没有消息从后端传输到前端 - 有一个连接,因为我可以强迫它们进行互动,并且我的角前端消息始终由后端正确接收。
问题是前端没有接收到后端消息,因此 - 这是我的后端代码...
clients = [];
wss;
webSocketServer() {
this.wss = new WebSocket.Server({ port: 7072 });
this.wss.on('connection', (ws) => {
const id = methods.generateRandomId(5);
this.clients.push({socket: ws, data: { id }});
ws.on('open', () => {
console.log(`Client ${id} has connected.`)
this.sendWebsockMessage(`User ${id} connected...`);
});
ws.on('message', (msg) => {
const message = JSON.parse(msg);
console.log(message);
});
ws.on('close', () => {
console.log(`Client ${id} has disconnected.`)
})
})
}
sendWebsockMessage(msg) {
this.clients.forEach(client =>{
client.socket.send(`[{hello: hi}]`);
});
}
问题是:
- 客户端$ {id}已连接不显示,这似乎暗示着“ open oble'例程不运行。
- 在前端,输出总是不确定的。我在该sendwebsockmessage函数中放置的内容似乎无关紧要 - 输出总是不确定的。
这是我的前端,角度代码:
ws: WebSocketSubject<any>;
webSockets(): void {
// deserializer is from this: https://github.com/ReactiveX/rxjs/issues/4166
// this solves a connection issue
this.ws = webSocket({url: `${environment.websocketUrl}`, deserializer: () => {}});
// this.sendMessage(ws, 'loginNotification', 'hello');
this.ws.subscribe(
{
next: (message: string) => {
console.log(`msg: ${message}`);
},
error: (error) => {
console.log(`Error in socket: ${error}`);
},
complete: () => {
console.log("connection to server closed");
}
});
}
sendTestMessage(): void {
this.ws.next({ msg: 'hello' });
}
所有这些代码都为我提供了msg:未定义的
从下一个:订阅的部分。如果我点击“ sendTestMessage”函数,则在node.js服务器上以字符串形式收到该对象。
I have angular frontend using RxJS websockets and a node.js backend using the 'ws' package.
The problem I am having is that no messages are being transmitted from the backend to the front end - there is a connection as I can force them to interact, and my Angular frontend messages are always received by the backend properly.
The issue is the backend messages are not being received by the front end, so for example - this is my backend code...
clients = [];
wss;
webSocketServer() {
this.wss = new WebSocket.Server({ port: 7072 });
this.wss.on('connection', (ws) => {
const id = methods.generateRandomId(5);
this.clients.push({socket: ws, data: { id }});
ws.on('open', () => {
console.log(`Client ${id} has connected.`)
this.sendWebsockMessage(`User ${id} connected...`);
});
ws.on('message', (msg) => {
const message = JSON.parse(msg);
console.log(message);
});
ws.on('close', () => {
console.log(`Client ${id} has disconnected.`)
})
})
}
sendWebsockMessage(msg) {
this.clients.forEach(client =>{
client.socket.send(`[{hello: hi}]`);
});
}
The problems are:
- Client ${id} has connected does not display, which seems to imply the 'open' routine doesn't run.
- On the front end the output is always undefined. It doesn't seem to matter what I put in that sendWebsockMessage function - the output is always undefined.
This is my front-end, Angular code:
ws: WebSocketSubject<any>;
webSockets(): void {
// deserializer is from this: https://github.com/ReactiveX/rxjs/issues/4166
// this solves a connection issue
this.ws = webSocket({url: `${environment.websocketUrl}`, deserializer: () => {}});
// this.sendMessage(ws, 'loginNotification', 'hello');
this.ws.subscribe(
{
next: (message: string) => {
console.log(`msg: ${message}`);
},
error: (error) => {
console.log(`Error in socket: ${error}`);
},
complete: () => {
console.log("connection to server closed");
}
});
}
sendTestMessage(): void {
this.ws.next({ msg: 'hello' });
}
All this code gives me is msg: undefined
from the next: section of the subscription. If I hit the 'sendTestMessage' function then that object is recieved in string form on the node.js server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论