2016-04-22 53 views
0

當用戶進行GET /檢查/健康時,該客戶端應該與服務器通話並且服務器應該給客戶端提供答案.. 但是,服務器發送的消息未在客戶端..節點 - 充當網絡服務器和客戶端

客戶端 - 也作爲一個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/'"); 

服務器端:

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

有許多這裏潛在的問題,但馬一個是你的服務器端代碼缺少以下非常重要的一行:

http.listen(4000); 

並稱,應該讓你開始正確的道路。此外,我建議將http變量重命名爲其他內容,因爲它不是http模塊。 server對我更有意義。

下面是您要做的更簡單的示例。它缺少一些東西,比如錯誤處理,考慮到/check/health的請求進來,並且你的socket.io連接沒有啓動時會發生什麼,等等,但是我會把它作爲一個練習。我還修剪了一些與這個問題無關的東西(貓鼬,ejs模板等),所以當你確信這件作品按照預期工作時,你必須重新添加這些東西。


客戶端

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

代碼中有很多未使用的變量,我只是試圖讓服務器通話客戶在這裏..其他代碼是爲了別的..有人可以幫我嗎? –