Every time my socket io client connects to the server, it seems to happen twice. You can see what I mean by looking at this screenshot of the double connection.
This project is built in Angular 2, so I'm coding in TypeScript:
class Server {
private app = express();
private httpServer = http.createServer(this.app);
private io = sio.listen(this.httpServer);
private users = Array<User>();
constructor() {
// routing to index.html
this.app.get('/',(req, res) => {
res.sendFile(__dirname + '/index.html');
});
// adding dependencies
this.app.use(express.static(__dirname + '/');
// connection & disconnection
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 adjust my code like this:
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() {
// routing to index.html
this.app.get('/',(req, res) => {
res.sendFile(__dirname + '/index.html');
});
// adding dependencies
this.app.use(express.static(__dirname + '/');
// connection & disconnection
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)
});
}
}
However, when I attempted this modification, an error occurred: server error message
The error indicates that the value is not initialized and appears as "undefined."
I am quite unsure about how to proceed with this issue. Any suggestions or ideas would be greatly appreciated!