net::ERR_CONNECTION_CLOSED after install SSL - javascript

my website was working great, but i need to install certifies SSL.
After install SSL my console show "net::ERR_CONNECTION_CLOSED".
Part of node file.
function load() {
query('SET NAMES utf8');
query('sql query was here', function(err, row) {
if((err) || (!row.length)) {
process.exit(0);
return;
}
currentid = row[0].id;
});
loadHistory();
setTimeout(function() { io.listen(8080); }, 3000);
}
Part of client side script
function connect() {
if(!SOCKET) {
chat("italic", "Generating token...");
var hash = getCookie('hash');
if(!hash) {
chat("italic", "Please, sing through steam.");
} else {
chat("italic", "Connecting...");
SOCKET = io(HOST);
SOCKET.on('connect', function(msg) {
chat("italic", "Connected!");
SOCKET.emit('hash', hash);
});
SOCKET.on('connect_error', function(msg) {
chat("italic", "Conection lost...");
});
SOCKET.on('message', function(msg) {
onMessage(msg);
});
}
} else {
console.log("Error: connection already exists.");
}
}

Related

Firebase transaction not working

I am calling from an angular app, using angularfire2, this.afDb.database is the db instance
const downloadsRef = this.afDb.database.ref('research_reports-published/' + rrid + '/' + field);
downloadsRef.transaction(function(fieldval) {
if (fieldval) {
fieldval = fieldval + 1;
}
return fieldval;
},
function(error, committed, snapshot) {
if (error) {
console.log('Transaction failed abnormally!', error);
} else if (!committed) {
console.log('We aborted the transaction (because ada already exists).');
} else {
console.log('User ada added!');
}
console.log('Adas data: ', snapshot.val());
}).then(function() {
console.log('Transaction successfully committed!');
}).catch(function(error) {
console.log('Transaction failed: ', error);
});
The code just silently prints
Adas data: oldvalue
Transaction successfully committed
and exits
Found the issue in above code. It should be
if (fieldval != null) {
fieldval = fieldval + 1;
}
as fieldval=0 also makes it false

How to develop and test Cordova SQLite application in browser?

The only way that I found is PouchDB with different adapters for browser and device. But this way does't fit for me.
Another way this using Cordova-sqlite-storage
Just use different Database object for browser and device.
This is part of my code(ES6):
var runiOS = false;
var DB;
// Check browser or device
var onDeviceReady = new Promise(function(resolve, reject) {
if (document.URL.match(/^https?:/i)) {
console.log("Running in a browser...");
resolve();
} else {
console.log("Running in an app...");
runiOS = true;
document.addEventListener("deviceready", resolve, false);
}
});
// Run application
onDeviceReady.then(function() {
// Init WebSQL on browser or SQLite on device
if (runiOS) {
DB = window.sqlitePlugin.openDatabase({ name: 'my.db', location: 'default' }, function (db) {}, function (error) { console.log('Open database ERROR: ' + JSON.stringify(error)); });
console.log('DB: SQLite');
}
else {
DB = window.openDatabase('my', "0.1", "My list", 200000);
console.log('DB: WebSQL');
}
// ...
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS DemoTable (name, score)');
tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101]);
tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202]);
}, function(error) {
console.log('Transaction ERROR: ' + error.message);
}, function() {
console.log('Populated database OK');
});
db.transaction(function(tx) {
tx.executeSql('SELECT count(*) AS mycount FROM DemoTable', [], function(tx, rs) {
console.log('Record count (expected to be 2): ' + rs.rows.item(0).mycount);
}, function(tx, error) {
console.log('SELECT error: ' + error.message);
});
});
});

Node js, Call WebSocket server from oracle apex

I am trying to create web-sockets in APEX for automatic refresh with node
but I am getting error This is not valid JSON:
java script code
$(function(){
window.WebSocket = window.WebSocket || window.MozWebSocket;
if (!window.WebSocket) {
console.log('Sorry websockets');
}
else {
var connection = new WebSocket('ws://127.0.0.1:1337');
connection.onerror = function (error) {
console.log( 'Sorry, but there is some problem with your connection or the server is down.');
};
connection.onmessage = function (message) {
console.log(message.data);
try {
var json = JSON.parse(message.data);
if (json.message=='REFRESH') {
gReport.pull();
}
else {
console.log(json.message);
}
}
catch (e) {
console.log('Thisnot valid JSON: ' + message.data);
}
};
}
});

Mute/kick channel web application (ARI)

So I currently have two functions which are called when I add a call to a bridge, and two functions within that get called automatically, so am trying to use JQuery so they only get called when a button is clicked, and then I can work from there in the server side of things.
Issue being at the moment an error gets thrown up saying $ isnt defined.
JQuery does work I have another file am calling which is all JQuery and handles my socket.io client side.
$("#mute").click(function () {
alert("Handler for .click() called.");
channel.mute({
channelId : 111
},
function (err) {});
});
$("#kick").click(function () {
alert("Handler for .click() called.");
channel.hangup({
channelId : 111
},
function (err) {});
});
As anyone any experience using both or even have any suggestion as to how I can do this.
Full code listing(Server side);
var ari = require('ari-client');
var util = require('util');
var chanArr = [];
var test;
var mute;
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
//ARI client
ari.connect('http://localhost:8088', 'asterisk', 'asterisk', clientLoaded);
function clientLoaded(err, client) {
if (err) {
throw err;
}
// find or create a holding bridges
var bridge = null;
client.bridges.list(function (err, bridges) {
if (err) {
throw err;
}
bridge = bridges.filter(function (candidate) {
return candidate.bridge_type === 'mixing';
})[0];
if (bridge) {
console.log(util.format('Using bridge %s', bridge.id));
} else {
client.bridges.create({
type : 'mixing'
}, function (err, newBridge) {
if (err) {
throw err;
}
bridge = newBridge;
console.log(util.format('Created bridge %s', bridge.id));
});
}
});
// handler for StasisStart event
function stasisStart(event, channel) {
console.log(util.format(
'Channel %s just entered our application, adding it to bridge %s',
channel.name,
bridge.id));
channel.answer(function (err) {
if (err) {
throw err;
}
bridge.addChannel({
channel : channel.id
}, function (err) {
var id = chanArr.push(channel.name)
console.log("Value: " + test);
test = channel.name;
updateSip);
if (err) {
throw err;
}
//If else statement to start music for first user entering channel, music will stop once more than 1 enters the channel.
if (chanArr.length <= 1) {
bridge.startMoh(function (err) {
if (err) {
throw err;
}
});
} else if (chanArr.length === 2) {
bridge.stopMoh(function (err) {
if (err) {
throw err;
}
});
} else {}
});
});
$("#mute").click(function () {
alert("Handler for .click() called.");
channel.mute({
channelId : 111
},
function (err) {});
});
$("#kick").click(function () {
alert("Handler for .click() called.");
channel.hangup({
channelId : 111
},
}
// handler for StasisEnd event
function stasisEnd(event, channel) {
console.log(util.format(
'Channel %s just left our application', channel.name));
console.log(channel.name);
var index = chanArr.indexOf(channel.name);
chanArr.splice(index, 1);
updateSip();
}
client.on('StasisStart', stasisStart);
client.on('StasisEnd', stasisEnd);
client.start('bridge-hold');
}
//Socket.io logic here
server.listen(3009, function () {
console.log('listening on *:3009');
});
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendfile(__dirname + "/testPage.html");
});
io.sockets.on('connection', function (data) {
updateSip();
});
io.sockets.on('muting', function (data) {
mute = data;
console.log("client side:" + mute);
});
function updateSip() {
console.log("Value: " + test);
io.sockets.emit('sip', chanArr);
}
This is how am kicking channels from the bridge.
Am currently working on a Mute & Unmute function, but am having some troubles but this is how I kicked users via the web application
For example.
Client Side
When kick button is clicked emit the data found in TD to the server side
$(document).on('click', '.kick', function () {
var hangup = $(this).closest('td').siblings(':first-child').text();
socket.emit('hangup', hangup);
});
Server Side
Store the data received from client side into a var and then call the stasis function.
io.sockets.on('connection', function (socket) {
updateSip();
socket.on('hangup', function (data) {
hangup(data);
});
});
Stasis function
Kick the channel which you have just passed from client to server side using ARI client commands, user will be kicked and will show in stasis application.
function hangup(hangval) {
console.log("Kicked:" + hangval);
client.channels.hangup
({
channelId : hangval
},
function (err) {
if (err) {
throw err;
}
});
}
Hope this helps.

