关于 nodejs socket.on('connection', function(){}) connection 事件无法触发 20
以下是我的代码为什么我可以向接收到‘data’事件却接收不到‘connection’事件求大神们解救varnet=require('net');varserver=net...
以下是我的代码
为什么我可以向接收到‘data’事件 却接收不到‘connection’事件
求大神们解救
var net = require('net');
var server = net.createServer(function(client){
socket.on('data', function(data){
data = data.toString();
channel.emit('broadcast', id, data);
console.log("broadcast");
});
socket.on('connection', function(){
channel.emit('join', id, client);
console.log("join");
});
});
server.listen(8000); 展开
为什么我可以向接收到‘data’事件 却接收不到‘connection’事件
求大神们解救
var net = require('net');
var server = net.createServer(function(client){
socket.on('data', function(data){
data = data.toString();
channel.emit('broadcast', id, data);
console.log("broadcast");
});
socket.on('connection', function(){
channel.emit('join', id, client);
console.log("join");
});
});
server.listen(8000); 展开
2个回答
展开全部
‘connection’是server监听的事件,应该放在外一层:
var server = net.createServer(function(client){
socket.on('data', function(data){
data = data.toString();
channel.emit('broadcast', id, data);
console.log("broadcast");
});
});
server.on('connection', function(){
channel.emit('join', id, client);
console.log("join");
});
server.listen(8000);
var server = net.createServer(function(client){
socket.on('data', function(data){
data = data.toString();
channel.emit('broadcast', id, data);
console.log("broadcast");
});
});
server.on('connection', function(){
channel.emit('join', id, client);
console.log("join");
});
server.listen(8000);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询