Undefined class variable when defined in constructor - javascript

Just started using CoffeeScript and have hit a wall. In my code below I construct WebSocketServer with a number as the first argument and a function as the second argument. When the code is ran and the websocket receives a message, #msgHandler is now magically undefined. I tried to work around this by setting a variable handler to #msgHandler but that ended up being undefined as well. If anyone has any ideas I'd love to hear them, thanks!
main.coffee
#Used to communicate with browser interface
webSocketServ = new sio.WebSocketServer Sauce.Server.WebSocketPort, (data, socketId) ->
try
json = JSON.parse(data)
msgType = json.msgType
catch error
return
if (this.isAuthed(socketId))
switch msgType
when "auth"
else
io.websocket 'TODO: ' + cmd
else
if msgType is 'auth'
token = json.token
socket.coffee
class WebSocketServer
constructor: (#port, #msgHandler) ->
#webSockets = []
#handlers = {}
#test = []
#authedSockes = []
#listen(#port);
console.log #msgHandler #msgHandler is defined here as [ Function ]
listen: (port) ->
#wsServ = engine.listen port
#wsServ.on 'connection', #onConnect
io.socket "WebServer socket started on port #{port}"
onConnect: (client) ->
io.websocket 'New connection with id of ' + client.id
handler = #msgHandler ##msgHandler is undefined here?
client.on 'message', (data) ->
handler data, client.id
io.websocket '[' + this.id + '] ' + JSON.stringify(data)
client.on 'close', ->
io.websocket '[' + this.id + '] Disconnect'
client.on 'error', (err) ->
io.websocket "IO error: " + err
compiled socket.coffee
WebSocketServer = (function() {
function WebSocketServer(port, msgHandler) {
this.port = port;
this.msgHandler = msgHandler;
this.webSockets = [];
this.handlers = {};
this.test = [];
this.authedSockes = [];
this.listen(this.port);
console.log(this.msgHandler);
}
WebSocketServer.prototype.listen = function(port) {
this.wsServ = engine.listen(port);
this.wsServ.on('connection', this.onConnect);
return io.socket("WebServer socket started on port " + port);
};
WebSocketServer.prototype.onConnect = function(client) {
var handler;
io.websocket('New connection with id of ' + client.id);
handler = this.msgHandler;
client.on('message', function(data) {
handler(data, client.id);
return io.websocket('[' + this.id + '] ' + JSON.stringify(data));
});
client.on('close', function() {
return io.websocket('[' + this.id + '] Disconnect');
});
return client.on('error', function(err) {
return io.websocket("IO error: " + err);
});
};
WebSocketServer.prototype.isAuthed = function(socketId) {
return __indexOf.call(this.authedSockets, user) >= 0;
};
WebSocketServer.prototype.authSocket = function(socketId) {
this.authedSockets.push(socketId);
return io.websocket('Authed socket with ID of ' + socketId);
};
WebSocketServer.prototype.deauthSocket = function(socketId) {
return this.authedSockets = this.authedSockets.filter(function(word) {
return word !== socketId;
});
};
return WebSocketServer;
})();

It's not an undefined class variable, you mean that you cannot access an instance property. It's caused by the common this context issue in callbacks - you cannot just pass a method and expect it to know the instance.
Fortunately, this can be trivially fixed in CS using the fat arrow syntax for the method definition and other callbacks:
onConnect: (client) =>
# ^^
io.websocket 'New connection with id of ' + client.id
handler = #msgHandler # # is now the expected instance
client.on 'message', (data) =>
# ^^
handler data, client.id
io.websocket '[' + #id + '] ' + JSON.stringify(data)
# ^ or did you mean to use `client.` here?
client.on 'close', =>
# ^^
io.websocket '[' + #id + '] Disconnect'
client.on 'error', (err) ->
io.websocket "IO error: " + err
A different (and possibly better) approach might be to let your WebSocketServer class inherit from the engine's class, instead of building a wrapper around it. Since all callbacks are called on the server instance usually, you could access your properties directly without needing to bind the callbacks.

Related

Node JS TCP Server with simple password prompt problem

I'm trying to build a TCP server on node JS. The idea is to have multiple TCP clients connect and send / receive data(from server to client / client to server) and to have some sort of authentication (at least to enter a strong password) Also not sure if this approach is worth it. But, I've come up with something (most from online sources and docs) and crated below code.
Code runs and I can connect, but if I send data from client to server, the "password" check function fires up and each time I enter the correct password, a new (duplicate connection) is created. Seems like it keeps calling the same function on each input.
Desired behavior would be ; once client try's to connect, needs to provide the password and then start sending date. Also could someone give me a hint on how to send data back from server, or is it even possible. Or do I need to create a separate function for server.createConnection()
thanks in advance
UPDATE :I've changed the code a bit, but the main issue remains. this bit was supposed to check whether "clientAddress" exists and if so skip the auth part all together.
server.on('connection', (socket) => {
let clientAddress = `${socket.remoteAddress}:${socket.remotePort}`;
console.log(clientAddress)
if(sock.indexOf(clientAddress) !== -1){
console.log('devie found, opening communication')
newConnectionHandler(socket,clientAddress)
} else {
console.log('devie not found, need to authenticate')
userAuth(socket,clientAddress)
}
but as you can guess, it's not working :) if I manually specify the "clientAddress" it works , if I place "sock.push(clientAddress);" within the first block of code, it also works. No auth is asked. But when it's placed within
function userAuth(socket,clientAddress){
socket.write('password : ' )
socket.on('data', function (data) {
let pass = data.toString()
if (pass == password) {
sock.push(clientAddress);
console.log(sock)
newConnectionHandler(socket,clientAddress)
return;
} else {
//console.log(pass)
socket.write('Sorry, you cannot access the server \n')
console.log('acess denied for ' + socket.remoteAddress + ':' + socket.remotePort + '\n')
socket.write('connection closed')
socket.destroy()
}
})
}
code does run as expected and goes all the way till
function newConnectionHandler(socket,clientAddress){
//console.log(sock)
socket.write('Welcome \n')
socket.on('data', function(data1){
console.log("Client Sent: " + data1);
});
but as soon as I send a new message from the client, it goes back as if it was never authenticated and treats my input as the password and because it does not match with the actual password,it destroys the connection.
can someone please give me a hand...
const net = require('net');
const port = 3001;
const host = '192.168.0.165';
const server = net.createServer()
let sock = [];
let password = 123
//server.on('listening',createConnection);
server.on('connection', (socket) => {
let clientAddress = `${socket.remoteAddress}:${socket.remotePort}`;
console.log(clientAddress)
if(sock.indexOf(clientAddress) !== -1){
console.log('devie found, opening communication')
newConnectionHandler(socket,clientAddress)
} else {
console.log('devie not found, need to authenticate')
userAuth(socket,clientAddress)
}
server.on('error', errorHandler);
function errorHandler(err){
console.log(`Error occurred in ${clientAddress}: ${err.message}`);
}
function userAuth(socket,clientAddress){
socket.write('password : ' )
socket.on('data', function (data) {
let pass = data.toString()
if (pass == password) {
sock.push(clientAddress);
console.log(sock)
newConnectionHandler(socket,clientAddress)
return;
} else {
//console.log(pass)
socket.write('Sorry, you cannot access the server \n')
console.log('acess denied for ' + socket.remoteAddress + ':' + socket.remotePort + '\n')
socket.write('connection closed')
socket.destroy()
}
})
}
function newConnectionHandler(socket,clientAddress){
//console.log(sock)
socket.write('Welcome \n')
socket.on('data', function(data1){
console.log("Client Sent: " + data1);
});
socket.once('close', (data) => {
let index = sock.findIndex((o) => {
return o.remoteAddress === socket.remoteAddress && o.remotePort === socket.remotePort;
})
if (index !== -1) sock.splice(index, 1);
sock.forEach((sock) => {
socket.write(`${clientAddress} disconnected\n`);
        });
console.log(`connection closed: ${clientAddress}`);
    });
}
/* function createConnection(){
// Start a connection to the server
var socket = server.on('connect',function(){
// Send the initial message once connected
socket.write({question: "Hello, world?"});
});
// Whenever the server sends us an object...
socket.on('data', function(data){
// Output the answer property of the server's message to the console
console.log("Server's answer: " + data.answer);
});
} */
})
server.listen(port, host, () => {
console.log('TCP Server is running on port ' + port + '.');
});
so it appears as the only bit of code that was causing the authentication loop was the
function userAuth(socket,clientAddress){
socket.write('password : ' )
**socket.on('data', function (data) {**
let pass = data.toString()
after changing "on" with "once" it is now functioning properly. I tested with two TCP clients, both connected and was asked to enter a password. They can both actively send messages to the server and both disconnected properly in the end.
this is the code if anyone finds any use for it :) the connection it's self is still unencrypted so not good for sending/receiving sensitive data.
const net = require('net');
const port = 3001;
const host = '192.168.0.165';
const server = net.createServer()
let sock = [];
let password = 123
//server.on('listening',createConnection);
server.on('connection', (socket) => {
let clientAddress = `${socket.remoteAddress}:${socket.remotePort}`;
console.log(clientAddress)
if(sock.indexOf(clientAddress) !== -1){
console.log('devie found, opening communication')
newConnectionHandler(socket,clientAddress)
} else {
console.log('devie not found, need to authenticate')
userAuth(socket,clientAddress)
}
})
server.on('error', errorHandler);
function errorHandler(err){
console.log(`Error occurred in ${clientAddress}: ${err.message}`);
}
function userAuth(socket,clientAddress){
socket.write('password : ' )
socket.once('data', function (data) {
let pass = data.toString()
if (pass == password) {
sock.push(clientAddress);
console.log(sock)
newConnectionHandler(socket,clientAddress)
return;
} else {
//console.log(pass)
socket.write('Sorry, you cannot access the server \n')
console.log('acess denied for ' + socket.remoteAddress + ':' + socket.remotePort + '\n')
socket.write('connection closed')
socket.destroy()
}
})
}
function newConnectionHandler(socket,clientAddress){
//console.log(sock)
socket.write('Welcome \n')
socket.on('data', function(data1){
console.log("Client Sent: " + data1);
});
socket.on('close', function(data) {
let index = sock.findIndex(function(o) {
return o.remoteAddress === sock.remoteAddress && o.remotePort === sock.remotePort;
})
if (index !== -1) sock.splice(index, 1);
console.log('CLOSED: ' + socket.remoteAddress + ' ' + socket.remotePort);
});
}
/* function createConnection(){
// Start a connection to the server
var socket = server.on('connect',function(){
// Send the initial message once connected
socket.write({question: "Hello, world?"});
});
// Whenever the server sends us an object...
socket.on('data', function(data){
// Output the answer property of the server's message to the console
console.log("Server's answer: " + data.answer);
});
} */
server.listen(port, host, () => {
console.log('TCP Server is running on port ' + port + '.');
});

Discord.js error = "message is not defined"

const Discord = require('discord.js');
exports.run = async (bot, message, args) => {
let userInfMent = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]))
message.channel.send(userInfo(userInfMent));
}
function userInfo(user) {
const Discord = require('discord.js');
let userInfMent = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]))
var userCreated = userInfMent.createdAt.toString().split(' ');
var lastMsg = userInfMent.lastMessage.createdAt.toString().split(' ')
const userInfoEmbed = new Discord.RichEmbed()
.addField('Никнейм: ', userInfMent.username)
.addField('Тег: ', userInfMent.tag)
.addField('ID: ', userInfMent.id)
.addField('Аккаунт был создан: ', userCreated[1] + ', ' + userCreated[2] + ', ' + userCreated[3])
.addField('Последнее сообщение: ', userInfMent.lastMessage + ' в ' + lastMsg[1] + ', ' + lastMsg[2] + ', ' + lastMsg[3] + ', ' + lastMsg[4])
.addField('Статус: ', userInfMent.presence.status)
.setColor('RANDOM')
.setThumbnail(userInfMent.avatarURL);
return userInfoEmbed
}
Hi. I'm a Node.js beginner.
When i start the command, in console i see the error:
(node:6312) UnhandledPromiseRejectionWarning: ReferenceError: message
is not defined
at userInfo (C:\Users\deris\Desktop\Проекты\Node.js\discord.js\JSBot-master\commands\userinfo.js:10:23)
at Object.exports.run (C:\Users\deris\Desktop\Проекты\Node.js\discord.js\JSBot-master\commands\userinfo.js:5:26)
message is only defined in the message event itself. So you can simply pass the message object as a parameter into your function.
// new function
function userInfo(user, message) {
//code here
}
Just remember that you'll have to add the additional parameter when you call the function.
message.channel.send(userInfo(userInfMent, message));
Message is not defined because you did not put the code in a Message Event.
I just been learning and found this one out.
Message.channel.send will not work inside a function. The function will read it as a variable. And since message hasn't been defined we draw the error.
outside the function the command will again work fine. I'm not sure if it's an error with the current builds or intentional. But there is your fix
Your using message.guild.member inside of the function and didn't define member which is causing the problem. Consider placing the function inside of the export

