получение нескольких console.logs в операциях с сокетами - PullRequest
0 голосов
/ 28 октября 2019

в приведенной ниже реализации socket.io я пытаюсь получить данные из сокета в приложении типа чата.

, но приложение не может хранить отдельные экземпляры.

например,когда 1 пользователь вошел в систему, когда он вызывает функцию getgroup от пользователя в клиенте, все остальные пользователи, вошедшие в систему, также называются.

client-code:

export class ExpensesSocketService {
    private url = 'http://localhost:3000/group';
    private socket;

    constructor(public http:HttpClient, ) { 
            this.socket=io(this.url); // connection is being created. i.e the handshake is happening here.
            console.log("group socket connected");
    }

    public getGroupList=(userId)=>{
            this.socket.emit('getUserGroup',userId);
    }

    public groupList=(userId)=>{
            return Observable.create((observer)=>{
                let id='groupListSuccess'+userId;
                this.socket.on(id,(data)=>{
                observer.next(data);
                })
            })
    }

}

serverкод стороны

let myGroupIo = io.of('/group');

    myGroupIo.on('connection',(socket)=>{

    console.log("on connection-- group functions are ready");

    socket.on('getUserGroup',(userId)=>{
            eventEmitter.emit('getGroup',userId);
        })
     eventEmitter.on('getGroup',(userId)=>{
        console.log('this is the socket id');
                    console.log(socket.id);

                    let id='groupListSuccess'+userId;
                    console.log(id);

                })
    })

здесь вывод, который я получаю в консоли сервера.

//if 1 user online
this is the socket id
/group#_wd5h0-OtjvROfAXAAAA
groupListSuccessU-jCfdD3Ia


//if 2 user onine
this is the socket id
/group#zM2l1IFBuzPeJ8xcAAAA
groupListSuccessU-BBtHCf1x
this is the socket id
/group#ErIqNwhnrifvbXziAAAB
groupListSuccessU-BBtHCf1x


//if 3 user online
this is the socket id
/group#1YaVWTVtJkA2cAetAAAA
groupListSuccessU-Ur0KbfuC
this is the socket id
/group#O3PA4jLJLb8Q27N6AAAB
groupListSuccessU-Ur0KbfuC
this is the socket id
/group#7adpDvnspJtvWPP-AAAC
groupListSuccessU-Ur0KbfuC

обратите внимание: запрос выполняется только userid = Ur0KbfuC ,но консоль ведет лог или код работает для всех экземпляров сокетов. код должен вести себя как-> когда пользователь вызывает событие сокета getUserGroup, только его сокет должен быть активирован, и он должен быть запущен и выдан в свой собственный идентификатор пользователя.

...