node.js socket.io没有广播到连接的客户端

时间:2017-08-02 07:11:10

标签: html node.js socket.io broadcastreceiver broadcast

我有一个从程序接收消息的应用程序。此应用程序接收消息,然后将消息广播到连接的客户端。现在我在控制台和控制台上显示消息,这个接收器应用程序正在接收完美。但是在客户端(html页面)上,它没有被广播。当我打开localhost / result时,不会显示任何内容。我做错了什么?

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var EventHubClient = require('azure-event-hubs').Client;
var connectionString = 'connection string';

const bodyParser = require('body-parser')

app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())

var printError = function (err) {
    console.log(err.message);
};

var result;

var printMessage = function (message) {
    console.log('Message received: ');
    result = JSON.stringify(message.body);
    obj = JSON.parse(result);
    console.log('message:' + result)

    console.log('');
};

count =0;

app.get('/result', function(req, res){

  res.sendFile(__dirname + '/result.html');
});


io.on('connection', function(socket){
  console.log('user connected');

  socket.on('chat message', function(msg){

     io.emit('chat message', result);
     console.log("This is id in the sock' section" + check_id);
  }); 
  socket.on('disconnect', function(){
    console.log('user disconnected');
      socket.removeAllListeners('disconnect');
      io.removeAllListeners('connection');
  });
});

var client = EventHubClient.fromConnectionString(connectionString);
client.open()
    .then(client.getPartitionIds.bind(client))
    .then(function (partitionIds) {
        return partitionIds.map(function (partitionId) {
            return client.createReceiver('$Default', partitionId, {     'startAfterTime' : Date.now()}).then(function(receiver) {
                console.log('Created partition receiver: ' + partitionId)
                receiver.on('errorReceived', printError);
                receiver.on('message', printMessage);
            });
        });
    })
    .catch(printError);


http.listen(3000, function(){
console.log('listening on *:3000');
});

result.html

<html>
  <head><title>Hello world</title></head>
  <script src="/socket.io/socket.io.js"></script>
  <script>
    var socket = io();
  //  socket.emit('chat message')


    socket.on('chat message',function(msg){

        socket.emit('chat message',msg);
        document.write('hello world');
        document.write(msg);
    });
  </script>
</html>   

1 个答案:

答案 0 :(得分:1)

我会直接从printMessage函数发送它:

function printMessage(message) {
 result = JSON.stringify(message.body);
 console.log('[message]',result);
 io.sockets.emit('chat message',result);
}

并记得修改您的客户端(result.html),以便它不会产生无限循环:

<script>
var socket=io();
socket.on('chat message',function(msg){
 console.log(msg); //alert(msg);
 document.write(msg);
});
</script>

编辑:

您如何包含 socket.io.js 脚本?

<script src="/socket.io/socket.io.js"></script>

尝试并指定服务器地址socket.connect('localhost:3000');

I made this working example for you

重新编辑: I remade the working example for you, this time I added a client-server emission that bounces back to all connected clients