Start SignalR connection after calling stop

I have two methods in my SignalRService class on client side:
public startConnection(): void {
this.connection.start().done((data: any) => {
console.log('Now connected ' + data.transport.name + ', connection
ID= ' + data.id);
//this.connectionEstablished.emit(true);
this.connectionExists = true;
}).fail((error: any) => {
console.log('Could not connect ' + error);
//this.connectionEstablished.emit(false);
});
}
public stopConnection():void {
this.connection.stop();
console.log('Stop connection');
}
Initialization goes in constructor like this:
this.connection = $.hubConnection(environment.apiUrl);
this.connection.qs = "Bearer="+this._authService.getToken();
// create new proxy as name already given in top
this.proxy = this.connection.createHubProxy(this.proxyName);
I need to call start again after stop but it fails with exception:
ERROR Error: SignalR: Connection has not been fully initialized. Use
.start().done() or .start().fail() to run logic after the connection has
started.
Help me please with it!

Nodejs server stop accept connections after a few hours

I made an application to the control of simultaneous logins, in starts everything works perfectly, however after a few hours i can not longer connect to the server, my client returns me the following error: net :: ERR_CONNECTION_TIMED_OUT and on the server side does not happen any error like it was running correctly... code below:
CLIENT SIDE:
var socket;
function connect(id) {
socket = io.connect('http://IP:4200');
socket.on('connect', function (data) {
socket.emit('join', id);
});
socket.on('messages', function (data) {
console.log('MSG: ' + data.toString());
switch (data.toString()) {
case "kick":
socket.close();
console.log("KICK!");
break;
case "duplicate_entry":
socket.close();
console.log("Another user connection!");
break;
}
});
}
SERVER SIDE:
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var clients = [];
app.use(express.static(__dirname + '/bower_components'));
function logtimestamp() {
var log_date = new Date();
log_date = '[' + log_date.getFullYear() + '/' + (log_date.getMonth() + 1) + '/' + log_date.getDate() + ' ' + log_date.getHours() + ':' + log_date.getMinutes() + ':' + log_date.getSeconds() + ']';
return log_date;
}// FUNCTION logtimestamp
console.log("Start time: " + logtimestamp());
console.log("Server port 4200")
console.log("websocket server created!");
try {
io.on('connection', function (client) {
try {
var id;
var conexao;
client.on('join', function (data) {
try {
console.log('Client connected...'+logtimestamp()+' ID:' + data);
id = data;
conexao = {
ws: client,
id_user: data
};
clients.push(conexao);
for (var x = 0; x < clients.length; x++) {
//desconect previous user
try {
if (clients[x].id_user == id) {
if (clients[x].ws != conexao.ws) {
clients[x].ws.emit('messages', 'duplicate_entry');
clients.splice(x, 1);
}
}
} catch (err) {
console.log("ERROR 1: " + err.message);
}
}
} catch (err) {
console.log("ERROR 2: " + err.message);
}
});
} catch (err) {
console.log("ERROR 3: " + err.message);
}
});
} catch (err) {
console.log("ERROR 4: " + err.message);
}
server.listen(4200);
I see a couple possible issues. It is hard for us to know by just inspecting code which issues actually are the cause of your issue. In any case, you should clean up these issues and see if it improves the situation:
You should respond the the disconnect event and immediately remove any socket from your clients array when it disconnects.
In your loop where you are looking to removing any prior instances of a given user, your for loop will not work properly when you are doing .splice(x, 1) in the middle of the for loop. This will move all items after it does one in the array causing you to skip the comparison of the next element in the array. One simple way to get around this is to iterate the array backwards: for (var x = clients.length - 1; x >= 0; x--) because then the elements who's position are affected after the .splice() are elements you have already looked at. None will be missed.
Beyond this, you should examine the memory usage of the nodejs process, the open sockets by the nodejs process and the CPU usage of the nodejs process to see if any of those point to any possible issues.
And, what is your deployment environment? Is there a proxy server in front of your web server?

Node.js variable getting reset - undefined

I have a global variable (openTokSessionID) that is set by a function. When the function is run again, it should check to see if openTokSessionID is set. If it is, it should use the current value. Instead, however, it complains that openTokSessionID is undefined. My server code is below. Thanks for your help.
var http = require('http').createServer(handler),
io = require('socket.io').listen(http).set('log level', 1),
opentok = require('opentok'),
key = '', // Replace with your API key
secret = ''; // Replace with your API secret
var ot = new opentok.OpenTokSDK(key,secret);
ot.setEnvironment("staging.tokbox.com");
//ot.setEnvironment("api.opentok.com"); //only for production
http.listen(8080);
console.log('Chatserver listening on port 8080');
var nicknames = {};
var log = {};
var connectedUsersObject={};
var privateSessionObject={};
var data;
var openTokSessionID='';
function handler(req, res) {
res.writeHead(200);
res.end();
}
////////////////////////////////////////////SOCKET.IO FUNCTIONS///////////////////////////////////////////////////////
io.sockets.on('connection', function (socket) {
socket.on('private message', function(data) {
console.log('OpenTok Session ID is: ....' + openTokSessionID);
if(data.messageType=='openTokDemoRequest'){ //if new session, create unique session ID, add to current chat object
console.log(data.messageType + ' sender: ' + data.from);
var location = '127.0.0.1'; // use an IP or 'localhost'
console.log('message type is: "openTokDemoRequest". openTokSessionID is: ' + openTokSessionID);
var messageRecipient=data.from;
if(openTokSessionID==''){
console.log('The session ID is: ' + openTokSessionID + '++++++++');openTokSessionID= ot.create_session(location, function(openTokSessionID){
var data= {'to':messageRecipient, 'message':{'token': ot.generate_token({'session_id':openTokSessionID, 'role': "publisher"}), 'sessionID': openTokSessionID, 'apikey':key}, 'from': 'openTok', 'messageType':'demoTokenInfo', 'privateSessionID':''};
console.log('NEW session id is: ' + openTokSessionID + '. token is: ' + data.message.token);
// privateMessageSend(data);
// sendToUser(data);
connectedUsersObject[messageRecipient].emit('private message', data);
console.log ('message recipient is: ' + messageRecipient);
});
}
else{
console.log('OpenTok Session ID is: ////' + openTokSessionID);
var data= {'to':messageRecipient, 'message':{'token': ot.generate_token({'session_id':openTokSessionID, 'role': "publisher"}), 'sessionID': openTokSessionID, 'apikey':key}, 'from': 'openTok', 'messageType':'demoTokenInfo', 'privateSessionID':''};
console.log('session id is: ' + openTokSessionID + '. token is: ' + data.message.token);
connectedUsersObject[messageRecipient].emit('private message', data);
console.log ('message recipient is: ' + messageRecipient);
}
}
});
Here you're trying to use the return value from an asynchronous call, and as a result you're setting your variable to undefined:
openTokSessionID= ot.create_session(location, function(openTokSessionID){
...
Because you gave your callback argument the same name as your variable, it masked your variable within the callback so it looked OK the first time, but your actual variable got trashed.
You need to change it to something like:
ot.create_session(location, function(sessionID){
openTokSessionID = sessionID;
...

Categories