When i connect to XMPP server using stropher.js it give connection status as 1 = The connection is currently being made
What is the problem for this status.
code is as below for connection.
it return me connecting status.
$(document).ready(function () {
$('#login_dialog').dialog({
autoOpen: true,
draggable: false,
modal: true,
title: 'Connect to XMPP',
buttons: {
"Connect": function () {
$(document).trigger('connect', {
jid: $('#jid').val(),
password: $('#password').val()
});
$('#password').val('');
$(this).dialog('close');
}
}
});
});
$(document).bind('connect', function (ev, data) {
var conn = new Strophe.Connection("http://127.0.0.1:5280/http-bind");
//"http://bosh.metajack.im:5280/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
Hello.log("Status DISCONNECTED.");
$(document).trigger('disconnected');
}
});
Hello.connection = conn;
});
$(document).bind('connected', function () {
// inform the user
Hello.log("Connection established.");
Hello.connection.addHandler(Hello.handle_pong, null, "iq", null, "ping1");
var domain = Strophe.getDomainFromJid(Hello.connection.jid);
Hello.send_ping(domain);
});
$(document).bind('disconnected', function () {
Hello.log("Connection terminated.");
// remove dead connection object
Hello.connection = null;
});
I am using phone gap.
Thanks
Your code at Stroph.Connection is wrong. First, you verify that passing URL parameter to Stroph.connnection("URL") is valid or not.
$(document).bind('connect', function (ev, data) {
var conn = new Strophe.Connection("http://example.com:7070/http-bind");
//"http://bosh.metajack.im:5280/xmpp-httpbind");
In the code above, 7070 is for unsecured port for HTTP binding connection on Openfire.
If your issue is not solved, please provide which XMPP server you are using in your PhoneGap application.
Related
On my project each user is joining his unique channel (I know that the user is also in a socked id channel).
Client
var socket = io('http://localhost:8000/');
var room = "unique_id";
socket.on('connect', function () {
socket.emit('room', room);
});
socket.on("unique_id", function() {
console.log("test");
})
On the server side I am checking the rooms a user is connected to:
var io = require('socket.io')(8000);
io.on('connection', function(socket) {
var room;
socket.on('room', function(unique_id) {
socket.join(unique_id);
room = unique_id;
console.log(io.sockets.adapter.rooms); //show the unique_id room
io.to(room).emit('hello', 'msg');
});
socket.on(room, function(data) {
console.log(room);
})
});
Output on connect
{ 'DQOt2DX2IsHh-m9nAAAC': { sockets: { 'DQOt2DX2IsHh-m9nAAAC': true }, length: 1 },
'unique_id': { sockets: { 'DQOt2DX2IsHh-m9nAAAC': true }, length: 1 } }
Now I am using elephant.io to emit to the client as follows
$client = new Client(new Version1X('http://localhost:8000'));
$client->initialize();
$client->emit('unique_id', ['news' => 'bar']);
$client->close();
I think there is something wrong on my server side but I am not able to solve it. I need to process the events for the unique_id but how?
server side
var io = require('socket.io')(8000);
io.on('connection', function(socket) {
var room;
socket.on('room', function(address) {
socket.join(address);
room = address;
console.log(io.sockets.adapter.rooms);
io.to(room).emit('hello', 'msg');
});
socket.on('message', function(data) {
console.log(data.unique_id);
io.to(data.unique_id).emit('hello', 'msg');
})
});
elephant.io
$client->emit('message', ['unique_id' => 'foo']);
This is my solution which is working for me.
I am using the following code to create/subscribe to a topic and handle the messages. Sometimes the connection gets lost and the error says:
Whoops! The connection was lost...
I would like to know if there is a way to reconnect it. Is it possible in the error callback or define the whole code in a method and call it recursively in error callback ?
$(document).ready(function () {
........
...............
try {
var socket = new SockJS("${createLink(uri: '/stomp')}");
var client = Stomp.over(socket);
client.connect({}, function () {
client.subscribe("/topic/${userInstance?.username}",
function (message) {
............
....................
});
});
} catch (error) {
console.log("ERROR: " + error.toString());
}
});
I managed to do it using failure callback and connect again. It will keep trying as long as it will fail.
This is what I am using in a Polymer element:
ready: function() {
this.connectWs();
},
connectWs: function() {
this.socket = new WebSocket(this.socketUrl);
this.stompClient = Stomp.over(this.socket);
this.stompClient.debug = null;
this.stompClient.connect({},
function(frame) {
// Connection OK
}.bind(this),
function(e) {
console.error(e, "Reconnecting WS", this.socketUrl);
window.setTimeout(function() {
this.connectWs();
}.bind(this), 2500);
}.bind(this)
);
},
I hope you all are doing well. I'm trying to establish connection to socket.io server from inside of the worker.js file using importScripts which loads the socket.io-client js file which is in the same directory with worker.js. After loading socket.io-client
by using var socket = io.connect('http://38.98.xxx.xxx:6000'); I am trying to establish connection to socket.io server on different host, but it ain't working. Please point me in the right direction.I appreciate any help.
<script>
var worker = new SharedWorker("http://baseUrl.com/js/push/worker/worker.js");
worker.port.addEventListener("message", function(e) {
console.log("Got message: " + e.data);
}, false);
worker.port.start();
worker.port.postMessage("start");
</script>
worker.js
importScripts('socket.io.js');
var socket = io.connect('http://38.98.154.167:6000');
var connections = 0;
self.addEventListener("connect", function(e) {
var port = e.ports[0];
connections ++;
port.addEventListener("message", function(e) {
if (e.data === "start") {
port.postMessage('hello');
}
}, false);
port.start();
}, false);
socket.on('connect', function () {
port.postMessage('connect');
});
socket.on('disconnect', function () {
port.postMessage('disconnect');
});
I figured it out. Just had to move
socket.on('connect', function () {
port.postMessage('connect');
});
socket.on('disconnect', function () {
port.postMessage('disconnect');
});
into the self.addEventListener("connect", function(e) {});in the worker.js and change from var socket=io.connect('http://38.98.xxx.xxx:6000');
to
var socket = io('http://38.98.xxx.xxx:6000');
Here is the working example is case if anybody needs.
worker.js
importScripts('socket.io.js');
var socket = io('http://38.98.xxx.xxx:6000');
var connections = 0;
self.addEventListener("connect", function(e) {
var port = e.ports[0];
connections ++;
port.addEventListener("message", function(e) {
if (e.data === "start") {
port.postMessage('hello');
}
}, false);
port.start();
socket.on('push', function(pushed){
port.postMessage(pushed);
});
socket.on('connect', function () {
port.postMessage('connect');
});
socket.on('disconnect', function () {
port.postMessage('disconnect');
});
}, false);
There is a drop in replacement for const io = require('socket.io-client');
which runs the connection for the returned socket in a dedicated webworker. It is
const io = require('sockerworker.io');
const socket = io([url][, options]);
Instead of writing your own boilerplate for the webworker, you could use this. It is available here via npm. (disclosure: I am its author.)
I have a requirement using node js that handles disconnecting a user from a chat application.
I am not sure how to handle telling the difference between a browser closing and a user refreshing the browser.
client.on('disconnect', function () {
console.log( 'Disconnected' );
// run mysql code to remove user from logged in table
});
I have googled for a couple hours and cannot find a solution.
This seems like something pretty simple and I think it is the keywords that I am using.
Can someone point me in the right direction on how to handle this?
Thanks in advance.
One way would be to generate a random UID and save it to local storage. Right after the client connects, send this UID to the server and check to see if that UID exists as a connected user. On the server side, set a timeout in the disconnect that gives the user 15 seconds or so before their unique UID is deleted from the "users online" data.
Client:
// When the client starts, create the uid.
localstorage.setItem('uUID', Math.random().toString(24) + new Date());
// Emit the UID right after connection
socket.emit('userLogin', localstorage.getItem('uUID');
Server:
var currentUIDS = [];
var userIsConnected = true;
io.sockets.on('connection', function (socket) {
var currentUID = null;
socket.on('userLogin', function (data) {
if (data !== null) {
if (currentUIDS.includes(data)) {
userIsConnected = true;
currentUID = data;
}
}
});
socket.on('disconnect', function () {
userIsConnected = false;
setTimeout(function () {
if (!userIsConnected) currentUIDS.pop(currentUID);
}, 15000);
});
});
I have a better solution for that to handle multiple users:
var users = [],
users_connected = [];
io.on('connection', function(socket) {
var uid = null;
// register the new user
socket.on('register', function (user_uid) {
if ( users_connected.indexOf(user_uid) < 0 ) {
users_connected.push(user_uid);
}
if ( users.indexOf(user_uid) < 0 ) {
console.log('New user connected: ' + user_uid);
users.push(user_uid);
// notify other clients that a new user has joined
socket.broadcast.emit('user:join', {
name: user_uid,
users: users_connected.length
});
}
uid = user_uid;
});
// clean up when a user leaves, and broadcast it to other users
socket.on('disconnect', function () {
users_connected.splice( users_connected.indexOf(uid), 1);
setTimeout(function () {
if ( users_connected.indexOf(uid) < 0 ) {
socket.broadcast.emit('user:left', {
name: uid
});
var index = users.indexOf(uid);
users.splice(index, 1);
}
}, 3000);
});
});
I am currently using this stack expres, socket.io, sessionstore. I followed the article here http://www.danielbaulig.de/socket-ioexpress/.
Well the problem is that i cannot modify the session values in socket.io callback.
Access from express side works well, the item get increased after each refresh.
app.get('/mysession', function(req, res) {
req.session.item++;
console.log(req.session);
res.render('session.jade', {
title: 'Sample title'
});
});
Using in socket.io side it does not and here is the problem, maybe i am setting the wrong object.
var io = io.listen(app);
io.sockets.on('connection', function(socket) {
var handshake = socket.handshake;
onlineCount++;
console.log('Well done id %s', handshake.sessionID);
handshake.session.item++;
console.log(handshake.session);
});
Here is bridge code.
io.set('authorization', function(data, accept) {
if (data.headers.cookie) {
data.cookie = parseCookie(data.headers.cookie);
data.sessionID = data.cookie['express.sid'];
sessionStore.get(data.sessionID, function(err, session) {
if (err || !session) {
accept('Error', false);
} else {
data.session = session;
accept(null, true);
}
});
} else {
return accept('No cookie tansmitted', false);
}
});
The only way I found to make this work is to grab the cookie from the request object on the connect event, parse it with your favourite cookie parser (I use connect.utils.parseCookie), and set it on that socket so that I may access it in future events:
socket.on('connection', function(client) {
var cookie = client.request.headers.cookie;
var pcookie = connect.utils.parseCookie(cookie);
var session_id = pcookie["connect.sid"];
if (session_id) {
sessionStore.get(session_id, function(err, sess) {
// do whatever you want with sess here
// ...
// if you want to "save" the session for future events
client.set('session_id', session_id);
}
}
});
The sessionStore API changed a little bit, now its sessionStore.load(sessionId, cb) instead of .get.