WebRTC works locally, but not across different IP addresses

My code is on Github at: https://github.com/rashadrussell/webrtc_experiment/blob/master/public/script.js
I am trying to write a 1-to-1 video video conferencing script with WebRTC. And is being stored on AppFog, a cloud hosting website. It works on my localhost when I test with two different Chrome windows on a single computer. It also works on AppFog when I test it on two different computers at home.
The problem occurs when I test my app with a friend living at a different house. The remote streams are not being set. My only guess is that there is some error with IP addresses, which means something is wrong my the setup of Ice Candidates. All that pops up is a black box where the remote stream is supposed to be.
Here is some of my code:
Client-Side
var isInitiator = false;
socket.on('initiatorFound', function(data) {
isInitiator = data.setInitiator;
console.log("Is Initiator? " + isInitiator);
});
navigator.getMedia = (
navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia
);
navigator.getMedia(
{video: true, audio: false},
(stream) => {
var video = document.getElementById("localView");
video.src = window.URL.createObjectURL(stream);
console.log("Add Stream");
sendMessage('streamAdd', {streamAdded: 'stream-added'});
createPeerConnection();
pc.addStream(stream);
if(isInitiator)
{
callPeer();
}
},
function(err) {
console.log("The following error occured: ");
console.dir(err);
}
);
function sendMessage(type, message)
{
console.log("Sending Message");
socket.emit('message',{
"type": type,
"message": message
});
}
function createPeerConnection() {
pc = new rtcPeerConnection(servers, options);
console.dir(pc);
pc.onicecandidate = function(evt) {
if(evt.candidate == null) return;
pc.onicecandidate = null;
console.log("Send Ice Candidate");
sendMessage("iceCandidate", JSON.stringify(evt.candidate));
};
pc.onaddstream = function(evt) {
document.body.append("<video id='remoteVideo' autoplay></video>");
var remoteVid = document.getElementById("remoteVideo");
remoteVid.src = window.URL.createObjectURL(evt.stream);
};
}
function callPeer() {
pc.createOffer(function (offer) {
pc.setLocalDescription(offer, function() {
sendMessage("offer", JSON.stringify(offer));
});
console.log("Send Offer");
}, function(err) { console.log("Offer Error: " + err) },
videoConstraints
);
}
function answerPeer() {
pc.createAnswer(function(answer) {
pc.setLocalDescription(answer);
sendMessage("answer", JSON.stringify(answer))
}, function(err) { console.log("Sending Answer Error: " + err) },
videoConstraints
);
}
socket.on('message', function(message) {
console.log("CONSOLE MESSAGE:");
console.dir(message);
if(message.type == 'streamAdd') {
console.log('Stream was added');
createPeerConnection();
if(isInitiator) {
callPeer();
}
} else if(message.type == 'offer') {
pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message)));
if(!isInitiator)
{
console.log("Sending Answer");
answerPeer();
}
} else if(message.type == 'answer') {
pc.setRemoteDescription( new rtcSessionDescription(JSON.parse(message.message)));
} else if(message.type == 'iceCandidate') {
console.log("Get Ice Candidate");
pc.addIceCandidate(new rtcIceCandidate(JSON.parse(message.message)) );
}
});
Server-Side
var isInitiator = false;
io.sockets.on('connection', function(socket) {
if (!isInitiator) {
isInitiator = true;
socket.emit('initiatorFound', {setInitiator: isInitiator});
} else {
socket.emit('initiatorFound', {setInitiator: !isInitiator});
}
// Signaling Channel
socket.on('message', function(message) {
if (message.type == 'streamAdd') {
console.log('Got message: ' + message);
}
//socket.emit('message' ,message);
// Should be:
socket.broadcast.emit('message', message);
});
});
In the line
pc = new rtcPeerConnection(servers, options);
While sending the server info through the server variable, you need to mention the addr of TURN servers as well along withthe STUN server. Typically the data gets blocked due to NAT or firewall and TURN should help in that case.
For more server details, you can check out https://gist.github.com/yetithefoot/7592580

Categories