Node.js HOME Node.js Intro Node.js Get Started Node.js Modules Node.js HTTP Module Node.js File System Node.js URL Module Node.js NPM Node.js Events Node.js Upload Files Node.js Email. Node.js server.listen Method HTTP Server. The server.listen method creates a listener on the specified port or path. Anatomy of an HTTP Transaction. The purpose of this guide is to impart a solid understanding of the process of Node.js HTTP handling. We'll assume that you know, in a general sense, how HTTP requests work, regardless of language or programming environment. We'll also assume a bit of familiarity with Node.js EventEmitters and Streams.
Websockets and a web page to connect to them in node-red
websocket example.json
[{'id':'b4215b01.c8ba88','type':'websocket-listener','z':'e39b5c88.c392a','path':'/websocket','wholemsg':'false'},{'id':'42cae1f4.fbed5','type':'http request','z':'e39b5c88.c392a','name':','method':'GET','ret':'txt','url':'http://realtimeweb-prod.nationalgrid.com/SystemData.aspx','x':250,'y':60,'wires':[['93f57ff5.c4d84']]},{'id':'bb84a980.f65648','type':'http in','z':'e39b5c88.c392a','name':','url':'/test','method':'get','swaggerDoc':','x':120,'y':280,'wires':[['1b003892.f6fcb7']]},{'id':'167893a2.afc11c','type':'http response','z':'e39b5c88.c392a','name':','x':430,'y':280,'wires':[]},{'id':'1b003892.f6fcb7','type':'template','z':'e39b5c88.c392a','name':'Webpage','field':'payload','fieldType':'msg','format':'html','syntax':'mustache','template':'n<!DOCTYPE HTML>n<html>nn <head>n <title>Websocket Test</title>n <script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>nn <script type='text/javascript'>nn var wsUriC = 'ws://192.168.11.205:1880/websocket';n var ws;n function wsConnectC() {n console.log('connect',wsUriC);n ws = new WebSocket(wsUriC);n ws.onmessage = function(msg) {n console.log(msg.data);n var msg=JSON.parse(msg.data);nn var Demand = parseInt(msg[0]);n var Frequency = parseInt(msg[1]);nn// update the cell contents with the utilisationn $('#Demand').html(Demand);n $('#Frequency').html(Frequency);nn } n ws.onopen = function() {n console.log('connected');n }n ws.onclose = function() {n setTimeout(wsConnectC,1000);n retryOpeningWebSocket();nn }nn }nn function retryOpeningWebSocket(){n if (retries < 2) { n setTimeout(openWebSocket, 1000); n retries++;n }n }nn n </script>n</head>n<body onload='wsConnectC();' onunload='ws.disconnect;'>nn<table align='center' class='inlineTable'>n<tr>nt<th>UK</th>n t<th>Demand</th>n <th>Frequency</th>n</tr>n<tr>n <td>Power</td>n <td id='Demand'> </td>n <td id='Frequency'> </td>nn</tr>nnnn</table>nn</html>','x':280,'y':280,'wires':[['167893a2.afc11c']]},{'id':'f2d81755.ce6f48','type':'function','z':'e39b5c88.c392a','name':'kill msg','func':'msg._session=';nreturn msg;','outputs':1,'noerr':0,'x':400,'y':140,'wires':[['7f996667.ad4648']]},{'id':'93f57ff5.c4d84','type':'function','z':'e39b5c88.c392a','name':'Calculate','func':'// does a simple text extract parse of the http output to provide ann// object containing the uk power demand, frequency and timennif (~msg.payload.indexOf('<span')) {n var dem = msg.payload.split('Demand:')[1].split('MW')[0];n var fre = msg.payload.split('Frequency:')[1].split('Hz')[0];nn var data = [];n data[0] = parseInt(dem.split('>')[1].split('<')[0]);n data[1] = parseFloat(fre.split('>')[1].split('<')[0]);n n msg.payload = JSON.stringify(data);n return msg;n}nreturn null;nnn','outputs':'1','noerr':0,'x':400,'y':60,'wires':[['7f996667.ad4648']]},{'id':'1345e463.36797c','type':'websocket in','z':'e39b5c88.c392a','name':','server':'b4215b01.c8ba88','client':','x':140,'y':140,'wires':[['f2d81755.ce6f48']]},{'id':'7f996667.ad4648','type':'websocket out','z':'e39b5c88.c392a','name':','server':'b4215b01.c8ba88','client':','x':630,'y':100,'wires':[]},{'id':'12d27aed.46ff35','type':'inject','z':'e39b5c88.c392a','name':','topic':','payload':'true','payloadType':'bool','repeat':'10','crontab':','once':false,'x':110,'y':60,'wires':[['42cae1f4.fbed5']]},{'id':'3a4a7760.383a28','type':'comment','z':'e39b5c88.c392a','name':'Webserver','info':','x':120,'y':240,'wires':[]},{'id':'4f96ad.f54a7954','type':'comment','z':'e39b5c88.c392a','name':'get some data and parse them into a JSON object','info':'Code ripped from the nodered.org nsecond flow example. ','x':330,'y':20,'wires':[]}] |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment