I am beginner in programming and I am trying the javascript classes, I want to call the boardCastinit function from the override function onConnMessage, but I am getting this error message, Please help in this issue.
boReferenceError: boardCastInit is not defined
websocket.js
class websocket extends webSocketModel {
constructor() {
let server = new Server();
let mongodb = new mongoDB();
super(server.server);
}
onConnMessage(message) {
let clients = this.clients;
boardCastInit(1);
}
boardCastInit(data){
console.log(data)
}
}
module.exports = websocket;
websocketModel.js
const ws = require('websocket').server;
class webSocketModel {
constructor(httpServer) {
if(!httpServer) throw 'Null Http Server';
this.websocket = new ws({ httpServer: httpServer, autoAcceptConnections: false });
this.websocket.on('request', this.onConnOpen.bind(this));
}
onConnOpen(request) {
var connection = request.accept('echo-protocol', request.origin);
console.log('Connection Accepted');
connection.on('message', this.onConnMessage);
connection.on('close', this.onConnClose);
}
onConnMessage(message) {
if (message.type === 'utf8') {
console.log(message.utf8Data);
} else if (message.type == 'binary') {
console.log(message.binaryData.length + 'bytes');
}
}
onConnClose(reasonCode, description) {
console.log('Connection Closed');
}
}
module.exports = webSocketModel;
just change boardCastInit(1) to this.boardCastInit(1)
onConnMessage(message) {
let clients = this.clients;
this.boardCastInit(1);
}
You should be calling it from the class this reference:
class websocket extends webSocketModel {
constructor() {
let server = new Server();
let mongodb = new mongoDB();
super(server.server);
}
onConnMessage(message) {
let clients = this.clients;
this.boardCastInit(1);
}
boardCastInit(data){
console.log(data)
}
}
module.exports = websocket;
You are missing this (should be this.boardCastInit(1)).
This could be a binding issue. Perhaps you want to use arrow function instead on your onConnMessage method:
onConnMessage = (message) => {
let clients = this.clients;
this.boardCastInit(1);
}
This will ensure that this refers to the websocket class which has the boardCastInit method defined.
Try binding the boardCastInit() function inside the constructor like this.
constructor() {
let server = new Server();
let mongodb = new mongoDB();
super(server.server);
this.boardCastInit = this.boardCastInit.bind(this);
}
Then call it from the this reference.
onConnMessage(message) {
let clients = this.clients;
this.boardCastInit(1);
}
Related
I am trying to synchronise a singleton.
I would need to make this method like the equivalent of synchronized in java.
What happens to me is that because the socket takes a while, if the first two requests are very close together I get two websockets created. (Then from the third one onwards it takes the instance correctly).
import io from 'socket.io-client';
export default class SocketIo {
static socket = null;
static instance = null;
async initialize() {
this.socket = await io(`http://${ip}:10300/`, {
transports: ['websocket'],
});
}
static async getInstance() {
logger.info('socketIo.api.getInstance: BEGIN');
if (!this.instance) {
logger.info('socketIo.api.getInstance: creating new socket instance...');
try {
const o = new SocketIo();
await o.initialize();
this.instance = o;
logger.info('socketIo.api.getInstance: socket instance created SUCCESSFULLY');
} catch (e) {
moaLog('socketIo.api.getInstance: ERROR: ', e);
throw e;
}
} else {
logger.info('socketIo.api.getInstance: a socket instance already exists, reusing that one');
}
logger.info('socketIo.api.getInstance: END');
return this.instance;
}
}
in main.js
var socket1 = SocketIo.getInstance();
var socket2 = SocketIo.getInstance();
// ... after a while
var socket3 = SocketIo.getInstance();
2022-06-16T17:53:40.658Z: socketIo.api.getInstance: BEGIN
2022-06-16T17:53:40.660Z: socketIo.api.getInstance: creating new socket instance...
2022-06-16T17:53:41.140Z: socketIo.api.getInstance: BEGIN
2022-06-16T17:53:41.141Z: socketIo.api.getInstance: creating new socket instance...
2022-06-16T17:53:41.379Z: socketIo.api.getInstance: socket instance created SUCCESSFULLY
2022-06-16T17:53:41.382Z: socketIo.api.getInstance: END
2022-06-16T17:53:41.411Z: socketIo.api.getInstance: socket instance created SUCCESSFULLY
2022-06-16T17:53:41.415Z: socketIo.api.getInstance: END
...
2022-06-16T17:56:13.076Z: socketIo.api.getInstance: BEGIN
2022-06-16T17:56:13.078Z: socketIo.api.getInstance: a socket instance already exists, reusing that one
2022-06-16T17:56:13.079Z: socketIo.api.getInstance: END
And from server view I see two websocket connections.
Any ideas?
Below is an attempt to synchronize your singleton. The idea is to store the o.intitialize promise and check if it already has been acquired before.
I added an uid, a random value set in initialize to show that only single instance is created.
class SocketIo {
static instance = null;
static _lock = null;
async initialize() {
this.uid = Math.random();
}
static async getInstance() {
console.log('socketIo.api.getInstance: BEGIN');
if (!this.instance) {
console.log('socketIo.api.getInstance: creating new socket instance...');
const o = new SocketIo();
if (!this._lock) {
this._lock = o.initialize();
await this._lock;
this.instance = o;
console.log('socketIo.api.getInstance: socket instance created SUCCESSFULLY');
this._lock = null;
} else {
await this._lock;
}
}
console.log('socketIo.api.getInstance: END');
return this.instance;
}
}
async function Main() {
var socket1 = SocketIo.getInstance();
var socket2 = SocketIo.getInstance();
console.log((await socket1).uid);
console.log((await socket2).uid);
}
Main()
Compare it to your version:
class SocketIo {
static instance = null;
async initialize() {
this.uid = Math.random();
}
static async getInstance() {
console.log('socketIo.api.getInstance: BEGIN');
if (!this.instance) {
console.log('socketIo.api.getInstance: creating new socket instance...');
const o = new SocketIo();
await o.initialize();
this.instance = o;
console.log('socketIo.api.getInstance: socket instance created SUCCESSFULLY');
}
console.log('socketIo.api.getInstance: END');
return this.instance;
}
}
async function Main() {
var socket1 = SocketIo.getInstance();
var socket2 = SocketIo.getInstance();
console.log((await socket1).uid);
console.log((await socket2).uid);
}
Main()
Hope this is what suits you.
I solved using async-lock.
import AsyncLock from 'async-lock';
const lock = new AsyncLock();
export default class SocketIo {
// ...
static async getInstance() {
logger.info('socketIo.api.getInstance: BEGIN');
if (!this.instance) {
logger.info('socketIo.api.getInstance: creating new socket instance...');
try {
await lock.acquire('socketIo', async () => {
if (!this.instance) {
const o = new SocketIo();
await o.initialize();
this.instance = o;
logger.info('socketIo.api.getInstance: socket instance created SUCCESSFULLY');
}
});
} catch (e) {
moaLog('socketIo.api.getInstance: ERROR: ', e);
throw e;
}
} else {
logger.info('socketIo.api.getInstance: a socket instance already exists, reusing that one');
}
logger.info('socketIo.api.getInstance: END');
return this.instance;
}
}
I made a NodeJS application which usere a static Method to do some calculation Function.
When i try to acces the Method i got the isNotAFunction Error.
Here a static class which causes the error while accessing it:
exports.module = class PlaceEvaluator
{
static testMethod()
{
console.log("Test");
}
}
Here is the Code of the file which throws the Exception while reading:
PositionFinder = require("./positionFinder.js");
PlaceObj = require("./placeObj.js");
PlaceEvaluator = require("./placeEvaluator.js");
fetch = require("cross-fetch");
const express = require("express");
const http = require("http").createServer(express);
const io = require("socket.io")(http, {
cors:{
origin: "*"
}
});
const application = express();
application.use(express.static("public"));
PlaceEvaluator.testMethod();
io.on("connection", socket => {
socket.on("placeQuery", async ({topic, lat, long}) => {
PlaceEvaluator.testMethod(); //Here is the Exception function call
//console.log("Response sent!");
})
})
async function findPlaceObject(type, lat, long)
{
let placeObj = await PositionFinder.FetchPosition(type, lat, long);
return placeObj;
}
function convertToPlaceObjArr(inputObj)
{
var outputArr = [];
let name;
let lat;
let long;
for(var i = 0; i < inputObj.results.length; i++)
{
name = inputObj.results[i].name;
lat = inputObj.results[i].geometry.location.lat;
long = inputObj.results[i].geometry.location.lng;
outputArr.push(new PlaceObj(name, lat, long));
console.log(inputObj.results[i].name);
}
return outputArr;
}
http.listen(4000, function(){
console.log("Running on Port 4000");
// PositionFinder.FetchPosition("Pizza", "51.896359", "6.982303");
});
You need to use module.exports to export a default rather than exports.module.
By using exports.module you are exporting your class with the key of module so in that case you would have to do:
PlaceEvaluator.module.testMethod();
this is my first post in this forum. So please forgive me the misstakes.
I want to write a NodeJS server which runs a WebSocket Server (npm ws module).
The NodeJS server contains also a Class Obj which i want to modify a funciton afterwards over the Websocket server.
My Problem is the modified functjion cant acces global variables.
Can someone help if there is a solution for this problem or why this happes because if you do this without the Websocket it works.
Here is the code:
Server code:
const WebSocket = require('ws');
// WebSocket Server
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
try {
message = JSON.parse(message);
if (message.type == "handler") {
handler.modify(message.data);
console.log("modifyed");
}
if (message.type == "func") {
handler.modify_func(message.data);
console.log("modifyed");
}
if (message.type == "run") {
eval(message.data);
}
}
catch (error) {
}
});
});
// Modifying class
class Handler {
constructor() {
this.functions = [];
}
modify(data) {
let temp_class = new Function('return ' + data)();
temp_class.functions.forEach(element => {
if (this.functions.indexOf(element) == -1) {
this.functions.push(element)
}
this[element] = temp_class[element];
});
}
modify_func(data) {
let temp_func = new Function('return ' + data)();
this[temp_func.name] = temp_func;
}
test_func_from_orginal() {
console.log("test_func_from_orginal says:");
console.log(test_val);
}
}
var test_val = "this is the global variable";
var handler = new Handler();
Client code:
const WebSocket = require('ws');
//WebSocket Client
var ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open(event) {
// ws.send(JSON.stringify({ type: "handler", data: Handler.toString() }))
ws.send(JSON.stringify({ type: "func", data: test_func_from_func.toString() }))
console.log("open")
});
//Class Module
class Handler {
static get functions() {
return ["test"];
}
static test_func_from_class() {
console.log("test_func_from_class sayes:")
console.log(test_val);
}
}
function test_func_from_func() {
console.log("test_func_from_func sayes:")
console.log(test_val);
}
setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.test_func_from_orginal()' })) }, 1000);
// setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.test_func_from_class()' })) }, 1000);
setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.test_func_from_func()' })) }, 1000);
Ok, so this is what it's all about - a simple mistake. If you cut out all the websocket stuff (which is not really relevant here, as strings, and not contexts, got passed from and back anyway), you'll get this:
class ServerHandler {
constructor() {
this.functions = [];
}
modify(data) {
let temp_class = new Function('return ' + data)();
// not sure why not just `eval(data)` btw
temp_class.functions.forEach(funcName => {
if (this.functions.indexOf(funcName) == -1) {
this.functions.push(funcName)
}
this[funcName] = temp_class[funcName];
});
}
}
class ClientHandler {
static get functions() {
return ["test_func_from_class"];
// not "test" as in your example
// you actually don't even need this registry:
// all the static methods can be collected in runtime
}
static test_func_from_class() {
console.log("test_func_from_class sayes:")
console.log(test_val);
}
}
var test_val = 42;
var handler = new ServerHandler();
handler.modify(ClientHandler.toString());
eval(`handler.test_func_from_class()`); // 42
This all works fine, as there's no longer a mismatch between a name of method stored in static get functions ("test") and actual name of that method ("test_func_from_class"). The trick is that all the static functions created along with that temporary class are scoped the same way any other entity created in ServerHandler; that's how they 'see' that test_val.
But 'works' here is about mere possibility of this approach from technical perspective, and not about feasibility. Both new Function and eval with arbitrary input are very dangerous security holes - and they're left wide open here.
I found now a solution for my problem.
Server Code
const WebSocket = require('ws');
// WebSocket Server
const wss = new WebSocket.Server({ port: 8080 });
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
message = JSON.parse(message);
try {
if (message.type == "run") {
eval(message.data);
}
if (message.type == "obj_handler") {
handler.modify(JSON.parse(message.data));
}
}
catch (error) {
console.log(error);
}
// console.log('received: %s', message);
});
ws.send('something');
});
class ServerHandler {
constructor() {
this.data = "hi";
}
modify(data) {
for (const func in data) {
this[func] = eval(data[func]);
}
}
}
var test_val = 42;
var handler = new ServerHandler();
Client Code:
const WebSocket = require('ws');
//WebSocket Client
try {
var ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open(event) {
ws.send(JSON.stringify({ type: "obj_handler", data: update.convert() }))
});
}
catch (error) {
}
// Needed Update with 2 new Functions
update = {
func_test_global: () => {
console.log(test_val);
},
func_test_this: _ => {
console.log(this.data);
},
convert: function () {
let new_update = {};
for (const func in this) {
if (func != "convert")
new_update[func] = "" + this[func];
}
return JSON.stringify(new_update)
}
}
// setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.func_test_global()' })) }, 1000);
// setTimeout(function () { ws.send(JSON.stringify({ type: "run", data: 'handler.func_test_this()' })) }, 1000);
I am trying to build a simple node.js client using the amqp library, that opens a single connection and then a single channel to a RabbitMQ server. I want to reuse the same connection and channel to send multiple messages. The main problem is, that I don't want to write my entire code inside the callback function of the ceateChannel() function.
How do I reuse the channel outside of the callback function and make sure the callback function has finished before I use the channel?
I've tried both the callback way and the promise way but I can't make either of them work. When using the callback method I run into the described problem.
When using promises, I have the problem that I can't keep a reference of the connection and channel outside of the .then() function because the passed variables get destroyed after setting up the connection and channel.
amqp.connect('amqp://localhost', (err, conn) => {
if (err !== null) return console.warn(err);
console.log('Created connection!');
conn.createChannel((err, ch) => {
if (err !== null) return console.warn(err);
console.log('Created channel!');
//this is where I would need to write the code that uses the variable "ch"
//but I want to move the code outside of this structure, while making sure
//this callback completes before I try using "ch"
});
});
amqp.connect('amqp://localhost').then((conn) => {
return conn.createChannel();
}).then((ch) => {
this.channel = ch;
return ch.assertQueue('', {}).then((ok) => {
return this.queueName = ok.queue;
});
}).catch(console.warn);
why you don't use async\await ?
const conn = await amqp.connect('amqp://localhost');
const ch = await conn.createChannel();
// after that you can use ch anywhere, don't forget to handle exceptions
Also if you use amqplib, don't forget to handle close and internal error events, for example like this:
conn.on('error', function (err) {
console.log('AMQP:Error:', err);
});
conn.on('close', () => {
console.log("AMQP:Closed");
});
Try with a class, like this:
RabbitConnection.js
const amqp = require('amqplib');
const RabbitSettings = {
protocol: 'amqp',
hostname: 'localhost',
port: 5672,
username: 'guest',
password: 'guest',
authMechanism: 'AMQPLAIN',
vhost: '/',
queue: 'test'
}
class RabbitConnection {
constructor() {
RabbitConnection.createConnection();
this.connection = null;
this.channel = null;
}
static getInstance() {
if (!RabbitConnection.instance) {
RabbitConnection.instance = new RabbitConnection();
}
return RabbitConnection.instance;
}
//create connection to rabbitmq
static async createConnection() {
try {
this.connection = await amqp.connect(`${RabbitSettings.protocol}://${RabbitSettings.username}:${RabbitSettings.password}#${RabbitSettings.hostname}:${RabbitSettings.port}${RabbitSettings.vhost}`);
this.channel = await this.connection.createChannel();
this.channel.assertQueue(RabbitSettings.queue);
console.log('Connection to RabbitMQ established');
} catch (error) {
console.log(error);
}
}
//send message to rabbitmq queue
static async sendMessage(message, queueName) {
try {
let msg = await this.channel.sendToQueue(queueName, Buffer.from(message));
console.log('Message sent to RabbitMQ');
return msg;
} catch (error) {
console.log(error);
}
}
}
module.exports = { RabbitConnection };
ServerExpress.js
const express = require('express');
const { RabbitConnection } = require('./RabbitConnection');
const serverUp = () => {
const app = express();
app.get('/', (req, res) => {
RabbitConnection.sendMessage('Hello World', 'test');
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
};
module.exports = { serverUp };
index.js
const { RabbitConnection } = require("./RabbitConnection");
const { serverUp } = require("./ServerExpress");
serverUp();
RabbitConnection.getInstance();
The code below returns 'goN is not a function' error.How to make proper errors handler, when in case of an error we need to delete an old object and create a new one instead?
The main module udpSocket.js:
const udp = require('dgram');
const goN = require('./goNext').goNext;
class udpSocket {
constructor(config){
this.config = config;
this.socket = udp.createSocket('udp4');
this.socket.on('message', (buf) => {
this.socket.send(buf, this.config.outPort);
});
this.socket.on('error', (err) => {
console.log(err);
goN(this.socket, this.config.host);
});
}
start(){
this.socket.bind(this.config.port, this.config.host, (err) => {
if (err){
console.error(err);
goN(this.socket, this.config.host);
} else {
this.socket.send('test', this.config.outPort, this.config.host, (err) => {
if (err) {
console.error(err);
goN(this.socket, this.config.host);
} else {
console.log('UDP server up and running on '+this.config.port+' inPort, '+this.config.outPort+' outPort');
}
});
}
});
}
close(){
this.socket.close( () => {
ports.add({"in": this.config.port, "out": this.config.outPort});
delete this.socket;
});
}
}
module.exports = udpSocket;
goNext.js:
const udpSocket = require('./udpSocket');
module.exports.goNext = (socket, host) => {
if (socket != null){ delete socket; }
if (ports.length > 0){
let pp = ports.shift();
let server = new udpSocket({
port: pp.in,
outPort: pp.out,
host: host
});
sockets.set(pp.in, server);
server.start();
} else {
console.log('no sockets left');
process.exit(1);
}
}
wrapper.js
const config = require('./config').udp;
const goNext = require('./lib/goNext').goNext;
const List = require('collections/list');
global.ports = new List(config.ports);
global.sockets = new Map();
goNext(null, config.host);
goNext(null, config.host);
Maybe it's because the files require each other?
"When we require the references to another file before setting module.exports, what we are actually getting back is an empty object, not the populated object we expect"