2017-08-02 91 views
1

我有一個應用程序接收來自程序的消息。該應用程序接收消息,並將消息廣播給連接的客戶端。現在我在控制檯和控制檯上顯示消息,這個接收器應用程序正在完美接收。但是在客戶端(html頁面),它不會被廣播。當我打開本地主機/結果時,不顯示任何內容。我究竟做錯了什麼?node.js socket.io未廣播到連接的客戶端

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> 
+0

您是否嘗試過重新制作的示例? – EMX

+0

它工作得很好。但是,爲什麼我不明白,爲什麼它的工作正常,當按鈕單擊而不是文檔加載觸發發射? – Neo

+0

,因爲onload你應該首先將套接字連接到服務器並設置客戶端套接字事件處理程序。然後在'連接'的客戶端內,你知道這個套接字已經連接上了,所以在這個事件中你可以開始發射。 – EMX

回答

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

+0

不工作。 result.html不顯示任何內容。 – Neo

+1

添加了一個工作示例[gist](https://gist.github.com/unknown0perator/50d6746cfddcb9c5c917b7baa6ae06b2) – EMX

+0

Thankyou的答案,但它似乎仍然不工作。當我做io.on('連接...)並打印一些東西時,它就可以工作。但是,當我這樣做,socket.on('聊天消息...)它不起作用。 – Neo

相關問題