//socket.js
var socketio = require('socket.io');
global.warn;
exports.listen = function(server) {\
    //nameSpace
  global.warn = socketio(server).of('/warnTips');
  global.warn.on('connection', function(socket) {
    //当前连接客户端对应的实例
    socket.on('hgk', function(data) {
      socket.broadcast.emit('warn', data);
    })
    socket.on('warn', function(data) {
      console.log('接受到客户端消息了:' + data);
    })
  });
}
//入口js
var WarnSocket = require('socket.js');
var http = require('http');
var app = require('./app');
var server = http.createServer(app);
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = http.createServer(app);
WarnSocket.listen(server);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function onError(err){
    ...省略
}
function onListening(){
    ...省略
}

clipboard.png


小脑fu
237 声望9 粉丝