跨越不同Node.js路由的Socket.io

时间:2022-08-22 16:27:41

I am building simple Node.js application that uses the Express Framework as well as Socket.io. I got stuck however trying to receive messages from the server across different express routes. Is there a way of receiving messages sent from one route to another route through the server? I've given a very simplified version of my code below:

我正在构建使用Express Framework和Socket.io的简单Node.js应用程序。我遇到了困难,但试图通过不同的快速路线从服务器接收消息。有没有办法通过服务器接收从一条路由发送到另一条路由的消息?我在下面给出了一个非常简化的代码版本:

app.js

var express = require("express"),
    app = express(),
    http = require("http").createServer(app),
    fs = require("fs"),
    io = require("socket.io").listen(http);

//Create a server
var port = 9001;
http.listen(port, function() {
  console.log("Server is running on port " + port);
});

//Set up routes
var anotherPage = express.Router();
anotherPage.route('/')
.get(function(req, res, next) {
  res.sendFile('/AnotherPage.html', {root: './public'});
});
var someOtherPage = express.Router();
someOtherPage.route('/')
.get(function(req, res, next) {
  res.sendFile('/SomeOtherPage.html', {root: './public'});
});

//Set up static directories
app.use(express.static(__dirname + "/public"))
    .use('/anotherpage', anotherPage)
    .use('/someotherpage', someOtherPage);

io.sockets.on("connection", function(socket) {
  socket.on("send message", function(data) {
    socket.emit('return message', data);
  });
});

Inside public folder

AnotherPage.html

<!DOCTYPE html>
<html>
<head>
  <title>Sender</title>
  <script type="text/javascript" src="/socket.io/socket.io.js"></script>
  <script src="/anotherPage.js"></script>
</head>
<body>
  <input id="input" type="text" />
  <button onclick="sendMessage()">Submit</button>
</body>
</html>

anotherPage.js

var socket = io.connect();

function sendMessage() {
  socket.emit("send message", document.getElementById('input').value );
}

socket.on('return message', function(data){
  //Working
  console.log(data);
});

SomeOtherPage.html

<!DOCTYPE html>
<html>
<head>
  <title>Receiver</title>
  <script type="text/javascript" src="/socket.io/socket.io.js"></script>
  <script src="/someOtherPage.js"></script>
</head>
<body>
  //TODO
</body>
</html>

someOtherPage.js

var socket = io.connect();

socket.on('return message', function(data){
  //Not working
  console.log(data);
});

1 个解决方案

#1


2  

As I said in the comment, socket.emit on the server will send return message only to the one client, the same that it received send message from. If you want to send to anyone listening, use io.sockets.emit instead; or to all except socket with socket.broadcast.emit.

正如我在评论中所说,服务器上的socket.emit只会向一个客户端发送返回消息,就像它收到的发送消息一样。如果你想发送给任何听力的人,请改用io.sockets.emit;或者除了socket与socket.broadcast.emit之外的所有插件。

Basically, you are wondering why someone else can't eavesdrop on a private conversation. :) It has nothing to do with Express routing, and everything to do with socket.io API.

基本上,你想知道为什么其他人不能窃听私人谈话。 :)它与Express路由无关,而且与socket.io API有关。

#1


2  

As I said in the comment, socket.emit on the server will send return message only to the one client, the same that it received send message from. If you want to send to anyone listening, use io.sockets.emit instead; or to all except socket with socket.broadcast.emit.

正如我在评论中所说,服务器上的socket.emit只会向一个客户端发送返回消息,就像它收到的发送消息一样。如果你想发送给任何听力的人,请改用io.sockets.emit;或者除了socket与socket.broadcast.emit之外的所有插件。

Basically, you are wondering why someone else can't eavesdrop on a private conversation. :) It has nothing to do with Express routing, and everything to do with socket.io API.

基本上,你想知道为什么其他人不能窃听私人谈话。 :)它与Express路由无关,而且与socket.io API有关。