发出后无法连接到套接字

Can't connect to socket after emit

我正在从事涉及实时温度的项目,并且有一个设备通过 get 发送温度,该温度通过服务器路由并发送到套接字。然后我希望服务器连接到原始套接字并将数据发送到我的客户端正在读取的新套接字。

这是我的app.js

    var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(8080);

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});


app.route('/:temp')
   .get(function (req, res){
        var temp = req.params.temp;
        res.end(temp);
        io.sockets.on('connection', function(socket){
            socket.emit('send temp', temp);
        });
    });

io.sockets.on('connection', function(socket){
    socket.on('send temp', function(data){
        console.log('connected to send temp');//this never shows in the console
        io.sockets.emit('new temp', data);
    });
});

app.js 中的路由代码工作正常。当我点击 localhost:3000/test 并将客户端更改为连接到 'send temp'(而不是 'new temp')时,输出 'test'。

这是我客户的相关部分

var socket = io.connect();
        var $temp = $('#temp');         


        socket.on('new temp', function(data){
            $temp.html("Temp: " + "<br/>"+data);
        });

我是 运行 node 版本 4.1.2,socket 1.3.7 和 express 4.10.8。 我想知道为什么我不能第二次连接到原来的套接字。或者那甚至可能不是我的问题。我研究了很多 'chat' 教程并搜索了其他教程,尝试做我想做的但没有成功。

最终我想做的是让客户端通过实时读取一遍又一遍地点击 /:temp,然后让其他客户端实时获取该数据。

这对我来说还是有点新鲜,所以感谢任何帮助。

您的代码示例在服务器上为 'send temp' 消息注册了一个消息处理程序。客户端为 'new temp' 消息注册消息处理程序。

这两个(客户端和服务器)然后僵持地坐在那里等待某人首先发送消息,但是(根据您披露的代码)从来没有人这样做过。

我不太明白你的代码的意图,但我看到了几个问题。

首先,您不想在这段代码中为 connection 事件安装侦听器:

app.route('/:temp')
   .get(function (req, res){
        var temp = req.params.temp;
        res.end(temp);
        io.sockets.on('connection', function(socket){
            socket.emit('send temp', temp);
        });
    });

为什么只有在获得特定路由处理程序时才开始侦听连接事件。而且,为什么每次命中该路由时还要添加另一个事件处理程序。这段代码是完全错误的。我不知道你认为你试图用它实现什么,但这不是做事的方式。

其次,此代码正在等待客户端发送 'send temp' 消息,当它收到一条消息时,它会尝试将其广播给所有客户端。但是,您披露的客户部分从未发送 'send temp' 消息。

io.sockets.on('connection', function(socket){
    socket.on('send temp', function(data){
        console.log('connected to send temp');//this never shows in the console
        io.sockets.emit('new temp', data);
    });
});

第三,请用文字准确描述您要实现的目标,以便我们更好地了解为实现该目标推荐的代码。


编辑

现在您已经在此处描述了实际问题:

Ultimately what I am trying to have happen is have a client hit /:temp over and over with a real-time reading and then have other clients get that data in real-time.

推荐一个解决方案比较简单:

在服务器上:

var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(8080);

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/index.html');
});


app.get('/:temp', function (req, res) {
     var temp = req.params.temp;
     res.end(temp);
     // send this temperature to all connected clients
     io.emit('new temp', temp);
});

在客户端:

var socket = io.connect();
var $temp = $('#temp');         
socket.on('new temp', function(data){
    $temp.html("Temp: " + "<br/>"+data);
});