-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
37 lines (28 loc) · 1.01 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var path = require('path');
var express = require('express');
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'node_modules')));
app.get('/', function(req, res){
res.sendFile(path.join(__dirname, 'index.html'));
});
io.on('connection', function(socket){
console.log(socket.id);
// socket.name = socket.remoteAddress + ':' + socket.remotePort;
// console.log('Hi ' + socket.name + '!\n');
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
socket.on('chat message', function(msg){
console.log('message: ' + msg);
// console.log(socket.id);
io.emit('chat message', socket.id.substr(0,4)+":"+msg);
});
});
app.set('port', process.env.PORT || 3010);
var server = http.listen(app.get('port'), function() {
console.log('start at port:' + server.address().port);
});