2016-04-22 5 views
0

ユーザーがGET/check/healthを実行すると、このクライアントはサーバーと通信し、クライアントに応答を提供する必要があります。 サーバーからのメッセージは、クライアント..Webサーバーとクライアントとしてのノード機能

クライアント側には - また、ウェブサーバとして動作する

var io = require('socket.io-client'); 
var socket = io.connect('http://localhost:4000', {reconnect: true}); 
var express = require('express'); 
var app= express(); 
var path = require('path'); 
var bodyParser= require('body-parser'); 
app.use(express.static(__dirname+"/public/")); 
app.use(bodyParser.json()); 
app.set('views',path.join(__dirname,'/public/html')); 
app.engine('html', require('ejs').renderFile); //specify which template engine to use 
app.set('view engine', 'ejs'); 

app.get('/check/health',function(req,res){ 
    //console.log('Connected Success!!'); 
    socket.on('connect', function(socket) { 
    console.log('Connected!'); 
    }); 
    socket.emit('data', 'I need your health status'); 

    socket.on('data', function(data) { 
    console.log('Message from monitoring is : ' + ': ' + data); 
    }); 

    socket.on('server data', function(data) { 
    console.log('Received server data: ' + data); 
    }); 
}); 

app.listen(3000); 
console.log("Server running at http://localhost:3000/'"); 

サーバー側:

ここに潜在的な問題の数が、maがあります
var app = require('express')(); 
var SERVER = require('http').Server(app); 
var io = require('socket.io')(SERVER); 
var express = require('express'); 

var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/sensor_db'); 

io.on('connection', function(socket){ 
    console.log('connection received from Provisioning '); 

    // To get messages from Provisioning server 
    socket.on('data', function(data) { 
    console.log('Message from provision is : ' + ': ' + data); 
    }); 

    socket.emit('server data', 'Here is yiour data - 1111'); 

}); 

SERVER.listen(4000, function(){ 
    console.log('listening on *:4000'); 
}); 
+0

質問は、スタンドアロンする必要があります。他の回答/質問を参照しないでください。 –

+0

これは、技術的には、別の質問を参照する文以外のスタンドアロンです。問題の答えをまとめるには十分なコードがここにあります。 – dvlsg

+0

私は、なぜサーバのエッセイがクライアントで見られないのかわかりません。 –

答えて

0

1つは、サーバー側のコードに次の重要な行がないことです。

http.listen(4000); 

これを追加すると、正しいパスを開始することができます。また、httpモジュールではないので、http変数の名前を変更することをお勧めします。 serverは私にとってもっと意味があります。

ここでは、あなたがやろうとしているもののうち、最小限の例があります。 /check/healthへのリクエストが来て、あなたのsocket.io接続が上がっていないなど、何が起こるべきかを考慮して、エラー処理などいくつかの欠点があります。私はまた、質問に関係のないもの(mongoose、ejs templatingなど)を整理しました。したがって、あなたがこの作品が意図したとおりに動いていると確信しているときに、それらを追加しなければなりません。


クライアント側

var io = require('socket.io-client'); 
var socket = io.connect('http://localhost:4000', { reconnect: true }); 
var express = require('express'); 
var app= express(); 
var path = require('path'); 

// careful here -- the socket.io connection will be made 
// outside of the context of the /check/health callback, 
// so you should move the connect event handler out here. 
socket.on('connect', function(socket) { 
    console.log('Connected!'); 
}); 

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

    // note the third argument here, 
    // which can be used as an acknowledgement from the server 
    // that your client's emit was received 
    socket.emit('data', 'I need your health status', function ack(data) { 
    console.log('data emit was acknowledged:', data); 

    // make sure you send something back to the requester 
    // or they'll just hang until timeout 
    return res.json(data); 
    }); 

    // if you want, you could technically use socket.once('server data'), 
    // in this location, but this is probably going to be closer 
    // to the style of communication you actually want -- 
    // which is one response to this specific single socket emit. 
}); 

app.listen(3000); 
console.log('Server listening at port 3000'); 

サーバー側

var app = require('express')(); 
var server = require('http').Server(app); 
var io = require('socket.io')(server); 
var express = require('express'); 

io.on('connection', function(socket){ 
    console.log('connection received from Provisioning'); 

    // To get messages from Provisioning server 
    socket.on('data', function(data, ack) { 
    console.log('Message from provision is : ' + ': ' + data); 
    ack('here is your data - 1111'); 
    }); 
}); 

server.listen(4000, function(){ 
    console.log('socket.io server listening on *:4000'); 
}); 
+0

http.listen(4000、function(){ console.log( 'listen on *:4000'); }); –

+0

上記のコードはサーバーに存在しますが、私の間違いは追加されません...サーバーメッセージはここにクライアントに到達できません –

+0

コードには未使用の変数がたくさんあります。私はサーバーを話そうとしていますここにクライアントに。他のコードは何かのためです..誰かが私をここで助けることができますか? –