listen(port: number, callback: any): void {
// const boundContextScopedFunc = contextScopedExecutionMethod.bind(this);
// const boundFunc = executionMethod.bind(this);
this.io.on("connection", (socket) => {
if(socket.user){
// this.rooms.socket.user=0;
// this.rooms[socket.user]++;
this.rooms.socket.user = true;
socket.join(socket.user);
}
console.log("client connected");
socket.emit("onconnect", { success: true });
// this.socketConnections.push(socket);
// console.log(socket);
socket.on("message", (data) => {
console.log("message called");
this.executionMethod(data, socket);
});
socket.on("disconnect",function(data){
const counter = this.io.sockets.clients(socket.room).length;
if(counter === 0){
// Make a REST call to a third-party service
delete this.rooms.socket.user;
}
});
});
注意:房间是一个对象,其中有一对用户名作为键,“true”作为值。我正在为每个登录的新用户的房间对象添加一个键值对。
现在在socket.on(“disconnect”,......)中,只要房间里没有用户的插座,我就会删除键值对。
我在行
中收到错误const counter = this.io.sockets.clients(socket.room).length;
这行代码是否正确或有其他方法吗?