using WiFly with ws websockets



我一直在试图找出一种方法来连接wily从Arduino发送一些加速度计数据到我的node.js服务器。目前我的解决方法是有三个服务器:

  1. Http>>这是客户端使用的
  2. Net server>>这基本上是TCP请求,这就是我的服务器如何从3接收信息。WS websockets>>这将从Net服务器获取数据并将其流式传输到客户端。

代码如下:

var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var net = require('net');
var sensorData;
var message = {
  "data": ''
}
var newValue,
  oldValue,
  diff;
//Settings
var HTTP_PORT = 9000;
var NET_PORT = 9001;
var WS_PORT = 9002;
//Server
var mimeTypes = {
  "html": "text/html",
  "jpeg": "image/jpeg",
  "jpg": "image/jpeg",
  "png": "image/png",
  "js": "text/javascript",
  "css": "text/css"
};
http.createServer(function (req, res) {
  var fileToLoad;
  if (req.url == '/') {
    fileToLoad = 'index.html';
  } else {
    fileToLoad = url.parse(req.url).pathname.substr(1);
  }
  console.log('[HTTP] :: Loading :: ' + 'frontend/' + fileToLoad);
  var fileBytes;
  var httpStatusCode = 200;
  fs.exists('frontend/' + fileToLoad, function (doesItExist) {
    if (!doesItExist) {
      console.log('[HTTP] :: Error loading :: ' + 'frontend/' + fileToLoad);
      httpStatusCode = 404;
    }
    var fileBytes = fs.readFileSync('frontend/' + fileToLoad);
    var mimeType = mimeTypes[path.extname(fileToLoad).split('.')[1]];
    res.writeHead(httpStatusCode, {
      'Content-type': mimeType
    });
    res.end(fileBytes);
  });
  // console.log("[INIT] Server running on HTTP Port");
}).listen(HTTP_PORT);
proxy.on("close", function(){
  console.log("Connection has closed");
});
proxy.on("end", function(){
  console.log("Connection has ended");
 });
var socket;
var clients = [];
var socketObject;
var server = net.createServer(function (socket) {
  socketObject = socket;
  socket.name = socket.remoteAddress + ":" + socket.remotePort;
  clients.push(socket);
  console.log(socket);
  socket.write("HTTP/1.1 101", function () {
    console.log('[CONN] New connection: ' + socket.name + ', total clients: ' + clients.length);
  });
  socket.setEncoding('utf8');
  socket.on('error', function (data) {
    console.log(data);
  });
  socket.on('end', function () {
    console.log('[END] Disconnection: ' + socket.name + ', total clients: ' + clients.length);
  });
  socket.on('data', function (data) {
    console.log('[RECV from ' + socket.remoteAddress + "] " + data);
    oldValue = newValue;
    newValue = data;
    diff = Math.abs(newValue) - Math.abs(oldValue);
    console.log(Math.abs(newValue) + '-' + Math.abs(oldValue));
    message.data = Math.abs(diff);
    console.log('[SAVED] ' + message.data);
  });
});
server.listen(NET_PORT, function () {
  console.log("[INIT] Server running on NET server port", NET_PORT);
});
var WebSocketServer = require('ws').Server,
  wss = new WebSocketServer({
    port: WS_PORT
  });
wss.on('connection', function (ws) {
  // ws.send(JSON.stringify(message));
  setInterval(function () {
    updateXData(ws)
  }, 500);
});

function updateXData(ws) {
  var newMessage = {
    "data": ""
  }
  newMessage.data = message.data
  ws.send(JSON.stringify(newMessage));
}

所以问题是:是否有一种更干净的方法来做到这一点,只是使用ws来处理来自wi - fi的数据,然后将其发送到客户端?

提前感谢!

不确定这是否适合你,可能对你来说是新的,但你可以利用MQTT,有免费的代理可用,这是非常好的,它相对容易设置和实现配备了WiFly Shield的Arduino。

http://mqtt.org/

希望这能有所帮助!

相关内容

  • 没有找到相关文章

最新更新