-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Description
Client (browser Chrome latest version):
Connect to server, pull out the plug online, wait 2 minutes, plug connect the Internet again.
Trigger events: disconnect, then reconnect.
The client when receiving data see error message in the console
Uncaught TypeError: Converting circular structure to JSON
and no data is being received.
From client to server data is transferred perfectly
Problems arise only when the client is lost internet connection. If the client does not reconnect no problem.
I see the error in your socket.io library.
Write how you can solve the problem?
Client Code (browser Chrome latest version):
var socket = io.connect('http://consultant.laura.ru:1988');
socket.on('disconnect', function () {
socket.client = client;
socket.emit('client_disconnect', socket);
console.log('Клиент '+client.client_id+' отключен');
});
socket.on('sendMessage', function (data) {
var audio = $("._quty_chat #c_sound")[0];
audio.play();
setMessage(data);
});
$('._quty_chat .c_send').click(function(){
var msg = $('._quty_chat .c_write input').val();
if (msg.length > 0) {
var message = {
dialog_id: dialog_id,
client_id: client.client_id,
user_id: consultant_id,
text: msg,
path: 1,
name: client.name
};
socket.emit('sendMessage', message);
setMessage(message);
$('._quty_chat .c_write input').val('');
} else {
alert('Введите текст сообщения');
}
return false;
});
Server code (NodeJS last version):
var io = require('socket.io').listen(1988);
io.set('log level', 1);
io.set('transports', ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling']);
var operators = []; //Операторы онлайн
var clients = []; //Клиенты онлайн
io.sockets.on('connection', function (socket) {
socket.on('reconnect_client', function (data) {
socket.client = data;
clients[data.client_id] = socket;
console.log(socket.client);
console.log('Клиент '+data.client_id+' переподключен');
});
socket.on('sendMessage', function(data){
var con = mysql_connect();
con.query("INSERT INTO messages VALUES ('', '"+data.dialog_id+"', '"+data.user_id+"', '"+data.client_id+"', NOW(), '"+data.text+"', '"+data.path+"')",
function(err, res, fields){
if (err) {
console.log(err);
}
if (data.path == 0) { //Отправляем сообщение клиенту
var socket_client = clients[data.client_id];
if (typeof(socket_client) != 'undefined') {
socket_client.emit('sendMessage', data);
console.log('Сообщение клиенту отправлено успешно');
} else {
console.log(clients[data.client_id]);
//console.log(clients);
console.log('Клиент '+data.client_id+' не найден');
//console.log(clients);
}
} else { //Отправляем сообщение оператору
var socket_user = operators[data.user_id];
if (typeof(socket_user) != 'undefined') {
socket_user.emit('sendMessage', data);
console.log('Сообщение оператору отправлено успешно');
} else {
console.log('Оператор '+data.user_id+' не найден');
}
}
});
});
});