nodejs 简单聊天应用

2015-08-26 • Node.JS2 Comments

/**
 * @desc 构建socket服务器
 * @anthor nocilol
 * @date 2015.8.25
 */
var http = require('http'), socketio = require('socket.io'), static_node = require('node-static');
var file = new static_node.Server('./public');
var server = http.createServer(init).listen(30001);

socketio.listen(server).on('connection', broadcast);
/**
 * @desc 首页
 * @param request
 * @param respone
 */
function init(request, respone){
    request.addListener('end', function(){
        file.serve(request, respone);
    }).resume();
}

/**
 * @desc 广播消息
 * @param socket
 */
function broadcast(socket){
    socket.on('message', function(msg){
        socket.broadcast.emit('message', msg);
    });
}

已有 2 条评论

粤公网安备 44011102000950号