Socket.io-haqida soket ajratiladi

Socket.io bilan ushbu senariy bor:

Ma'lumotlarni o'chirishni istayman va Vebclientga ma'lumotlarni uzatmoqchiman. Ammo ko'p ma'lumot olsam va sahifani yopsam, u konsolda

DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
DISCONNECTED FROM CLIENT
...(a lot)

Kodi:

server:

var express=require('express');
var app=express();
var net=require('net');
var http=require('http').createServer(app);
var io=require('socket.io')(http);
var net=require('net');
var nodeServer = new net.Socket();
var aSocket=null;

io.on('connection', function (socketIO) {
  aSocket=socketIO;
};
nodeServer.on('data', function(data) {
 if(aSocket!=null){
    aSocket.emit('pushToWebClient',useData);
    aSocket.on('disconnect', function() {
      console.log('DISCONNECTED FROM CLIENT');
    });

  }

mijoz:

socket.on('pushToWebClient', function (useData) {


    });

Men topdim

aSocket.on('disconnect', function() {
          console.log('DISCONNECTED FROM CLIENT');
        });

konsolda juda ko'p "mijozdan" tanlangan ", lekin aslida kodda bir marta konsol bo'lishi kerak.

Hatto console.log (aSocket.id) ham bor edi, faqat bittasi konsol.

Nima uchun u ko'p marta konsol ekanini bilmayman.

Men ishlatilgan setMaxListeners (10) dan qochishga harakat qildim. Bu xotira sızıntısına nima bo'ladi?

0
Veb-brauzer har safar yangi veb-sahifaga o'tayotganida, veb-sahifadagi socket.io ulanishi o'chiriladi. Berilgan havola muayyan sahifaga va faqat shu sahifaga bog'langan. Brauzer ushbu sahifani tark etganda, o'sha sahifadagi barcha resurslar yopiladi va ozod qilinadi. Odatda, ayni saytdagi keyingi veb-sahifa faqat o'sha sahifaning davomiyligi uchun foydalanishi mumkin bo'lgan yangi ulanishni ochadi.
qo'shib qo'ydi muallif jfriend00, manba
@ jfriend00 Men aniq aytolmayman ... Men sizning ma'numatingizni bilaman. Serverdan qabul qilish uchun faqat bitta sahifani ochaman. sahifani yopganimda, u "konsullikdan olib tashlangan" ko'p konsolga ega. Men hatto console.log ("aSocket.id") ham bor edi, bu faqat bitta id edi. Bu meni eng qorong'u.
qo'shib qo'ydi muallif TDarray, manba

9 javoblar

Bu socket.io uchun asosiy koddir

     

Quyidagi misol socket.io-ni oddiy Node.JS HTTP-ga taqdim etadi   server 3000 serverini tinglash.

var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(client){
  client.on('event', function(data){});
  client.on('disconnect', function(){});
});
server.listen(3000);

O'ylaymanki, sinash kerak.

1
qo'shib qo'ydi
Men veb-saytga ko'ra, lekin shunga o'xshash natija
qo'shib qo'ydi muallif TDarray, manba

Bir xil disconnect hodisasi uchun bir nechta hodisa tinglovchilarini ro'yxatdan o'tkazayotganingiz ko'rinadi. Ushbu kodda:

nodeServer.on('data', function(data) {
 if(aSocket!=null){
    aSocket.emit('pushToWebClient',useData);
    aSocket.on('disconnect', function() {
      console.log('DISCONNECTED FROM CLIENT');
    });

  }

data </​​code> xabarini har safar olganingizda yangi ajratish hodisani tinglovchi ro'yxatga olinadi. Shunday qilib, bir nechta tinglovchilaringiz bo'lsa, har bir kishi rozetkadan ajratilganda chaqiriladi va natija shuni anglatadiki, bir xil xabarni bir xil priz uchun bir necha bor kirishingiz mumkin.

connect işleyicisini ulanishi işleyicisine ko'chirish orqali nima sodir bo'lganini tasdiqlashingiz mumkin, shuning uchun faqat har bir soket uchun faqat bir marta qo'shiladi.

Bundan tashqari, asocket ni global yoki modul darajasidagi o'zgaruvchiga qo'yish, server kodingiz faqat bir vaqtning o'zida bitta mijoz bilan ishlashini anglatadi. nodeserver havolasi haqida ma'lumot olganda, nima qilishni xohlayotganingiz aniq emas - bu ma'lumotni faqat ma'lum bir mijozga yoki barcha bog'langan mijozlarga yuborishga harakat qilyapsizmi.

0
qo'shib qo'ydi
katta rahmat. Mening inglizcham aniq ifoda etish uchun juda kambag'aldir. Uni aloqa o'rnatuvchiga ko'chirishga urinib ko'rganman, faqat bir marta konsol. Lekin agar buni qilsam, nodeServer kodidan data </​​code> Bir server, ma'lumotni mijozga qanday yo'naltirishni bilmayman.
qo'shib qo'ydi muallif TDarray, manba

Bir xil disconnect hodisasi uchun bir nechta hodisa tinglovchilarini ro'yxatdan o'tkazayotganingiz ko'rinadi. Ushbu kodda:

nodeServer.on('data', function(data) {
 if(aSocket!=null){
    aSocket.emit('pushToWebClient',useData);
    aSocket.on('disconnect', function() {
      console.log('DISCONNECTED FROM CLIENT');
    });

  }

data </​​code> xabarini har safar olganingizda yangi ajratish hodisani tinglovchi ro'yxatga olinadi. Shunday qilib, bir nechta tinglovchilaringiz bo'lsa, har bir kishi rozetkadan ajratilganda chaqiriladi va natija shuni anglatadiki, bir xil xabarni bir xil priz uchun bir necha bor kirishingiz mumkin.

connect işleyicisini ulanishi işleyicisine ko'chirish orqali nima sodir bo'lganini tasdiqlashingiz mumkin, shuning uchun faqat har bir soket uchun faqat bir marta qo'shiladi.

Bundan tashqari, asocket ni global yoki modul darajasidagi o'zgaruvchiga qo'yish, server kodingiz faqat bir vaqtning o'zida bitta mijoz bilan ishlashini anglatadi. nodeserver havolasi haqida ma'lumot olganda, nima qilishni xohlayotganingiz aniq emas - bu ma'lumotni faqat ma'lum bir mijozga yoki barcha bog'langan mijozlarga yuborishga harakat qilyapsizmi.

0
qo'shib qo'ydi
katta rahmat. Mening inglizcham aniq ifoda etish uchun juda kambag'aldir. Uni aloqa o'rnatuvchiga ko'chirishga urinib ko'rganman, faqat bir marta konsol. Lekin agar buni qilsam, nodeServer kodidan data </​​code> Bir server, ma'lumotni mijozga qanday yo'naltirishni bilmayman.
qo'shib qo'ydi muallif TDarray, manba

Bir xil disconnect hodisasi uchun bir nechta hodisa tinglovchilarini ro'yxatdan o'tkazayotganingiz ko'rinadi. Ushbu kodda:

nodeServer.on('data', function(data) {
 if(aSocket!=null){
    aSocket.emit('pushToWebClient',useData);
    aSocket.on('disconnect', function() {
      console.log('DISCONNECTED FROM CLIENT');
    });

  }

data </​​code> xabarini har safar olganingizda yangi ajratish hodisani tinglovchi ro'yxatga olinadi. Shunday qilib, bir nechta tinglovchilaringiz bo'lsa, har bir kishi rozetkadan ajratilganda chaqiriladi va natija shuni anglatadiki, bir xil xabarni bir xil priz uchun bir necha bor kirishingiz mumkin.

connect işleyicisini ulanishi işleyicisine ko'chirish orqali nima sodir bo'lganini tasdiqlashingiz mumkin, shuning uchun faqat har bir soket uchun faqat bir marta qo'shiladi.

Bundan tashqari, asocket ni global yoki modul darajasidagi o'zgaruvchiga qo'yish, server kodingiz faqat bir vaqtning o'zida bitta mijoz bilan ishlashini anglatadi. nodeserver havolasi haqida ma'lumot olganda, nima qilishni xohlayotganingiz aniq emas - bu ma'lumotni faqat ma'lum bir mijozga yoki barcha bog'langan mijozlarga yuborishga harakat qilyapsizmi.

0
qo'shib qo'ydi
katta rahmat. Mening inglizcham aniq ifoda etish uchun juda kambag'aldir. Uni aloqa o'rnatuvchiga ko'chirishga urinib ko'rganman, faqat bir marta konsol. Lekin agar buni qilsam, nodeServer kodidan data </​​code> Bir server, ma'lumotni mijozga qanday yo'naltirishni bilmayman.
qo'shib qo'ydi muallif TDarray, manba

EDIT: I'm moving this to the top because I saw that someone already provided my solution but you were having a problem managing the data sent to the client. Your aSocket variable will be overwritten by every new client that connects to your app. If you want to send data to a specific client using your server nodeServer, you should create a global variable (an array) that keeps track of all of your client socket connections. So instead of using one global variable aSocket do the following:

var net=require('net');
var nodeServer = new net.Socket();
var clients = [];

io.on('connection', function (socketIO) {
    clients.push(socketIO);
    var clientNum = clients.length-1;
    aSocket.on('disconnect', function() {
        clients.splice(clientNum, 1);
        console.log('DISCONNECTED FROM CLIENT: '+socketIO.id);
    });
};

nodeServer.on('data', function(data) {
//have your data object contain an identifier for the client that caused the handler to fire
//for the sake of the answer I just use data.id

    var clientID = data.id;
    if(clients[clientID]!=null){
        clients[clientID].emit('pushToWebClient', useData);
    }
}

Let me know how it goes! My original answer is below:

Ko'chirib ko'ring

aSocket.on('disconnect', function() {
    console.log('DISCONNECTED FROM CLIENT');
});

nodeServer.on ("ma'lumotlar", ...) hodisani tinglovchilaringizdan io.on ('aloqa', ...)

io.on('connection', function (socketIO) {
    aSocket=socketIO;
    aSocket.on('disconnect', function() {
        console.log('DISCONNECTED FROM CLIENT');
    });
};

socket.io is designed to keep polling for the presence of the server/client. If either the server or the client are disconnected, the remaining 'side' continues to receive polling requests and, consequently, will continuously print an error.

Serverni uzish va mijozlar sahifasini ochiq qoldirganda, bu effektni brauzeringizdagi mijoz tomonida ko'rishingiz mumkin. Brauzer xatosini/konsolining jurnaliga qaralsangiz, siz ko'rib turganingizdek net :: ERR_CONNECTION_REFUSED xatolarining uzluksiz oqimidir. Sizning serveringiz uchun .on ("ma'lumotlar", ...) ishlov berish uskunasiga ajratilgan voqea ishlovchilarini qo'yib, bu vaziyatning suhbatini ko'rmoqdasiz.

aniq: ERR_CONNECTION_REFUSED misol

0
qo'shib qo'ydi

EDIT: I'm moving this to the top because I saw that someone already provided my solution but you were having a problem managing the data sent to the client. Your aSocket variable will be overwritten by every new client that connects to your app. If you want to send data to a specific client using your server nodeServer, you should create a global variable (an array) that keeps track of all of your client socket connections. So instead of using one global variable aSocket do the following:

var net=require('net');
var nodeServer = new net.Socket();
var clients = [];

io.on('connection', function (socketIO) {
    clients.push(socketIO);
    var clientNum = clients.length-1;
    aSocket.on('disconnect', function() {
        clients.splice(clientNum, 1);
        console.log('DISCONNECTED FROM CLIENT: '+socketIO.id);
    });
};

nodeServer.on('data', function(data) {
//have your data object contain an identifier for the client that caused the handler to fire
//for the sake of the answer I just use data.id

    var clientID = data.id;
    if(clients[clientID]!=null){
        clients[clientID].emit('pushToWebClient', useData);
    }
}

Let me know how it goes! My original answer is below:

Ko'chirib ko'ring

aSocket.on('disconnect', function() {
    console.log('DISCONNECTED FROM CLIENT');
});

nodeServer.on ("ma'lumotlar", ...) hodisani tinglovchilaringizdan io.on ('aloqa', ...)

io.on('connection', function (socketIO) {
    aSocket=socketIO;
    aSocket.on('disconnect', function() {
        console.log('DISCONNECTED FROM CLIENT');
    });
};

socket.io is designed to keep polling for the presence of the server/client. If either the server or the client are disconnected, the remaining 'side' continues to receive polling requests and, consequently, will continuously print an error.

Serverni uzish va mijozlar sahifasini ochiq qoldirganda, bu effektni brauzeringizdagi mijoz tomonida ko'rishingiz mumkin. Brauzer xatosini/konsolining jurnaliga qaralsangiz, siz ko'rib turganingizdek net :: ERR_CONNECTION_REFUSED xatolarining uzluksiz oqimidir. Sizning serveringiz uchun .on ("ma'lumotlar", ...) ishlov berish uskunasiga ajratilgan voqea ishlovchilarini qo'yib, bu vaziyatning suhbatini ko'rmoqdasiz.

aniq: ERR_CONNECTION_REFUSED misol

0
qo'shib qo'ydi

EDIT: I'm moving this to the top because I saw that someone already provided my solution but you were having a problem managing the data sent to the client. Your aSocket variable will be overwritten by every new client that connects to your app. If you want to send data to a specific client using your server nodeServer, you should create a global variable (an array) that keeps track of all of your client socket connections. So instead of using one global variable aSocket do the following:

var net=require('net');
var nodeServer = new net.Socket();
var clients = [];

io.on('connection', function (socketIO) {
    clients.push(socketIO);
    var clientNum = clients.length-1;
    aSocket.on('disconnect', function() {
        clients.splice(clientNum, 1);
        console.log('DISCONNECTED FROM CLIENT: '+socketIO.id);
    });
};

nodeServer.on('data', function(data) {
//have your data object contain an identifier for the client that caused the handler to fire
//for the sake of the answer I just use data.id

    var clientID = data.id;
    if(clients[clientID]!=null){
        clients[clientID].emit('pushToWebClient', useData);
    }
}

Let me know how it goes! My original answer is below:

Ko'chirib ko'ring

aSocket.on('disconnect', function() {
    console.log('DISCONNECTED FROM CLIENT');
});

nodeServer.on ("ma'lumotlar", ...) hodisani tinglovchilaringizdan io.on ('aloqa', ...)

io.on('connection', function (socketIO) {
    aSocket=socketIO;
    aSocket.on('disconnect', function() {
        console.log('DISCONNECTED FROM CLIENT');
    });
};

socket.io is designed to keep polling for the presence of the server/client. If either the server or the client are disconnected, the remaining 'side' continues to receive polling requests and, consequently, will continuously print an error.

Serverni uzish va mijozlar sahifasini ochiq qoldirganda, bu effektni brauzeringizdagi mijoz tomonida ko'rishingiz mumkin. Brauzer xatosini/konsolining jurnaliga qaralsangiz, siz ko'rib turganingizdek net :: ERR_CONNECTION_REFUSED xatolarining uzluksiz oqimidir. Sizning serveringiz uchun .on ("ma'lumotlar", ...) ishlov berish uskunasiga ajratilgan voqea ishlovchilarini qo'yib, bu vaziyatning suhbatini ko'rmoqdasiz.

aniq: ERR_CONNECTION_REFUSED misol

0
qo'shib qo'ydi

EDIT: I'm moving this to the top because I saw that someone already provided my solution but you were having a problem managing the data sent to the client. Your aSocket variable will be overwritten by every new client that connects to your app. If you want to send data to a specific client using your server nodeServer, you should create a global variable (an array) that keeps track of all of your client socket connections. So instead of using one global variable aSocket do the following:

var net=require('net');
var nodeServer = new net.Socket();
var clients = [];

io.on('connection', function (socketIO) {
    clients.push(socketIO);
    var clientNum = clients.length-1;
    aSocket.on('disconnect', function() {
        clients.splice(clientNum, 1);
        console.log('DISCONNECTED FROM CLIENT: '+socketIO.id);
    });
};

nodeServer.on('data', function(data) {
//have your data object contain an identifier for the client that caused the handler to fire
//for the sake of the answer I just use data.id

    var clientID = data.id;
    if(clients[clientID]!=null){
        clients[clientID].emit('pushToWebClient', useData);
    }
}

Let me know how it goes! My original answer is below:

Ko'chirib ko'ring

aSocket.on('disconnect', function() {
    console.log('DISCONNECTED FROM CLIENT');
});

nodeServer.on ("ma'lumotlar", ...) hodisani tinglovchilaringizdan io.on ('aloqa', ...)

io.on('connection', function (socketIO) {
    aSocket=socketIO;
    aSocket.on('disconnect', function() {
        console.log('DISCONNECTED FROM CLIENT');
    });
};

socket.io is designed to keep polling for the presence of the server/client. If either the server or the client are disconnected, the remaining 'side' continues to receive polling requests and, consequently, will continuously print an error.

Serverni uzish va mijozlar sahifasini ochiq qoldirganda, bu effektni brauzeringizdagi mijoz tomonida ko'rishingiz mumkin. Brauzer xatosini/konsolining jurnaliga qaralsangiz, siz ko'rib turganingizdek net :: ERR_CONNECTION_REFUSED xatolarining uzluksiz oqimidir. Sizning serveringiz uchun .on ("ma'lumotlar", ...) ishlov berish uskunasiga ajratilgan voqea ishlovchilarini qo'yib, bu vaziyatning suhbatini ko'rmoqdasiz.

aniq: ERR_CONNECTION_REFUSED misol

0
qo'shib qo'ydi

Kodni o'chirishga harakat qilaman:

aSocket.on('disconnect', function() {
      console.log('DISCONNECTED FROM CLIENT');
    });

yoki nodeServer işleyicisinden tashqariga ko'chirish,

u odatdagiday o'zgaradi va hech qachon meniMaksliserga taklif qilmaydi.

O'ylaymanki, bir API API ichiga noto'g'ri kiritilgan bo'lishi mumkin

Va enventor rozetkani bo'shatmasligi mumkin, shuning uchun u ko'p marta konsol bo'ladi.

0
qo'shib qo'ydi
NodeJS Uzbekistan
NodeJS Uzbekistan
152 ishtirokchilar

Guruh NodeJS bo'yicha muloqot qilish uchun ochilgan. Sizni qiziqtirsa: @nodejs_uz @react_uz @angular_uz @yiiframework_uz @js_uzb @typescript_uzb @vuejs_uz @ngTashkent @laravel_uz @uzdevgroup Ish o'rinlari @UzDev_jobs

Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb