If a message comes in for server01, both server01 and server02's message events will be triggered. I thought the line
Socket.prototype = new events.EventEmitter;
would result in completly seperate event instances
Thanks for any help!
var events = require('events');
var uuid = require('uuid');
// Server class
function Socket (host) {
var self = this;
self.options = {
"serverHost": host,
"serverName": "server",
"clientName": uuid.v4()
};
self.socket = new require('zmq').socket('router');
self.socket.identity = self.options.clientName;
self.socket.connect('tcp://' + self.options.serverHost);
self.socket.on('message', function (sender, data) {
console.log('Sender: %s', self.options.clientName);
console.log('Data: %s', data.toString());
self.emit('message', sender, data);
});
setInterval(function () {
self.socket.send([self.options.serverName, uuid.v4()]);
}, 5000);
self.send = function (obj, callback) {
var status = true;
if(obj !== 'object') {
status = false;
} else {
self.socket.send([self.options.serverName, obj]);
}
if(callback === 'function') {
callback(status);
} else {
return status;
};
};
};
Socket.prototype = new events.EventEmitter;
// Userland
var server01 = new Socket('127.0.0.1:3000');
server01.on('message', function (sender, data) {
console.log('Server01: %s', data.toString());
});
var server02 = new Socket('127.0.0.1:3000');
server02.on('message', function (sender, data) {
console.log('Server02: %s', data.toString());
});
Here is an example of the output from this script
Sender: 14d36a66-a4e7-484a-9ce0-3f0d368a6986
Data: 03e6bb47-6af0-4700-9b95-7bbc310477f6
Server01: 03e6bb47-6af0-4700-9b95-7bbc310477f6
Server02: 03e6bb47-6af0-4700-9b95-7bbc310477f6
Sender: 59ec292e-abd2-4c9f-ac3e-2bf92c656fde
Data: d66cd320-c3f2-4842-b66b-1d89f656d32f
Server01: d66cd320-c3f2-4842-b66b-1d89f656d32f
Server02: d66cd320-c3f2-4842-b66b-1d89f656d32f
The problem is the way you manage inheritance. Correct JavaScript code for inheritance is:
Socket.prototype = Object.create(EventEmitter.prototype);
Related
Check this snippet:
function connect() {
var request = window.indexedDB.open('test', 1);
request.onerror = function(event) {
console.log('error');
};
request.onupgradeneeded = function (event) {
console.log('upgrade needed');
};
request.onsuccess = function(event) {
console.log('success');
};
}
connect();
When I put breakpoints on callbacks assignment lines (4, 8, 12) and run it in chrome debugger, the code inside callbacks will never fire. Contrary, this code will work correctly:
function connect2() {
var IDB = {
open: function() {
var req = {};
setTimeout(() => {
req.onsuccess();
}, 0)
return req;
}
}
var request = IDB.open();
request.onsuccess = function(event) {
console.log('onsuccess2');
};
}
connect2();
Do I understand correctly, that the case with IndexedDB.open and debugger breaks js event loop? What can be the reason for this?
It's working for me! onsuccess and onupgradeneeded are called!
See fiddle:
function connect() {
var request = window.indexedDB.open('test', 1);
request.onerror = function(event) {
console.log('error');
};
request.onupgradeneeded = function (event) {
console.log('upgrade needed');
};
request.onsuccess = function(event) {
console.log('success');
};
}
connect();
If you want to use more modular:
var APP = APP || {};
APP.Database = (function(){
return {
request : null,
init : function()
{
this.connect();
this.request.onerror = this.logError;
this.request.onupgradeneeded = this.onUpgradedNeeded;
this.request.onsuccess = this.onSuccessCallback;
},
connect : function()
{
this.request = window.indexedDB.open('test', 1);
},
onSuccessCallback : function(evt)
{
console.log('Successfully conneted!');
},
logError : function(evt)
{
console.log('error');
},
onUpgradedNeeded : function(evt)
{
console.log('upgrade needed');
}
};
})();
APP.Database.init();
I am executing a code in NodeJS child_process.
I used MailParser of Andris9.
I used console.log(JSON.stringify({obj:mail_object})); to get the data of mail_object to the parent.
In the parent, I have this code, JSON.parse(retVal);.
The error came up is "Unexpected end of Input".
This error only shows when the email I received is having an attachment.
If the email doesnt have attachment, there is no error.
Here is the parent method,
getEmails(){
let workerProcess = child_process.spawn('node', [basePath+'imports/workers/retrieveEmail.js']);
workerProcess.stdout.on('data', function (data) {
try{
let retVal = new Buffer(data).toString();
retVal = JSON.parse(retVal);
console.log(retVal);
if(typeof retVal.err == "undefined"){
console.log(retVal.obj.from[0].address);
Fiber(function () {
var objs = [];
if (typeof retVal.obj.attachments !== "undefined") {
console.log("Test passed");
retVal.obj.attachments.forEach(function (attachment) {
let future = new Future();
Fiber(function () {
Files.write(attachment.content, {
fileName: attachment.fileName,
type: attachment.contentType
}, function (error, fileRef) {
if (error) {
future.throw(new Meteor.Error(500, error.message));
} else {
...
}
});
}).run();
var bool = true;
if (bool = future.wait())
objs.push(bool);
});
}
...
}).run();
}else{
console.log(retVal.err);
}
}catch(e){
console.log(e);
}
});
workerProcess.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
workerProcess.on('close', function (code) {
console.log('child process exited with code ' + code);
});
},
I removed some unnecessary codes.
Here is my retrieveEmail.js,
...
client.on("retr", function (status, msgnumber, data, rawdata) {
if (status === true) {
var mailparser = new MailParser({
streamAttachments: false
});
var timeStamp = Math.floor(Date.now());
mailparser.on("attachment", function (attachment, mail) {
console.log(JSON.stringify({err:"testpassed1"}));
});
mailparser.on("end", function (mail_object) {
console.log(JSON.stringify({err:"testpassed2"}));
console.log(JSON.stringify({obj:mail_object}));
});
mailparser.write(data);
mailparser.end();
client.dele(msgnumber);
} else {
console.log("RETR failed for msgnumber " + msgnumber);
client.quit();
}
});
...
So I'm trying to send my data from my server code to my client code, yet it's returning undefined. Not sure what to do as I've been stuck here for a while.
I used these packages:
https://github.com/meteorhacks/npm
https://www.npmjs.com/package/wiki-infobox
Rappers = new Mongo.Collection(null)
var page = 'Tupac'
var language = 'en'
var rappers = null
var texts
var rappers2
if (Meteor.isClient) {
getGists = function getGists(user, callback) {
Meteor.call('getGists', user, callback);
}
Meteor.startup(function() {
rappers2 = []
function call(text, callback) {
Meteor.call('getWikiStuff', rappers2, function(err, result) {
console.log(result)
})
var timer = setTimeout(function() {
callback()
}, 4000)
}
function consoleit() {
console.log(rappers2)
}
call('hello', consoleit)
})
}
if (Meteor.isServer) {
Meteor.startup(function() {
Meteor.methods({
getWikiStuff: function(rappers3) {
var infobox = Meteor.npmRequire('wiki-infobox')
var bound = Meteor.bindEnvironment(function(callback) {
callback()
});
console.log("HERE")
bound(function() {
infobox(page, language, function(err, data) {
if (err) {
return
}
rappers = data.associated_acts
for (var x = 0; x < rappers.length; x++)
if (rappers[x].text != undefined) {
var yo = rappers[x].text
rappers3.push(yo)
}
for (var value of rappers3)
console.log(value)
})
})
return rappers3
}
})
})
}
I am using a javascript bridge to communicate between a hybrid web app and iOS using a webView.
my question is how to get the function ios_getUserID() from within the script to put a value to the variable UserID that exists above the javascript bridge function.
I have a web page and connected to it is a script js file. scripts.js looks like this:
var userID="";
//javascript ios bridge
window.onerror = function(err) {
log('window.onerror: ' + err)
}
function connectWebViewJavascriptBridge(callback) {
if (window.WebViewJavascriptBridge) {
callback(WebViewJavascriptBridge)
} else {
document.addEventListener('WebViewJavascriptBridgeReady', function() {
callback(WebViewJavascriptBridge)
}, false)
}
}
connectWebViewJavascriptBridge(function(bridge) {
var uniqueId = 1
function log(message, data) {
var log = document.getElementById('log')
var el = document.createElement('div')
el.className = 'logLine'
el.innerHTML = uniqueId++ + '. ' + message + ':<br/>' + JSON.stringify(data)
if (log.children.length) { log.insertBefore(el, log.children[0]) }
else { log.appendChild(el) }
}
bridge.init(function(message, responseCallback) {
log('JS got a message', message)
var data = { 'Javascript Responds':'Weeeeeeee!' }
log('JS responding with', data)
responseCallback(data)
})
bridge.registerHandler('testJavascriptHandler', function(data, responseCallback) {
log('ObjC called testJavascriptHandler with', data)
var responseData = { 'Javascript Says':'Right back atcha!' }
log('JS responding with', responseData)
responseCallback(responseData)
})
bridge.registerHandler('softUserID', function(data, responseCallback) {
log('softUserID ObjC called testJavascriptHandler with', data)
alert(data.userID);
var responseData = { 'Javascript Says':'super!' }
log('JS responding with', responseData)
responseCallback(responseData)
})
ios_getUserID = function(){
var obj = '{"action" : "getUserID"}';
//alert("crossing bridge");
var data = obj
log('JS sending message', data)
bridge.send(data, function(responseData) {
log('JS got response', responseData)
alert(responseData);
})
}
});
I have been trying to execute a number of tests for an API, for this example it required my account details to be updated. When I run the test the retrieveAccount call is sometimes run before my putRequest making the tests fail. What am I doing wrong?
var frisby = require('frisby');
var url = require('endpoints.js');
var auth = require('auth.js');
var oracledb = require('oracledb');
var dbConnect = require('dbconfig.js');
var myDetails = undefined;
var putRequest = function() {
frisby.create('Put update contact details - required values')
.put(url.myAccount, {
addressLine1: 'String',
addressTown: 'String',
addressCounty: 'String'
}, {json: true})
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.auth(auth.username, auth.password)
.toss();
}
var retrieveAccount = function() {
oracledb.getConnection(
{
user : dbConnect.user,
password : dbConnect.password,
connectString : dbConnect.connectString
},
function(err, connection)
{
if (err) {
console.error(err.message);
return;
}
connection.execute(
"SELECT addressLine1, addressTown, addressCounty "
+ "FROM accounts "
+ "WHERE account_id = 1",
function(err, result)
{
if (err) {
console.error(err.message);
return;
}
myDetails = JSON.stringify(result.rows);
myDetails = JSON.parse(myDetails);
});
});
}
var matchValues = function() {
frisby.create('Match Database and API Values')
.get(url.myAccount)
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.auth(auth.username1, auth.password1)
.afterJSON(function (body) {
expect(body.addressLine1).toMatch(myDetails[0][0])
expect(body.addressCounty).toMatch(myDetails[0][1])
expect(body.addressTown).toMatch(myDetails[0][0])
})
.toss();
}
function Promise(fn) {
var state = 'pending';
var value;
var deferred = null;
function resolve(newValue) {
value = newValue;
state = 'resolved';
if(deferred) {
handle(deferred);
}
}
function handle(handler) {
if(state === 'pending') {
deferred = handler;
return;
}
if(!handler.onResolved) {
handler.resolve(value);
return;
}
var ret = handler.onResolved(value);
handler.resolve(ret);
}
this.then = function(onResolved) {
return new Promise(function(resolve) {
handle({
onResolved: onResolved,
resolve: resolve
});
});
};
fn(resolve);
}
function sendRequest() {
return new Promise(function(resolve){
var value = putRequest();
resolve(value);
});
}
function readDatabase() {
return new Promise(function(resolve){
var value = retrieveAccount();
resolve(value);
});
}
function getAccount() {
return new Promise(function(resolve){
var value = matchValues();
resolve(value);
});
}
sendRequest()
.then(readDatabase)
.then(getAccount);
I recommend using sequenty instead of promises to execute synchronous REST calls.
sudo npm install sequenty
var sequenty = require('sequenty');
function f1(cb) // cb: callback by sequenty
{
frisby.create('Match Database and API Values')
.get(url.myAccount)
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.auth(auth.username1, auth.password1)
.afterJSON(function (body) {
expect(body.addressLine1).toMatch(myDetails[0][0])
expect(body.addressCounty).toMatch(myDetails[0][1])
expect(body.addressTown).toMatch(myDetails[0][0])
cb();
})
.toss();
}
function f2(cb)
{
frisby.create('Put update contact details - required values')
.put(url.myAccount, {
addressLine1: 'String',
addressTown: 'String',
addressCounty: 'String'
}, {json: true})
.expectStatus(200)
.expectHeaderContains('content-type', 'application/json')
.auth(auth.username, auth.password)
.toss();
}
sequenty.run([f1, f2]);