节点 - 充当Web服务器和客户端

时间:2016-04-22 23:17:07

标签: node.js

当用户执行GET / check / health时,此客户端应该与Server通信,服务器应该给客户端答案。 但是客户端没有收到来自服务器的消息..

客户端 - 也充当网络服务器

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');
});

1 个答案:

答案 0 :(得分:0)

此处存在许多潜在问题,但主要问题是您的服务器端代码缺少以下非常重要的一行:

http.listen(4000);

添加它应该让你开始走正确的道路。另外,我建议将http变量重命名为其他变量,因为它不是http模块。 server对我来说更有意义。

这是您想要做的最简单的例子。它缺少一些诸如错误处理之类的东西,考虑到/check/health的请求进入时你应该发生什么以及你的socket.io连接没有等等,但是我会把它作为练习留给你。我还修剪了一些与问题无关的东西(mongoose,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');
});