当我的socket io客户端连接到服务器时,它会执行两次 double connection
这是一个角度2项目,所以我可以看到打字稿中的代码:
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)
});
});
}
}
&#13;
我试着像这样编码:
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)
});
}
}
&#13;
但不幸的是,它向我显示了这个错误: error server
服务器告诉该值未初始化==&gt; &#34;未定义&#34;
我真的不知道该怎么做:( 如果你有想法!
答案 0 :(得分:2)
this.s
收到this.io
事件之前, "connection"
未初始化。如果这是您正在初始化的唯一地方,则您只能在连接时订阅this.s
其他活动。
从
切换 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)
});
到
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)
});
});
可能会成功。
答案 1 :(得分:0)
您在初始化之前尝试使用s
。通过将this.s.on
调用移至this.io.on
回调,您将确保s
不是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)
});
});
}
}