I've been stuck on this issue for a while the answer might be really basic but I fail to understand what the problem is. AFAIU It execute the function but doesnt trigger the callback and I dont know why.
My script aim to have both a tcp server to have a device (raspberry pi) that connect a tcp socket and a client to connect to a websocket on a sailsjs app.
I manage to have both this thing running on the following code, the problem is they only work separatly, simultanuously but separatly, when I try a get outside the socket everything works fine but when I do inside, the io.socket object is just piling up the get request in a requestQueue.
{ useCORSRouteToGetCookie: true,
url: 'http://localhost:1337',
multiplex: undefined,
transports: [ 'polling', 'websocket' ],
eventQueue: { 'sails:parseError': [ [Function] ] },
query:'__sails_io_sdk_version=0.11.0&__sails_io_sdk_platform=node&__sails_io_sdk_language=javascript',
_raw:
{ socket:
{ options: [Object],
connected: true,
open: true,
connecting: false,
reconnecting: false,
namespaces: [Object],
buffer: [],
doBuffer: false,
sessionid: '0xAlU_CarIOPQAGUGKQW',
closeTimeout: 60000,
heartbeatTimeout: 60000,
origTransports: [Object],
transports: [Object],
heartbeatTimeoutTimer: [Object],
transport: [Object],
connectTimeoutTimer: [Object],
'$events': {} },
name: '',
flags: {},
json: { namespace: [Circular], name: 'json' },
ackPackets: 0,
acks: {},
'$events':
{ 'sails:parseError': [Function],
connect: [Object],
disconnect: [Function],
reconnecting: [Function],
reconnect: [Function],
error: [Function: failedToConnect],
undefined: undefined } },
requestQueue:
[ { method: 'get', headers: {}, data: {}, url: '/', cb: [Function] },
{ method: 'get', headers: {}, data: {}, url: '/', cb: [Function] } ] }
The code is the following :
//library to connect to sailsjs websockets
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
//library to do the tcp server
var net = require('net');
// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
var server = net.createServer(function(tcpSocket) { //'connection' listener
//socket was sucessfully connected
console.log('client connected');
//notify on deconnection
tcpSocket.on('end', function() {
console.log('client disconnected');
});
// Handle incoming messages from clients.
tcpSocket.on('data', function (data) {
console.log(data.toString('utf8', 0, data.length));
//if data is PING respond PONG
if(data.toString('utf8', 0, 4)=='PING'){
console.log('I was pinged');
tcpSocket.write('PONG\r\n');
}
console.log(io.socket);//debugging purpose
//trigger a socket call on the sails app
io.socket.get('/', function (body, JWR) {
//display the result
console.log('Sails responded with: ', body);
console.log('with headers: ', JWR.headers);
console.log('and with status code: ', JWR.statusCode);
});
});
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});
It looks like your socket isn't autoconnecting. Try connecting manually:
// Instantiate the socket client (`io`)
// (for now, you must explicitly pass in the socket.io client when using this library from Node.js)
var io = sailsIOClient(socketIOClient);
// Set some options:
// (you have to specify the host and port of the Sails backend when using this library from Node.js)
io.sails.url = 'http://localhost:1337';
var socket = io.sails.connect();
socket.on('connect', function() {
... connect TCP server and continue ...
});
I found a solution, I just got rid of sails.io.js and used plain socket.io it now works as intended feel free to explain though why it didnt in sails.io.js
//library to connect to sailsjs websockets
var socketIOClient = require('socket.io-client');
//var sailsIOClient = require('sails.io.js');
//library to do the tcp server
var net = require('net');
var socket=socketIOClient.connect('http://localhost:1337', {
'force new connection': true
});
var server = net.createServer(function(tcpSocket) { //'connection' listener
//socket was sucessfully connected
console.log('client connected');
//notify on deconnection
tcpSocket.on('end', function() {
console.log('client disconnected');
});
// Handle incoming messages from clients.
tcpSocket.on('data', function (data) {
console.log(data.toString('utf8', 0, data.length));
console.log(data.toString('utf8', 0, data.length));
//if data is PING respond PONG
if(data.toString('utf8', 0, 4)=='PING'){
console.log('I was pinged');
tcpSocket.write('PONG\r\n');
}
if(data.toString('utf8', 0, 4)=='test'){
socket.emit('test',{message : 'test'});
//io.socket.disconnect();
}
});
});
Related
i want kuzzle to reconnect on a disconnected event, so i set autoReconnect argument to true as written in my code:
const connectionOptions = {
autoReconnect: true,
// reconnectionDelay: 500,
ssl: true,
port: PORT
}
kuzzle = new Kuzzle(new WebSocket(ADDRESS, connectionOptions), {
autoQueue: true,
autoReplay: true,
offlineMode: 'auto',
queueTTL: 0,
queueMaxSize: 500
})
i check the event of kuzzle with this:
kuzzle.on('reconnected', () => {
console.log('kuzzle reconnected')
})
kuzzle.on('disconnected', () => {
console.log('kuzzle disconnected')
})
But the only thing i see is the disconnected event but never the reconnect. Why ????
PS: i disable and enable my network interfaces to test my code
From electron I pass node.js modules with the contextBridge to the interface like this:
const udp = require('dgram');
contextBridge.exposeInMainWorld(
'electron',
{
udp: udp,
}
)
Then for example in the web console I can do:
var udp = window.electron.udp
var client = udp.createSocket('udp4');
Most of the methods on client work.
But when I try to attach event handlers I this error:
client.on('message',function(msg,info){
console.log('Data received from server : ' + msg.toString());
});
=> VM542:1 Uncaught TypeError: client.on is not a function
I don't understand why in the web console I can't use the on-methods?
How should I listen to events there? Or could this error be related to Jquery?
Thank you
I know that I could set: contextIsolation: false but then I got other errors.
client => {_events: {…}, _eventsCount: 0, _maxListeners: undefined, type: "udp4", Symbol(kCapture): false, …}
You can see for yourself that the request hangs:
curl "http://europasprak.com:9001/socket.io/?EIO=4&transport=polling"
It also hangs when sending the request from the server machine itself:
curl "http://localhost:9001/socket.io/?EIO=4&transport=polling"
I'm trying to create a socket.io server following the documentation.
I have the NodeJS socket server:
var http = require('http');
var socketio = require('socket.io');
var httpServer = http.createServer(utils.httpHandler);
httpServer.listen(config.socketio.port, function() {
console.log('The NodeJS HTTP server [port: ' + config.socketio.port + '] is listening...');
});
Using the DEBUG variable to start the server as in:
DEBUG=socket* node /usr/local/learnintouch/engine/api/js/socket/elearning-server.js 2>&1 >> /usr/local/learnintouch/logs/nodejs.log
shows:
socket.io:server creating engine.io instance with opts {"cors":{"origin":"*"},"path":"/socket.io"} +2ms
socket.io:server attaching client serving req handler +5ms
socket.io:server initializing namespace / +1ms
socket.io:server initializing namespace /elearning +10ms
The NodeJS log shows:
The NodeJS HTTP server [port: 9001] is listening...
The server object is:
{
io: Server {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
_nsps: Map { '/' => [Namespace] },
parentNsps: Map {},
_path: '/socket.io',
clientPathRegex: /^\/socket\.io\/socket\.io(\.min|\.msgpack\.min)?\.js(\.map)?$/,
_connectTimeout: 45000,
_serveClient: true,
_parser: {
protocol: 5,
PacketType: [Object],
Encoder: [Function: Encoder],
Decoder: [Function: Decoder]
},
encoder: Encoder {},
_adapter: [Function],
sockets: Namespace {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
sockets: Map {},
_fns: [Array],
_ids: 0,
server: [Circular],
name: '/',
adapter: [RedisAdapter],
[Symbol(kCapture)]: false
},
opts: { cors: [Object] },
[Symbol(kCapture)]: false
}
}
The connection is then handled like this:
module.exports.io = socketio(httpsServer, {
cors: {
origin: '*',
}
});
module.exports.io.adapter(ioredis({ host: config.redis.hostname, port: config.redis.port }));
var redisClient = redis.createClient(config.redis.port, config.redis.hostname);
module.exports.io.use(function (socket, handler) {
console.log('The namespace middleware is registered');
console.log(socket.request.headers.cookie);
if (socket.request.headers.cookie) {
socket.request.cookies = cookie.parse(decodeURIComponent(socket.request.headers.cookie));
socket.request.sessionID = socket.request.cookies['PHPSESSID'];
socket.request.socketSessionId = socket.request.cookies['socketSessionId'];
console.log("Authorization attempt with sessionID: " + socket.request.sessionID + " and socketSessionId: " + socket.request.socketSessionId);
redisClient.get("PHPREDIS_SESSION:" + socket.request.sessionID, function (error, reply) {
if (error) {
console.log("The redis client had an error: " + error);
return handler(new Error('The connection was refused because the redis client had an error.'));
} else if (!reply) {
console.log('The connection was refused because the redis client did not find the sessionID.');
return handler(new Error('The connection was refused because the redis client did not find the sessionID.'));
} else {
var redisSocketSessionId = utils.getRedisValue(reply, "socketSessionId");
if ('undefined' == typeof socket.request.socketSessionId || redisSocketSessionId != socket.request.socketSessionId) {
console.log('The connection was refused because the socketSessionId was invalid.');
return handler(new Error('The connection was refused because the socketSessionId was invalid.'));
} else {
console.log('The connection was granted.');
handler();
}
}
});
} else {
console.log('The connection was refused because no cookie was transmitted.');
return handler(new Error('The connection was refused because no cookie was transmitted.'));
}
});
The client connection:
<script type="text/javascript">
var elearningSocket;
$(function() {
if ('undefined' != typeof io && 'undefined' == typeof elearningSocket) {
console.log("Creating a socket on //dev.learnintouch.com:9001/elearning");
elearningSocket = io.connect('//dev.learnintouch.com:9001/elearning', { reconnect: true, rejectUnauthorized: false });
}
if ('undefined' != typeof elearningSocket) {
console.log("A socket on //dev.learnintouch.com:9001/elearning has been created");
elearningSocket.on('connect', function() {
console.log("The elearning namespace socket connected");
elearningSocket.emit('watchLiveCopilot', {'elearningSubscriptionId': '63', 'elearningClassId': '7'});
});
elearningSocket.on('postLogin', function(data) {
isAdmin = data.admin;
});
elearningSocket.on('message', function(message) {
console.log(message);
});
}
});
</script>
And the Chrome browser console:
Creating a socket on //dev.learnintouch.com:9001/elearning
A socket on //dev.learnintouch.com:9001/elearning has been created
But when sending a client connection, the log never shows the The namespace middleware is registered message.
Versions:
http#0.0.1-security
https#1.0.0
socket.io#4.1.3
cors#2.8.5
redis#3.1.2
socket.io-redis#6.1.1
connect#3.7.0
cookie#0.4.1
lodash#4.17.21
It looks like the connection cannot be established.
After a while the browser console shows an ERR_EMPTY_RESPONSE message as in:
socket.io.min.js:6 GET http://dev.learnintouch.com:9001/socket.io/?EIO=4&transport=polling&t=NbGNn6V net::ERR_EMPTY_RESPONSE
But the NodeJS socket server seems to be listening:
netstat -l | grep 9001
tcp6 0 0 [::]:9001 [::]:* LISTEN
The NodeJS socket server is not reachable.
But the firewall seems not to be the issue:
sudo ufw status verbose
Status: active
To Action From
-- ------ ----
9001 ALLOW IN Anywhere
The error can be seen live at
http://www.europasprak.com/elearning/subscription/2938/course
by first logging at
http://www.europasprak.com/engine/modules/user/login.php
with using the user demo#demo.com with the demo password.
UPDATE: I could solve the issue. It related to a handler preventing the connection from being established.
When changing the following:
module.exports.io.use((socket, handler) => {
to the following:
module.exports.io.of('/elearning').use((socket, handler) => {
the connection could then be done.
you need to determine whether the socket.io connection is successful, and then add events such as cores, adapters, events etc...to track down the problem step by step.
judging from the error report you provided above, socket.io was not established successfully.
I'm trying to connect to a cloud server that runs my MongoDB from my local machine. I'm using tunnel-ssh within the Node.js application I'm creating, however, I seem to have multiple problems and I don't fully understand what's going on.
Problems
I'm not 100% sure I'm successfully connecting to the server. There's no error, however, when I console.log(server) it says _connections: 0,. see full log below.
If I am connecting and then I try to run the getDataFromMongoDB function it returns the error, EADDRINUSE: address already in use 127.0.0.1:27000.
I've been trying to wrap my head around this all day and I'm not getting anywhere. Please help.
Error 1 - Is server connecting
Server {
_events:
[Object: null prototype] { connection: [Function], close: [Function] },
_eventsCount: 2,
_maxListeners: undefined,
_connections: 0,
_handle:
TCP {
reading: false,
onread: null,
onconnection: [Function: onconnection],
[Symbol(owner)]: [Circular] },
_usingWorkers: false,
_workers: [],
_unref: false,
allowHalfOpen: false,
pauseOnConnect: false,
_connectionKey: '4:127.0.0.1:27017',
[Symbol(asyncId)]: 7 }
Code
var config = {
username: "root",
Password: "password on the server",
host: "server IP address",
port: 22,
dstHost: "127.0.0.1",
dstPort: 27017,
localHost: "127.0.0.1",
localPort: 27000
};
var tnl = tunnel(config, function(error, tnl) {
if (error) {
console.log(error);
}
// yourClient.connect();
// yourClient.disconnect();
console.log(tnl);
getDataFromMongoDB();
});
async function getDataFromMongoDB(page) {
const MongoClient = require("mongodb").MongoClient;
const uri = "mongodb://USRNAME:PASSWORD_FOR_MONGDB_DATABASE#localhost:27017";
const client2 = new MongoClient(uri, { useNewUrlParser: true });
const client = await connectToMongodb(client2);
const collection = client.db("my_database_name").collection("jobs");
const jobs = await collection.find().toArray();
console.log("jobs", jobs);
}
function connectToMongodb(client) {
return new Promise((resolve, reject) => {
client.connect(function(err) {
console.log("connected", err);
return resolve(client);
});
});
}
I'm trying to get a persistent connection from my socket.io-client (running on Node.js) to a remote websocket. I do not have control over the remote socket, and sometimes it can go down entirely. I would like to attempt to reconnect() whenever an error or disconnect occurs. In the following example, I'm trying to test the case where the remote host is refusing a connection. In this case, I would like to attempt to reconnect after 1 second. It calls a second time, and exits.
Here's the code:
var events = require('events'),
util = require('util'),
io = require('socket.io-client'),
url = "ws://localhost:12345", // intentionally an unreachable URL
socketOptions = {
"transports" : [ "websocket" ],
"try multiple transports" : false,
"reconnect" : false,
"connect timeout" : 5000
};
// The goal is to have this socket attempt to connect forever
// I would like to do it without the built in reconnects, as these
// are somewhat unreliable (reconnect* events not always firing)
function Test(){
var self = this;
events.EventEmitter.call(self);
var socket;
function reconnect(){
setTimeout(go, 1000);
}
function go(){
console.log("connecting to", url, socketOptions);
socket = io.connect(url, socketOptions);
socket.on('connect', function(){
console.log("connected! wat.");
});
socket.on('error', function(err){
console.log("socket.io-client 'error'", err);
reconnect();
});
socket.on('connect_failed', function(){
console.log("socket.io-client 'connect_failed'");
reconnect();
});
socket.on('disconnect', function(){
console.log("socket.io-client 'disconnect'");
reconnect();
});
}
go();
}
util.inherits(Test, events.EventEmitter);
var test = new Test();
process.on('exit', function(){
console.log("this should never end");
});
When running it under node 0.11.0 I get the following:
$ node socketio_websocket.js
connecting to ws://localhost:12345 { transports: [ 'websocket' ],
'try multiple transports': false,
reconnect: false,
'connect timeout': 5000 }
socket.io-client 'error' Error: connect ECONNREFUSED
at errnoException (net.js:878:11)
at Object.afterConnect [as oncomplete] (net.js:869:19)
connecting to ws://localhost:12345 { transports: [ 'websocket' ],
'try multiple transports': false,
reconnect: false,
'connect timeout': 5000 }
this should never end
The ECONNREFUSED is an exception you don't manage.
Try with this:
process.on('uncaughtException', function(err) {
if(err.code == 'ECONNREFUSED'){
reconnect();
}
}
Edit
Modify the options like this:
socketOptions = {
"transports" : [ "websocket" ],
"try multiple transports" : false,
"reconnect" : false,
'force new connection': true, // <-- Add this!
"connect timeout" : 5000
};
and the reconnect function (look in the comments for the explanation)
function reconnect(){
socket.removeAllListeners();
setTimeout(go, 1000);
}
Probably socket.io reuse the same connection without creating a new one, forcing it the app works