-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.js
executable file
·44 lines (36 loc) · 1.3 KB
/
server.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
38
39
40
41
42
43
44
//expree를 사용하여 http서버 생성
var express = require('express');
var app = express();
var http = require('http').Server(app);
//생성된 sever를 socket.io 서버로 업그레이드
var io = require('socket.io')(http);
//모든 request는 cleint를 responce하도록 라우터임
app.get('/',function(req, res){
res.sendFile(__dirname + '/FrontEnd/chattingPage.html');
// res.sendFile(__dirname + '/FrontEnd/draw.html');
});
app.get('/draw',function(req, res){
res.sendFile(__dirname + '/FrontEnd/draw.html');
});
//io.on(EVENT,함수)는 서버에 전달된 EVENT를 인식하여 함수를 실행시키는 event listener
var count = 1;
io.on('connection', function(socket){
console.log('user connected: ', socket.id);
var name = "user" + count++;
io.to(socket.id).emit('change name', name);
// var enter = name+ '님이 입장하셨습니다.<br/>';
// io.emit('enter user', enter);
socket.on('disconnect', function(){
console.log('user disconnect : '+ socket.id);
// io.emit('disconnected message', name);
});
socket.on('send message', function(name,text){
var msg = name + ': ' + text+ '<br/>';
io.emit('receive message', msg);
console.log(msg);
});
});
http.listen(3000, function(){
console.log('server on!');
});
app.use(express.static(__dirname +'/public'));