node.js - using WiFly with ws websockets -
I'm trying to remove a way to connect WiFly to an Arduino so that my exposure data is stored in my node. To send in Server.
Here is the code:
var http = Required ('http'); Var FS = Requirement ('FS'); Var path = requirement ('path'); Var url = requirement ('url'); Var Net = Requirement ('Net'); Var sensorata; Var message = {"data": ''} var newValue, oldValue, diff; // settings var HTTP_PORT = 9000; Var NET_PORT = 9 001; Var WS_PORT = 9002; // server var mimetaps = {"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';} and {fileToLoad = url.parse (req.url) .pathname Substr (Http: Function does not exist) {if (! DoesItExist) {console.log ('[HTTP]: Error loading:' + 'frontend /' + file toload); 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 is running on HTTP port");}). Listen (HTTP_PORT); Proxy On ("off", function () {console.log ("connection is closed");}); Proxy ("End", function () {console.log ("the connection has ended");}); Var socket; Var Client = []; Var socketObject; Var server = net.createServer {socketObject = socket; socket.name = socket.remoteAddress + ":" + socket.remotePort; clients.push; console.log (socket); socket. Written ( "Http / 1.1 101", function () {console.log ('[CONN] new connection:' + socket.name + ', total customers:' + clients.length);}); socket.setEncoding ('utf8' ); Socket.on ('Error', function (data) {console.log (data);}); socket.on ('end', function () {console.log ('[END] disconnection:' + socket .name + ', Total customers:' + clients.length);}); socket.on ("data", function (data) {"console.log" ("[Socket.remoteAddress +"] "RECV + +" + Data); old value = new value; newvalue = data; diff = Math.abs (newValue) - Math.bas (old value); console.log (Math.babs (newValue) + '-' + Math.bas (oldview); message .data = Math.abs (diff); console .log ('[SAVED]' message.data);});}); Server.listen (NET_PORT, function () {console.log ("[INIT] server is running on server server port", NET_PORT); }); Var requires WebSocketServer = ('ws'). Server, wss = new WebSocketServer ({port: WS_PORT}); Wss.on ('connection', function (ws) {// ws.send (JSON.stringify (message)) Set enterwall (function () {updateXData (ws)}, 500);}); Function Update XData (ws) {var newMessage = {"Data": ""} newMessage.data = message.data ws.send (JSON.stringify (newMessage)); }
The question is, is it just a cleaner way to handle data from WiFi and send it to the client?
Thanks in advance!
Not sure whether it's favorable for you or not, but it can be new to you But you use the MCTT, free brokers are available which are very good and apply with Arduino equipped with its relatively easy installation and WiFi Shield.
Hope it helps to some extent! / P>
Comments
Post a Comment