Nod
Nod

Reputation: 63

socket io client connect twice

when my socket io client connect to the server it does it twice double connection

this is an angular 2 project so i code in typescript as you can see :

class Server {
    private app = express();
    private httpServer = http.createServer(this.app);
    private io = sio.listen(this.httpServer);
    private users = Array<User>();
    constructor() {
        //routage sur index.html
        this.app.get('/',(req, res) => {
            res.sendFile(__dirname + '/index.html');
        });
        //ajout des dépendences
        this.app.use(express.static(__dirname + '/'));
        //connexion & deconnexion
        this.io.on('connection', (socket: SocketIO.Socket) => {
            var date = new Date();
            console.log(date+' : a user connected '+socket.id);
            
            socket.on('broadcast users srv',(user) => {
                var b = new Branch();
                var n = new NVNode(b);n.image_path = user._node._image_path;
                var u = new User(user._mail,user._id,n);
                u.socket = socket.id;
                this.users.push(u);
                socket.broadcast.emit('broadcast users clt',u)
            });
          });
      }
}

i tried to code it like that :

class Server {
    private app = express();
    private httpServer = http.createServer(this.app);
    private io = sio.listen(this.httpServer);
    private users = Array<User>();
    private s : SocketIO.Socket
    constructor() {
        //routage sur index.html
        this.app.get('/',(req, res) => {
            res.sendFile(__dirname + '/index.html');
        });
        //ajout des dépendences
        this.app.use(express.static(__dirname + '/'));
        //connexion & deconnexion
        this.io.on('connection', (sock: SocketIO.Socket) => {
            var date = new Date();
            console.log(date+' : a user connected '+sock.id);
            this.s = sock;
            
        });
        
        this.s.on('broadcast users srv',(user) => {
            var b = new Branch();
            var n = new NVNode(b);n.image_path = user._node._image_path;
            var u = new User(user._mail,user._id,n);
            u.socket = this.s.id;
            this.users.push(u);
            this.s.broadcast.emit('broadcast users clt',u)
        });
      }
}

but unfortunately it shows to me this error : error server

The server tell that the value is not initialized ==> "undefined"

I really do not know what to do :( if you have an idea !

Upvotes: 1

Views: 1858

Answers (2)

SnareChops
SnareChops

Reputation: 13347

You are attempting to use s before it is initialized. By moving the this.s.on call into the this.io.on callback, you will ensure that s is not undefined.

class Server {
  private app = express();
  private httpServer = http.createServer(this.app);
  private io = sio.listen(this.httpServer);
  private users = Array<User>();
  private s : SocketIO.Socket
  constructor() {
    //routage sur index.html
    this.app.get('/',(req, res) => {
        res.sendFile(__dirname + '/index.html');
    });
    //ajout des dépendences
    this.app.use(express.static(__dirname + '/'));
    //connexion & deconnexion
    this.io.on('connection', (sock: SocketIO.Socket) => {
        var date = new Date();
        console.log(date+' : a user connected '+sock.id);
        this.s = sock;
        this.s.on('broadcast users srv',(user) => {
          var b = new Branch();
          var n = new NVNode(b);n.image_path = user._node._image_path;
          var u = new User(user._mail,user._id,n);
          u.socket = this.s.id;
          this.users.push(u);
          this.s.broadcast.emit('broadcast users clt',u)
        });
    });
  }
}

Upvotes: 0

Daniel Rosenwasser
Daniel Rosenwasser

Reputation: 23483

this.s is uninitialized until this.io receives a "connection" event. If that's the only place you're initializing, you can only subscribe this.s to other events upon a connection.

Switching from

    this.io.on('connection', (sock: SocketIO.Socket) => {
        var date = new Date();
        console.log(date+' : a user connected '+sock.id);
        this.s = sock;

    });

    this.s.on('broadcast users srv',(user) => {
        var b = new Branch();
        var n = new NVNode(b);n.image_path = user._node._image_path;
        var u = new User(user._mail,user._id,n);
        u.socket = this.s.id;
        this.users.push(u);
        this.s.broadcast.emit('broadcast users clt',u)
    });

to

    this.io.on('connection', (sock: SocketIO.Socket) => {
        var date = new Date();
        console.log(date+' : a user connected '+sock.id);
        this.s = sock;

        this.s.on('broadcast users srv',(user) => {
            var b = new Branch();
            var n = new NVNode(b);n.image_path = user._node._image_path;
            var u = new User(user._mail,user._id,n);
            u.socket = this.s.id;
            this.users.push(u);
            this.s.broadcast.emit('broadcast users clt',u)
        });
    });

might do the trick.

Upvotes: 2

Related Questions