尝试与socket.io聊天 这是我的服务器
var io = require('socket.io');
connect = require('connect');
var Sequelize = require("sequelize");
var sequelize = new Sequelize('XXXXXX', 'XXXX', 'XXXXX');
var app = connect().use(connect.static('public')).listen(80,'myip');
var server_listen = io.listen(app);
server_listen.sockets.on('connection',function(socket){
socket.on('entrance1',function(data){
//insert socket id
if(!isNaN(parseFloat(data.user_id)) && isFinite(data.user_id)){
var friends_user='select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where user_id in (select user_id from base_friendship where (user_id='+data.user_id+' or friend_id= '+data.user_id+') and is_active=1) or user_id in (select friend_id from base_friendship where (user_id='+data.user_id+' or friend_id= '+data.user_id+') and is_active=1)';
sequelize.query('select * from msg_users_sockets where user_id='+data.user_id, null, {raw: true}).success(function(result){
if(result.length>0){
sequelize.query("INSERT INTO `msg_users_sockets` (`user_id` ,`socket_id`,`cookie_id`)VALUES ('"+data.user_id+"', '"+socket.id+"','"+result[0]['cookie_id']+"'); ").success(function(myTableRows) {});
}
else{
var tim=new Date().getTime();
sequelize.query("INSERT INTO `msg_users_sockets` (`user_id` ,`socket_id`,`cookie_id`)VALUES ('"+data.user_id+"', '"+socket.id+"','"+tim+"'); ").success(function(myTableRows) {});
}
sequelize.query(friends_user).success(function(result1){
socket.emit('online',{message:'you are online',user_id:data.user_id});
for(var i=0;i<result1.length;i++){
server_listen.sockets.socket(result1[i]['s']).emit('entrance',{message:'Anew chatter is online'+socket.id,user_id:data.user_id});
}
});
});
}
});
socket.on('remove_tab_user', function (data) {
if(!isNaN(parseFloat(data.user_id)) && isFinite(data.user_id)){
sequelize.query('select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where socket_id!="'+socket.id+'" and user_id='+data.user_id, null, {raw: true}).success(function(result){
for(var i=0;i<result.length;i++){
if(result[i]['s']!=socket.id)
server_listen.sockets.socket(result[i]['s']).emit('remove_tab_socket',data);
}
});
}
});
socket.on('add_tab_user', function (data) {
if(!isNaN(parseFloat(data.user_id)) && isFinite(data.user_id)){
sequelize.query('select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where socket_id!="'+socket.id+'" and user_id='+data.user_id, null, {raw: true}).success(function(result){
for(var i=0;i<result.length;i++){
if(result[i]['s']!=socket.id)
server_listen.sockets.socket(result[i]['s']).emit('add_tab_socket',data);
}
});
}
});
socket.on('chat', function (data) {
// socket.broadcast.emit('updateusers',data);
if(!isNaN(parseFloat(data[0].sender_id)) && isFinite(data[0].sender_id)){
var friends_user='select substring(socket_id, 1, length(socket_id)) s,socket_id i from msg_users_sockets where user_id in (select user_id from base_friendship where (user_id='+data[0].sender_id+' or friend_id= '+data[0].sender_id+') and is_active=1) or user_id in (select friend_id from base_friendship where (user_id='+data[0].sender_id+' or friend_id= '+data[0].sender_id+') and is_active=1)';
sequelize.query(friends_user, null, {raw: true}).success(function(result1){
for(var i=0;i<result1.length;i++){
if(result1[i]['s']!=socket.id)
server_listen.sockets.socket(result1[i]['s']).emit('updateusers',data);
}
});
}
});
socket.on('disconnect', function () {
sequelize.query('select * from msg_users_sockets where socket_id='+socket.id, null, {raw: true}).success(function(result){
if(result.length>0){
sequelize.query('select * from msg_users_sockets where user_id='+result[0]['user_id'], null, {raw: true}).success(function(result2){
if(result2.length>0){
var friends_user='select substring(socket_id, 1, length(socket_id)) s from msg_users_sockets where user_id in (select user_id from base_friendship where (user_id='+result[0]['user_id']+' or friend_id= '+result[0]['user_id']+') and is_active=1) or user_id in (select friend_id from base_friendship where (user_id='+result[0]['user_id']+' or friend_id= '+result[0]['user_id']+') and is_active=1)';
sequelize.query(friends_user).success(function(result1){
for(var i=0;i<result1.length;i++){
server_listen.sockets.socket(result1[i]['s']).emit('entrance2',{message:'Anew chatter is online'+socket.id,user_id:result[0]['user_id']});
}
});
}
});
}
sequelize.query("delete from msg_users_sockets where socket_id="+socket.id).success(function(myTableRows) {
});
server_listen.sockets.emit('entrance',{message:'Anew chatter is disconnect'+socket.id});
});
});
});
这是我的客户代码:
var socket=io.connect('http://myib:80',{
'max re connection attempts':5,
'sync disconnect on unload':true
});
这是客户活动 '错误' 'reconnect_failed' '连接失败' 还有一些聊天活动等等 当socket尝试连接时给我这个警告: “websocket连接无效” 套接字连接一两次 有时插座断开连接 当尝试socket重新连接时给我同样的警告 当某些情况下可能开始插座5 6 7时,切断与其中一些插座的接触并再次尝试连接,但会发出相同的警告并继续尝试,直到重新加载页面才能连接
我正在使用最新版本的节点 我试试 io.set(“transports”,[“xhr-polling”,“jsonp-polling”]); 请告诉我导致此问题及其解决方案的所有可能性。 感谢。
答案 0 :(得分:0)
您没有启用可能导致问题的所有传输。使用此:
io.set('transports', [
'websocket',
'flashsocket',
'htmlfile',
'xhr-polling'
'jsonp-polling',
]);
});
另外,我怀疑你在
中对服务器的引用var socket=io.connect('http://myib:80')
这是什么myib ???你已经定义了吗?您应该使用localhost或IP地址代替myib