我正在使用server.js文件和routes.js文件创建一个node.js应用程序并运行一个WebSocket。我正在尝试使用routes.js文件在某个页面内发送消息,并最终将套接字消息指向特定的websocket客户端。目前,我在使用on("message")函数时遇到了问题。
从server.js文件:
require('./app/routes.js')(app, passport, wss); // load our routes and pass in our app and fully configured passport
wss.on('connection', function connection(ws, req) {
console.log('Client connected');
ws.on('message', function incoming(message){
console.log("Incoming message: %s", message);
})
ws.on('close', () => console.log('Client disconnected'));
})
;从routes.js文件:
app.post('/allbooks', function(
req,res)
{
wss.clients.forEach((client) => {
var arrayToSend = JSON.stringify([req.body.bookid, [], req.body.userSays]);
console.log("Printing within routes.js");
console.log(arrayToSend);
client.send(arrayToSend);
});
res.redirect('/allbooks');
});当我发送消息时,我在日志中看到“在routes.js中打印”,但没有看到来自server.js部分的“传入消息”部分。为什么不行?
PS下面是我正在运行的页面中的scripts.js文件:
var HOST = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(HOST);
ws.onmessage = function (event) {
eventJSON = JSON.parse(event.data);
if ($("#"+eventJSON[0]+"-request").length > 0){
$("#"+eventJSON[0]+"-request").html("Message sent");
}
} 发布于 2018-06-02 06:28:13
我不知道您使用的是不是this包,但无论您使用的是什么包,都会是相似的。在自述文件中,它介绍了两个示例:
服务器示例
在服务器示例中,他们创建了一个web socket服务器:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('hello from the server!');
});这将创建侦听套接字服务器。它为message添加了一个事件侦听器,以便当客户端连接到服务器时,该客户端可以发送消息,该事件处理程序将被触发。
在该事件侦听器之后,它将发送字符串'hello from the server!'。那个send调用不会触发上面的事件处理程序,因为这个send调用发生在套接字的服务器端。它正在发送要在该套接字的客户端接收的消息。
客户端示例(通常在浏览器中)
const WebSocket = require('ws'); // omit this line if including ws via <script> tag
const ws = new WebSocket('ws://www.host.com/path');
ws.on('open', function open() {
ws.send('hello from the client!');
});
ws.on('message', function incoming(data) {
console.log(data);
});这是连接到第一个示例中的套接字服务器的客户端脚本。它将连接起来,并在上面的套接字服务器上引发'connection'事件。然后,套接字服务器调用ws.send('hello from the server!');,我们的客户端脚本在这里为'message'设置一个侦听器,这样它就会触发并将'hello from the server!'输出到浏览器控制台。
此客户端脚本还设置了一个'open'事件处理程序,该处理程序在成功连接到套接字服务器后触发。因此,它将启动并将'hello from the client!'发送到服务器。服务器的'message'处理程序将触发,并且在服务器的终端中,您将看到'hello from the client!'打印输出。
在您的示例中,您是从服务器(服务器端的routes.js文件)执行此操作的。
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});您在那里所做的是迭代所有已成功连接到套接字服务器的已连接客户端,并分别向每个客户端发送一条消息。这仍然不会触发您在每个套接字首次连接时在每个套接字上设置的'message'的服务器端on事件处理程序。它将在浏览器中触发客户端的处理程序。
根据您上面的最后一条评论进行编辑:
要做你想做的事情,你需要在客户机上有类似这样的东西:
myButton.onClick = function (e) {
ws.send("Hey everyone! I'm Chev!");
};然后,在服务器上的连接处理程序中,您需要设置一个消息处理程序,如下所示:
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(message);
}
});
});
});您可以将message设置为对象,而不是字符串,这样您就可以根据该对象的属性执行不同的操作,但为了简单起见,我只是让服务器将它接收到的任何消息广播到服务器的其余部分。
注意if语句的检查,以确保我们不会将消息重新广播回发送它的同一客户端。
https://stackoverflow.com/questions/50651208
复制相似问题