2017-08-02 21 views
1

私はプログラムからメッセージを受け取るアプリケーションを持っています。このアプリケーションはメッセージを受信し、接続されたクライアントにメッセージをブロードキャストします。今、私はコンソールとコンソール上にメッセージを表示していますが、この受信機アプリケーションは完全に受信しています。しかし、クライアント(htmlページ)では、ブロードキャストされていません。 localhost/resultを開くと何も表示されません。何が間違っているのですか?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'); 
}); 

私が直接printMessage機能から、それを送信し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

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> 

EDIT:あなたはどのようにsocket.io.js含めているスクリプト

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

しようとすると、指定したサーバーのアドレスsocket.connect('localhost:3000');

I made this working example for you


RE-EDIT: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

回答ありがとうございますが、まだ動作していません。私がio.on( '接続...)して何かを印刷すると、それは動作します。しかし、私がすると、socket.on( 'chat message ...)それは動作しません。 – Neo

関連する問題