var express = require('express')
var app = express()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var path = require('path')
// app.use(express.static(path.join(__dirname + '/')))
// app.use(express.static('.'))
// app.use(express.static('./'))
app.use('/static', express.static('./'))
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html')
})
var userID = []
var number = 0
var PORT = 3000
io.on('connection', function (socket) {
var id = new Date().getTime()
socket.emit('s_id', id)
socket.on('c_id', function (c_id) {
if (c_id === id) {
userID.push(id)
number++
console.log('用户: ' + id + '已连接,当前用户数量: ' + number)
}
})
socket.on('c_msg', function (msg) {
// 全网广播
io.emit('s_msg', msg)
})
socket.on('disconnect', function (msg) {
number--
console.log('用户: ' + id + '已退出')
})
})
http.listen(PORT, function (err) {
if (err) {
console.log(err)
return
}
console.log('listening at http://localhost:' + PORT + '\n')
})