When a net.Server receives data that exceeds 1500 bytes (default mtu), the 'on data' event is executed with each fragment of the packet. Is there a way to receive the whole packet in a single 'on data' call?
Thanks.
Try this
var sys = require('sys');
var net = require('net');;
var socktimeout = 600000;
var svrport = your_port;
var svr = net.createServer(function(sock) {
var mdata = new Buffer(0);
//sys.puts('Connected: ' + sock.remoteAddress + ':' + sock.remotePort);
sock.setTimeout(socktimeout,function(){
sock.end("timeout");
sock.destroy();
});
sock.on('data', function(data) {
if(mdata.length != 0)
{
var tempBuf = Buffer.concat([mdata, data]);
mdata = tempBuf;
}
else
{
mdata = data;
}
var len=got_your_Packget_length(mdata);
if(mdata.length == len)
{
do_your_job(mdata)
mdata = new Buffer(0);
}
});
sock.on('error', function(err) { // Handle the connection error.
sys.puts('error: ' + err +'\n');
});
});
svr.listen(svrport);
Related
Azure IoT Hub on server side and Azure IoT Client SDK on Device side, we are found 60-65 seconds delay and
drop between device sent a status message over MQTT and message was received by IoT Hub.
In some cases, we are seeing 1-2 minutes delay when IoT Hub enqueued the message.
Below is the code for communication with device to device
//'use strict';
//DeviceToCloudMessages libraries
var EventHubClient = require('azure-event-hubs').Client;
//var base64 = require('base-64');
//*NK+1 19-02-18 CloudToDeviceMessage Libraries
var Registry = require('azure-iothub').Registry;
//CloudToDeviceMessage libraries
var Client = require('azure-iothub').Client;
var Message = require('azure-iot-common').Message;
//*NK+1 13-03-18 Get constant values stored in GlobalConstants js
var constant = require('./GlobalConstants');
var connectionString = constant.connectionString;
var serviceClient = Client.fromConnectionString(connectionString);
//*NK+ 16-03-18 Open service cline
serviceClient.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Open connection: ');
}
});
//*NK+1 19-02-18 Create registry for connection string
var registry = Registry.fromConnectionString(connectionString);
//To prevent memory leak
require('events').EventEmitter.defaultMaxListeners = Infinity;
//To check device connection state
var request = require('request');
auth = constant.authorization;
//Datetime library
date = require('date-and-time');
var moment = require("moment");
//For log
const fs = require('fs');
var datetime = require('node-datetime');
//For database connection
var sql = require('mssql');
var config =
{
user: constant.sUsername, // update me
password: constant.sPassword, // update me
server: constant.sServer, // update me
database: constant.sDatabase,
options: {
encrypt: true
},
//*start 30-01-18 set resource request timeout
connectionTimeout: 0,
requestTimeout: 0,
pool: {
max: 100,
min: 1,
idleTimeoutMillis: 30000
}
//*end 30-01-18 set resource request timeout
}
//*NK+1 23-03-18 declare flag false
//var isLog = false;
// sql.close();
//SQL starts
sql.connect(config).then(function (err) {
if (err) {
//console.log('sql CONNECTION ERROR');
console.log(err);
}
console.log('Opening connection');
var printError = function (err) {
logmsg(err.message);
logmsg('Error from event hub:', err.message);
startReceiver();
};
function logmsg(msg)
{
//if(isLog){
try {
//console.log(msg);
var dt = datetime.create();
var formatted = dt.format('Y-m-d H:M:S');
var date = dt.format('Y-m-d');
var checkhr = dt.format('H');
var filepath = 'D:/home/site/wwwroot/WebJobLogs/SendD2DMsgLog_' +date +'_' +checkhr + '-00-00' +'.txt';
fs.appendFile(filepath, formatted + " " + msg + "\n");
} catch(error) {
//console.log(error);
}
//}
}
function printResultFor(op) {
try {
return function printResult(err, res) {
if (err)
{
logmsg(op + ' error: ' + err.toString());
}
if (res)
{
logmsg(op + ' status: ' + res.constructor.name);
}
};
} catch (exception) {
logmsg("Print result function " + exception)
}
}
function receiveFeedback(err, receiver) {
receiver.on('message', function (msg) {
logmsg('IOT Feedback message:')
logmsg(msg.getData().toString('utf-8'));
});
}
//*NK+start 19-02-18 for firmware update
function queryTwinFWUpdateReported(deviceToUpdate) {
logmsg('deviceToUpdate : ' + deviceToUpdate);
logmsg('deviceToUpdate : ' + deviceToUpdate);
registry.getTwin(deviceToUpdate, function(err, twin){
if (err) {
//console.error('Could not query twins: ' + err.constructor.name + ': ' + err.message);
logmsg('Could not query twins: ' + err.constructor.name + ': ' + err.message);
} else {
//console.log('queryTwinFWUpdateReported: ' + (JSON.stringify(twin.properties.reported.iothubDM.firmwareUpdate)) + "\n");
//logmsg((JSON.stringify(twin.properties.reported.iothubDM.firmwareUpdate)) + "\n");
}
});
}
function startFirmwareUpdateDevice (linkString, deviceToUpdate) {
//console.log('linkString: ' + linkString);
var params = {
FwPackageUri: linkString
};
var methodName1 = "FirmwareUpdate";
var payloadData = JSON.stringify(params);
var payloadData1 = JSON.parse(payloadData);
logmsg('payloadData : ' + payloadData1);
var methodParams = {
methodName: methodName1,
payload: payloadData1,
timeoutInSeconds: 3600//,
//responseTimeoutInSeconds: 3600
};
//var methodParamsTest = JSON.stringify(methodParams);
//var objmethodParams = JSON.parse(methodParamsTest);
logmsg('methodParams : ' + JSON.stringify(methodParams));
//console.log('methodParams : ' + JSON.stringify(methodParams));
serviceClient.invokeDeviceMethod(deviceToUpdate, methodParams, function(err, result) {
if (err) {
//console.log('Could not start the firmware update on the device: ' + err.message);
//console.log('Device: ' + err.stack);
logmsg('Could not start the firmware update on the device: ' + err.message);
}else{
logmsg('success result : ' + JSON.stringify(result));
//console.log('success result : ' + JSON.stringify(result));
}
});
}
//*NK+end 19-02-18 for firmware update
var printMessage = function (message) {
logmsg('Message received: ');
try {
var chunk_msg = JSON.stringify(message.body);
logmsg(chunk_msg);
var obj = JSON.parse(chunk_msg);
//*NK+start 19-02-18 firmware update code
if(obj.hasOwnProperty('firmwareUpdate')){
var firmwaretype= obj.controllerName;
var firmwaremodel= obj.modelNumber;
var firmwareDeviceId = obj.firmwareUpdate;
var appDeviceId = obj.appDeviceId;
new sql.Request()
.query("SELECT f.fw_version, f.[loc] FROM <<table>> f WHERE f.[Type] = '" + firmwaretype + "' ").then(function (recordset) {
var statusLocation = JSON.stringify(recordset);
var obj = JSON.parse(statusLocation);
var result = obj.recordset;
logmsg('result: ' + result);
console.log('result: ' + result);
if(result.length > 0){
console.log('fw_version: ' + result[0].fw_version);
console.log('loc: ' + result[0].loc);
console.log('obj.firmwareUpdate : ' + firmwareDeviceId);
console.log('obj.appDeviceId : ' + appDeviceId);
new sql.Request()
.query("select TOP 1 * from <<table>> where Id = '" + firmwareDeviceId + "' order by id desc ").then(function (recordset) {
var status = JSON.stringify(recordset);
var objstatus = JSON.parse(status);
var resultstatus = objstatus.recordset;
var datastatus = resultstatus[0];
var chunkstatus = JSON.stringify(datastatus);
var objnewstatus = JSON.parse(chunkstatus);
var endDate = objnewstatus.last_update;
logmsg('controller last status date ' + endDate);
console.log('controller last status date ' + endDate);
now = new Date();
var currentDate = date.format(now, 'YYYY-MM-DD HH:mm:ss');
logmsg('server current date ' + currentDate);
console.log('server current date ' + currentDate);
var start_date = moment(currentDate, 'YYYY-MM-DD HH:mm:ss');
var end_date = moment(endDate, 'YYYY-MM-DD HH:mm:ss');
var duration = moment.duration(start_date.diff(end_date));
var hours = duration.asHours();
//var seconds = Math.abs(hours * 3600);
var seconds = Math.abs(hours * 3600);
logmsg('difference in seconds ' + seconds);
console.log('difference in seconds ' + seconds);
if (seconds < 40)
{
logmsg('fw_version: ' + result[0].fw_version);
logmsg('loc: ' + result[0].loc);
console.log('fw_version: ' + result[0].fw_version);
console.log('loc: ' + result[0].loc);
startFirmwareUpdateDevice(result[0].loc, firmwareDeviceId);
setInterval(function() {queryTwinFWUpdateReported(firmwareDeviceId);}, 1000);
}
else{
//format failure msg to send to app
var result = '{"response_status":"true","device_id":"' + firmwareDeviceId + '"}';
logmsg('Sending message to app:' + result);
console.log('Sending message to app:' + result);
var message = new Message(result);
serviceClient.send(appDeviceId, message, printResultFor('send'));
logmsg('Feedback Message for firmware upgrade failure sent to app ' + appDeviceId);
console.log('Feedback Message for firmware upgrade failure sent to app ' + appDeviceId);
}
}).catch(function (err) {
logmsg('firmware upgrade: ' + err);
});
}
}).catch(function (err) {
logmsg(err);
});
}
//*NK+end 19-02-18
if (obj.hasOwnProperty('command'))
{
logmsg('command exist');
console.log('command exist');
//Send Msg to device
//serviceClient.open(function (err) {
//if (err) {
// console.error('Could not connect: ' + err.message);
//} else {
// logmsg('Service client connected');
//*NK+1 23-03-18 Commented feedback receiver
//serviceClient.getFeedbackReceiver(receiveFeedback);
message.ack = 'full';
//message.messageId = "My Message ID";
//*NK+1 20-02-18 Update messageId
console.log('obj.messageId : ' + obj.messageId);
message.messageId = obj.messageId;
//Check the status of controller before sending message and return message to user in case of failure
var targetDevice = obj.deviceId;
new sql.Request()
.query("select TOP 1 * from <<table>> where Id = '" + targetDevice + "' order by id desc ").then(function (recordset) {
var status = JSON.stringify(recordset);
var objstatus = JSON.parse(status);
var resultstatus = objstatus.recordset;
var datastatus = resultstatus[0];
var chunkstatus = JSON.stringify(datastatus);
var objnewstatus = JSON.parse(chunkstatus);
var endDate = objnewstatus.last_update;
logmsg('controller last status date ' + endDate);
console.log('controller last status date ' + endDate);
now = new Date();
var currentDate = date.format(now, 'YYYY-MM-DD HH:mm:ss');
logmsg('server current date ' + currentDate);
console.log('server current date ' + currentDate);
var start_date = moment(currentDate, 'YYYY-MM-DD HH:mm:ss');
var end_date = moment(endDate, 'YYYY-MM-DD HH:mm:ss');
var duration = moment.duration(start_date.diff(end_date));
var hours = duration.asHours();
//var seconds = Math.abs(hours * 3600);
var seconds = Math.abs(hours * 3600);
logmsg('difference in seconds ' + seconds);
console.log('difference in seconds ' + seconds);
if (seconds <= 65)
{
var data = obj.command;
//format msg to send to controller
var str1 = '{"Name":"Play","Parameters":{"Data":"';
var res = str1.concat(data);
var str2 = '"}}';
var result = res.concat(str2);
var message = new Message(result);
logmsg('Sending message to controller: ' + message.getData());
console.log('Sending message to controller: ' + message.getData());
serviceClient.send(targetDevice, message, printResultFor('send'));
logmsg('Message sent to controller ' + targetDevice);
console.log('Message sent to controller ' + targetDevice);
}
else
{
//*NK+start 29-03-18 Check reset command and send to the server
var data_command = obj.command;
if(data_command == "<<RESETDEVICECOMMAD>>"){
//format msg to send to controller
var str1 = '{"Name":"Play","Parameters":{"Data":"';
var res = str1.concat(data_command);
var str2 = '"}}';
var result = res.concat(str2);
var message = new Message(result);
logmsg('Reset message is: ' + message.getData());
console.log('Reset message is: ' + message.getData());
serviceClient.send(targetDevice, message, printResultFor('send'));
logmsg('Reset message sent to controller ' + targetDevice);
console.log('Reset message sent to controller ' + targetDevice);
}
//*NK+start 29-03-18 Check reset command and send to the server
if (obj.hasOwnProperty('appDeviceId'))
{
//Get app device id
var AppDeviceId = obj.appDeviceId;
//format failure msg to send to app
var result = '{"response_status":"false","device_id":"' + targetDevice + '"}';
logmsg('Sending message to app:' + result);
console.log('Sending message to app:' + result);
var message = new Message(result);
serviceClient.send(AppDeviceId, message, printResultFor('send'));
logmsg('Feedback Message for command failure sent to app ' + AppDeviceId);
console.log('Feedback Message for command failure sent to app ' + AppDeviceId);
}
}
}).catch(function (err) {
logmsg(err);
});
// }
//});
}
else if (obj.hasOwnProperty('u32SH'))
{
// console.log('json exist');
var deviceId = obj.deviceId;
var data = chunk_msg;
var datetime = new Date();
//SQL to get list of device ids
new sql.Request()
.query("select * from <<table>> where id in (select id from <<table>> where id in (select id from <<table>> where Id = '" + deviceId + "')) and connectionState = 'Connected' ").then(function (recordset) {
var chunk = JSON.stringify(recordset);
var obj = JSON.parse(chunk);
var result = obj.recordset;
var message = new Message(chunk_msg);
message.ack = 'full';
message.messageId = "My Message ID";
logmsg('Sending message: ' + message.getData());
for (var i = 0, len = result.length; i < len; i++)
{
var data = result[i];
var chunk1 = JSON.stringify(data);
var obj1 = JSON.parse(chunk1);
//Get device id of user
var targetDevice = obj1.device_id;
serviceClient.send(targetDevice, message, printResultFor('send'));
logmsg('Message sent to user ' + targetDevice);
} //for loop ends
}).catch(function (err) {
logmsg(err);
}); //1st sql ends
} //else if for json object check ends
//*NK++ 06-03-18 update controller table
var macId = obj.deviceId;
updateControllerFirmwareVersion(macId);
} catch (exception) {
logmsg("Handling exception....");
logmsg(exception);
}
}; //printMessage function ends
//*NK+start 05-03-18 update controller firmware version
function updateControllerFirmwareVersion(deviceId){
registry.getTwin(deviceId, function(err, twin){
if (err) {
//console.log('Could not query twins: ' + err.constructor.name + ': ' + err.message);
console.error('Could not query twins: ' + err.constructor.name + ': ' + err.message);
} else {
logmsg((JSON.stringify(twin.properties.reported.AzureFwVersion)) + "\n");
//console.log((JSON.stringify(twin.properties.reported.AzureFwVersion)) + "\n");
//var azureFwVersion = twin.properties.reported.AzureFwVersion.replace('Azure_Sns_DM V','');
var azureFwVersion = twin.properties.reported.AzureFwVersion;
var s = azureFwVersion.lastIndexOf(".");
var res = azureFwVersion.substring(s+1, azureFwVersion.length);
//console.log('azureFwVersion: ' + res);
var firmwareV = Number(res).toString(16).toUpperCase() + '00';
//console.log('hex : ' + firmwareV);
logmsg('hex : ' + firmwareV);
new sql.Request()
.query("select fw_version from <<table>> where Id = '" + deviceId + "'").then(function (recordset) {
logmsg('Get current controller table firmware version');
var currentData = JSON.stringify(recordset);
var obj = JSON.parse(currentData);
var resultstatus = obj.recordset;
var datastatus = resultstatus[0];
var chunkstatus = JSON.stringify(datastatus);
var objnewstatus = JSON.parse(chunkstatus);
if(objnewstatus.fw_version != firmwareV){
console.log('Available firmware is ' + firmwareV);
logmsg('Available firmware is ' + firmwareV);
new sql.Request()
.query("UPDATE <<table>> SET fw_version = '" + firmwareV + "' where Id = '" + deviceId + "'").then(function (recordset) {
logmsg('Updated Controller table for firmware');
//console.log('Updated Controller table for firmware');
}).catch(function (err) {
logmsg('Qry to update Controller table for firmware' + err);
//console.log('Qry to update Controller table for firmware' + err);
});
}
}).catch(function (err) {
logmsg('Qry to update Controller table for firmware' + err);
//console.log('Qry to update Controller table for firmware' + err);
});
}
});
}
//*NK+end 05-03-18 update controller firmware version
var startReceiver = function () {
try {
logmsg('Receiver on');
var client = EventHubClient.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
return partitionIds.map(function (partitionId) {
return client.createReceiver('$Default', partitionId, {'startAfterTime': Date.now()}).then(function (receiver) {
logmsg('Created partition receiver: ' + partitionId)
// receiver.on('errorReceived', printError);
// receiver.on('message', printMessage);
receiver.on('errorReceived', function callback() {
// console.log('Error Received in receiver');
client.close()
startReceiver()
});
receiver.on('message', printMessage);
});
})
})
// .catch(printError);
.catch(function callback() {
logmsg('Error Received in catch');
client.close()
startReceiver()
});
} catch (exception) {
logmsg("startReceiver function " + exception)
}
};
startReceiver();
}); //sql connection ends
Question:
1. What is the reason for this late/delay/drop in message reaching to IoT Hub?
2. Is there any issue on node js code?
I am using following node js packages :
{
"name": "Haven",
"version": "1.0.0",
"description": "",
"main": "AppToServerToDevice.js",
"dependencies": {
"azure-event-hubs": "0.0.8",
"azure-iot-device": "^1.2.1",
"azure-iot-device-mqtt": "^1.2.2",
"azure-iothub": "^1.2.3",
"base-64": "^0.1.0",
"date-and-time": "^0.6.2",
"heapdump": "^0.3.9",
"moment": "^2.20.1",
"mssql": "^4.1.0",
"tedious": "^2.1.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
IOT Hub details :
IoT Hub details:
Pricing tier: S2 standard
IoT Hub units: 1
SQL database Pricing tier is Standard: S3
Please suggest.
Please see below sample log.
2018-04-02 14:00:18 Message received:
2018-04-02 14:00:23 send status: MessageEnqueued
2018-04-02 14:00:23 send status: MessageEnqueued
2018-04-02 14:00:23 send status: MessageEnqueued
2018-04-02 14:00:23 send status: MessageEnqueued
2018-04-02 14:00:25 {"deviceId":"<<FAKE_ID>>","messageId":54,"u32SH":1094861843,"u32SVN":1215,"u32CVN":645,"u32SRS":2,"u32EVT_ID":9,"u8RSSI":94,"u8C1TID":0,"u8C1ETID":1,"u8C1STId":0,"u8C1ATId":1,"u8C1Rt":3,"u8C1B":36,"u8C1WL":0,"u8C1WB":36,"u8C1SB":68,"u8C1PB":0,"u8C2TID":0,"u8C2ETID":1,"u8C2STId":0,"u8C2ATId":2,"u8C2Rt":11,"u8C2B":99,"u8C2WL":11,"u8C2WB":99,"u8C2SB":99,"u8C2PB":99,"ts":"2018-04-02T14:00:23Z"}
2018-04-02 14:00:25 Message sent to user <<FAKE_ID>>
2018-04-02 14:00:25 Message sent to user <<FAKE_ID>>
2018-04-02 14:00:26 IOT Feedback message:
2018-04-02 14:00:26 [{"originalMessageId":"My Message ID","description":"Success","deviceGenerationId":"636573862820707504","deviceId":"34cebb0dd9eab292","enqueuedTimeUtc":"2018-04-02T14:00:18.6513817Z","statusCode":"Success"},{"originalMessageId":"My Message ID","description":"Success","deviceGenerationId":"636573862820707504","deviceId":"34cebb0dd9eab292","enqueuedTimeUtc":"2018-04-02T14:00:22.9953077Z","statusCode":"Success"}]
So I have set up a web RTC connection, nearly fully understand the steps and mechanisms involved and have a test out on the web with a full SSL certificate (so I can use it on chrome) but It doesn't ever seem to work with computers that are not under the same wireless connection/router.
In my test I have logged that there are ICE candidates generated and that the two browsers are communicating but I get no video feed except for when the two trying to communicate are under the same connection.
Browser Side to set up webRTC:
var socket = io.connect();
var isChannelReady;
var isInitiator = false;
var isStarted = false;
var localStream;
var pc;
var remoteStream;
var turnReady;
var pc_config = {'iceServers': [{'url': 'stun:stun.l.google.com:19302'}]};
var pc_constraints = {'optional': [{'DtlsSrtpKeyAgreement': true}]};
// Set up audio and video regardless of what devices are present.
// var sdpConstraints = {'mandatory': {
// 'OfferToReceiveAudio':true,
// 'OfferToReceiveVideo':true }};
var sdpConstraints = {
optional: [],
'mandatory': {
'OfferToReceiveAudio': true,
'OfferToReceiveVideo': true
}
};
function sendMessage(message){
console.log('Client sending message: ', message);
// if (typeof message === 'object') {
// message = JSON.stringify(message);
// }
socket.emit('message', message);
}
socket.on('message', function (message){
console.log('Client received message:', message);
if (message === 'got user media') {
maybeStart();
} else if (message.type === 'offer') {
if (!isInitiator && !isStarted) {
maybeStart();
}
pc.setRemoteDescription(new RTCSessionDescription(message), function(){doAnswer()});
} else if (message.type === 'answer' && isStarted) {
pc.setRemoteDescription(new RTCSessionDescription(message));
} else if (message.type === 'candidate' && isStarted) {
var candidate = new RTCIceCandidate({
sdpMLineIndex: message.label,
candidate: message.candidate
});
pc.addIceCandidate(candidate);
} else if (message === 'bye' && isStarted) {
handleRemoteHangup();
}
});
////////////////////////////////////////////////////
var localVideo = document.getElementById('localVideo');
var remoteVideo = document.getElementById('remoteVideo');
function handleUserMedia(stream) {
console.log('Adding local stream.');
console.log(localVideo);
localVideo.src = window.URL.createObjectURL(stream);
localStream = stream;
sendMessage('got user media');
if (isInitiator) {
maybeStart();
}
}
function handleUserMediaError(error){
console.log('getUserMedia error: ', error);
}
// window.onload = function () {
// var localVideo = document.getElementById('localVideo');
// var remoteVideo = document.getElementById('mehh');
// }
var constraints = {video: true};
navigator.getUserMedia(constraints, handleUserMedia, handleUserMediaError);
console.log('Getting user media with constraints', constraints);
if (location.hostname != "localhost") {
requestTurn('https://computeengineondemand.appspot.com/turn?username=41784574&key=4080218913');
}
function maybeStart() {
if (!isStarted && typeof localStream != 'undefined' && isChannelReady) {
createPeerConnection();
pc.addStream(localStream);
isStarted = true;
console.log('isInitiator', isInitiator);
if (isInitiator) {
doCall();
}
}
}
window.onbeforeunload = function(e){
sendMessage('bye');
}
/////////////////////////////////////////////////////////
function createPeerConnection() {
try {
pc = new RTCPeerConnection(null);
pc.onicecandidate = handleIceCandidate;
pc.onaddstream = handleRemoteStreamAdded;
pc.onremovestream = handleRemoteStreamRemoved;
console.log('Created RTCPeerConnnection');
} catch (e) {
console.log('Failed to create PeerConnection, exception: ' + e.message);
alert('Cannot create RTCPeerConnection object.');
return;
}
}
function handleIceCandidate(event) {
console.log('handleIceCandidate event: ', event);
if (event.candidate) {
sendMessage({
type: 'candidate',
label: event.candidate.sdpMLineIndex,
id: event.candidate.sdpMid,
candidate: event.candidate.candidate});
} else {
console.log('End of candidates.');
}
}
function handleRemoteStreamAdded(event) {
console.log('Remote stream added.');
remoteVideo.src = window.URL.createObjectURL(event.stream);
remoteStream = event.stream;
}
function handleCreateOfferError(event){
console.log('createOffer() error: ', e);
}
function doCall() {
console.log('Sending offer to peer');
pc.createOffer(setLocalAndSendMessage, handleCreateOfferError);
}
function doAnswer() {
console.log('Sending answer to peer.');
pc.createAnswer(setLocalAndSendMessage, function(err){if(err) throw err;}, sdpConstraints);
}
function setLocalAndSendMessage(sessionDescription) {
// Set Opus as the preferred codec in SDP if Opus is present.
sessionDescription.sdp = preferOpus(sessionDescription.sdp);
pc.setLocalDescription(sessionDescription);
console.log('setLocalAndSendMessage sending message' , sessionDescription);
sendMessage(sessionDescription);
}
function requestTurn(turn_url) {
var turnExists = false;
for (var i in pc_config.iceServers) {
if (pc_config.iceServers[i].url.substr(0, 5) === 'turn:') {
turnExists = true;
turnReady = true;
break;
}
}
if (!turnExists) {
console.log('Getting TURN server from ', turn_url);
// No TURN server. Get one from computeengineondemand.appspot.com:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(){
if (xhr.readyState === 4 && xhr.status === 200) {
var turnServer = JSON.parse(xhr.responseText);
console.log('Got TURN server: ', turnServer);
pc_config.iceServers.push({
'url': 'turn:' + turnServer.username + '#' + turnServer.turn,
'credential': turnServer.password
});
turnReady = true;
}
};
xhr.open('GET', turn_url, true);
xhr.send();
}
}
function handleRemoteStreamAdded(event) {
console.log('Remote stream added.');
remoteVideo.src = window.URL.createObjectURL(event.stream);
remoteStream = event.stream;
}
function handleRemoteStreamRemoved(event) {
console.log('Remote stream removed. Event: ', event);
}
function hangup() {
console.log('Hanging up.');
stop();
sendMessage('bye');
}
function handleRemoteHangup() {
// console.log('Session terminated.');
// stop();
// isInitiator = false;
}
function stop() {
isStarted = false;
// isAudioMuted = false;
// isVideoMuted = false;
pc.close();
pc = null;
}
///////////////////////////////////////////
// Set Opus as the default audio codec if it's present.
function preferOpus(sdp) {
var sdpLines = sdp.split('\r\n');
var mLineIndex;
// Search for m line.
for (var i = 0; i < sdpLines.length; i++) {
if (sdpLines[i].search('m=audio') !== -1) {
mLineIndex = i;
break;
}
}
if (mLineIndex === null || mLineIndex === undefined) {
return sdp;
}
// If Opus is available, set it as the default in m line.
for (i = 0; i < sdpLines.length; i++) {
if (sdpLines[i].search('opus/48000') !== -1) {
var opusPayload = extractSdp(sdpLines[i], /:(\d+) opus\/48000/i);
if (opusPayload) {
sdpLines[mLineIndex] = setDefaultCodec(sdpLines[mLineIndex], opusPayload);
}
break;
}
}
// Remove CN in m line and sdp.
sdpLines = removeCN(sdpLines, mLineIndex);
sdp = sdpLines.join('\r\n');
return sdp;
}
function extractSdp(sdpLine, pattern) {
var result = sdpLine.match(pattern);
return result && result.length === 2 ? result[1] : null;
}
// Set the selected codec to the first in m line.
function setDefaultCodec(mLine, payload) {
var elements = mLine.split(' ');
var newLine = [];
var index = 0;
for (var i = 0; i < elements.length; i++) {
if (index === 3) { // Format of media starts from the fourth.
newLine[index++] = payload; // Put target payload to the first.
}
if (elements[i] !== payload) {
newLine[index++] = elements[i];
}
}
return newLine.join(' ');
}
// Strip CN from sdp before CN constraints is ready.
function removeCN(sdpLines, mLineIndex) {
console.log(sdpLines[mLineIndex])
var mLineElements = sdpLines[mLineIndex].split(' ');
// Scan from end for the convenience of removing an item.
for (var i = sdpLines.length-1; i >= 0; i--) {
var payload = extractSdp(sdpLines[i], /a=rtpmap:(\d+) CN\/\d+/i);
if (payload) {
var cnPos = mLineElements.indexOf(payload);
if (cnPos !== -1) {
// Remove CN payload from m line.
mLineElements.splice(cnPos, 1);
}
// Remove CN line in sdp
sdpLines.splice(i, 1);
}
}
sdpLines[mLineIndex] = mLineElements.join(' ');
return sdpLines;
}
/////////////////////////////////////////////
room = prompt("Enter room name:");
var socket = io.connect();
if (room !== '') {
console.log('Create or join room', room);
socket.emit('create or join', room);
}
socket.on('created', function (room){
console.log('Created room ' + room);
isInitiator = true;
});
socket.on('full', function (room){
console.log('Room ' + room + ' is full');
});
socket.on('join', function (room){
console.log('Another peer made a request to join room ' + room);
console.log('This peer is the initiator of room ' + room + '!');
isChannelReady = true;
});
socket.on('joined', function (room){
console.log('This peer has joined room ' + room);
isChannelReady = true;
});
socket.on('log', function (array){
console.log.apply(console, array);
});
////////////////////////////////////////////////
And here is everything on the server side pertaining to webRTC (using socket io):
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
var io = require('socket.io').listen(server);
io.sockets.on('connection', function (socket){
console.log('connected');
// convenience function to log server messages on the client
function log(){
var array = [">>> Message from server: "];
for (var i = 0; i < arguments.length; i++) {
array.push(arguments[i]);
}
socket.emit('log', array);
}
socket.on('message', function (message) {
log('Got message:', message);
// for a real app, would be room only (not broadcast)
socket.broadcast.emit('message', message);
});
socket.on('create or join', function (room) {
//io.sockets.clients(room)
//io.of('/').in(room).clients
//io.sockets.adapter.rooms[room]
var numClients;
var ish = io.sockets.adapter.rooms[room];
(ish === undefined) ? numClients = 0 : numClients = ish.length
log('Room ' + room + ' has ' + numClients + ' client(s)');
log('Request to create or join room ' + room);
if (numClients === 0){
socket.join(room);
socket.emit('created', room);
} else if (numClients === 1) {
io.sockets.in(room).emit('join', room);
socket.join(room);
socket.emit('joined', room);
} else { // max two clients
socket.emit('full', room);
}
socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
});
});
I simply can't find any errors that prevent the streams from being sent...
More than willing to do a private chat so I can get moving on my project.
I was given a task to send JSON string from client to server and from server to client, whenever there is a new record found to send.
I decided to build TCP connection(suggest me if there is any other better way in Node.js) between server and client to transfer data.
The problem is, I was supposed to use a delimiter to separate JSON strings one from another. I am afraid what if the json string contains the delimiter string inside the object. I am looking for a better way to separate two JSON strings.
Below is my code. Please help me.
Client
var net = require('net')
, client = new net.Socket();
var chunk = ''
, dlim_index = -1
, delimit = '~~';
client.connect(config.Port, config.IpAddress, function () {
console.log('Server Connected');
client.write('CLIENTID:' + process.argv[2]);
client.write(delimit);
});
client.on('data', function (data) {
var recvData = data.toString().trim();
chunk += recvData;
dlim_index = chunk.indexOf(recvData);
console.log(data);
while (dlim_index > -1) {
var useData = chunk.substring(0, dlim_index);
if (useData == 'SUCCESS') {
controller.listenOutQueue(function (dataToSend) {
var object = JSON.parse(dataToSend);
client.write(dataToSend);
client.write(delimit);
});
}
else {
var record = JSON.parse(useData);
controller.insertIntoQueue(record, function (status) {
});
}
chunk = chunk.substring(dlim_index + 2);
dlim_index = chunk.indexOf(delimit);
}
});
client.on('close', function () {
console.log('Connection closed');
});
client.setTimeout(50000, function () {
//client.destroy();
});
Server
var net = require('net')
, server = net.createServer()
, delimit = '~~'
, clients = [];
controller.listenOutQueue(function (dataToSend) {
client.write(dataToSend);
client.write(delimit);
});
server.on('connection', function (socket) {
var chunk = '';
var dlim_index = -1;
socket.on('data', function (data) {
var recvData = data.toString().trim();
chunk += recvData;
dlim_index = chunk.indexOf(delimit);
while (dlim_index > -1) {
var useData = chunk.substring(0, dlim_index);
if (useData.substring(0, 9) == 'CLIENTID:') {
socket.clientid = useData.replace('CLIENTID:', '');
console.log('Client Id: ' + socket.clientid);
clients.push(socket);
var successMessage = "SUCCESS";
socket.write(successMessage);
socket.write(delimit);
}
else {
controller.insertIntoQueue(JSON.parse(useData), function (status) {
});
}
chunk = chunk.substring(dlim_index + 2);
dlim_index = chunk.indexOf(delimit);
}
});
socket.on('end', function () {
console.log('Connection Closed (' + socket.clientid + ')');
});
socket.on('error', function (err) {
console.log('SOCKET ERROR:', err);
});
});
server.listen(config.Port, config.IpAddress);
I have been trying to figure out a way to connect a WiFly from an Arduino to send some accelerometer data to my node.js server. Currently the way that I have it worked out is having three servers:
Http >> This is for clients purposes
Net server >> This is basically for TCP request, this is how my server receives the information from 3. WS websockets >> this takes the data from the Net server and streams it to the client side.
Here is the code:
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));
}
So the question is: Is there a cleaner way to do this just by using ws to handle the data from the WiFly and then sending it to the client?
Thanks in advance!
Not sure whether this will suit you and might be new to you but you could make use of MQTT, there are free brokers available which are very good and its relatively easy to set up and implement with Arduino equipped with WiFly Shield.
http://mqtt.org/
Hope this helps somewhat!
My goal is to create a parent process that streams messages to the child processes indefinitely. To test this, i tried the below code for 150M messages to a child process that does nothing.
(Possible Answer Edited Below) Why would this code run out of memory?
I am using the ofe module but I do not know what to look for in the resulting heapdump, what method could I have used to search the heapdump for clues?
code:
server.js
// After 20M messages i get:
// FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
var cp = require('child_process');
var xrange = require('xrange');
var maxChildren = require('os').cpus().length;
var util = require('util');
require('ofe').call();
var childError = function(err) {
console.log('Error: ' + err);
};
var childExit = function(code, signal) {
console.log('Exit Code: ' + code);
console.log('Exit Signal: ' + signal);
};
var childClose = function(code, signal) {
console.log('Close Code: ' + code);
console.log('Close Signal: ' + signal);
};
var childDisconnect = function() {
console.log('Disconnect');
};
var childMessage = function(msg, handle) {
console.log('Msg: ' + msg);
};
var createChild = function(){
var child = cp.fork("./child");
child.on('error', childError);
child.on('exit', childExit);
child.on('close', childClose);
child.on('disconnect', childDisconnect);
child.on('message', childMessage);
console.log("Child Created: " + child.pid);
return child;
}
var createChildren = function(){
var children = [];
xrange(maxChildren).each(function(i) {
children[i] = createChild();
});
return children;
}
var sendMessages = function(children) {
xrange(150000000).each(function(num) {
var idx = num % maxChildren;
if (num % 1000000 == 0) {
console.log(num);
}
children[idx].send(num);
});
};
child.js (contents)
process.on('message', function(msg) {});
Answer
Since this code is asynchronous , the parent process will send all the messages to the children without waiting for them to be processed, which seems to overload the clients. I believe the solution is to send a message back from the clients to 'pull' the next number after processing. Assuming this is the answer, I have a follow up question.
1) I'd like to write this but i'm not sure how to turn the xrange into a generator without the ability to yield (no-harmony nodejs), is there a callback solution? :
function getNextNumber(){
//pull the next available number from the 150M
}
child.on('message',function(msg) {
child.send(getNextNumber());
});
Followup
If I just wanted to iterate, this seems to be the way: https://stackoverflow.com/a/5784473/1578888 .
If I am reading this alternate answer correctly, it seems that implementing the xrange call as a true generator is not possible pre-harmony: https://stackoverflow.com/a/7442013/1578888
This is the code i ended up using (also renamed server.js to parent.js):
parent.js
var cp = require('child_process');
var xrange = require('xrange');
var maxChildren = require('os').cpus().length;
var util = require('util');
require('ofe').call();
var childError = function(err) {
console.log('Error: ' + err);
};
var childExit = function(code, signal) {
console.log('Exit Code: ' + code);
console.log('Exit Signal: ' + signal);
};
var childClose = function(code, signal) {
console.log('Close Code: ' + code);
console.log('Close Signal: ' + signal);
};
var childDisconnect = function() {
console.log('Disconnect');
};
var childMessage = function(msg, handle) {
//no output anymore!
//console.log('Msg: ' + msg);
};
var createChild = function(){
var child = cp.fork("./child.js");
child.on('error', childError);
child.on('exit', childExit);
child.on('close', childClose);
child.on('disconnect', childDisconnect);
child.on('message', childMessage);
console.log("Child Created: " + child.pid);
return child;
}
var getNextFn = (function () {
var i = 0
return function(cb) {
i = i + 1;
if (i < 150000000) {
return i;
} else {
return null;
}
}
})();
var createChildren = function(){
var children = [];
xrange(maxChildren).each(function(i) {
var child = createChild();
child.on('message', function(msg) {
var next = getNextFn();
if (next) {
if (next % 1000000 == 0) {
console.log(next + " " + new Date().toISOString());
}
child.send(next);
} else {
child.kill();
}
});
children[i] = child;
});
return children;
}
var c = createChildren();
child.js
process.on('message', function(msg) {
process.send({});
});
process.send({});