Skip to content

Commit b5a7762

Browse files
authored
Merge pull request #312 from connect-foundation/live-server
1.3.0 배포!
2 parents d33c0b3 + 7bab6ac commit b5a7762

File tree

5 files changed

+538
-0
lines changed

5 files changed

+538
-0
lines changed

live-server/.gitignore

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Created by https://www.gitignore.io/api/intellij,webstorm
2+
# Edit at https://www.gitignore.io/?templates=intellij,webstorm
3+
4+
# Dependency directories
5+
node_modules/
6+
7+
# env
8+
env.tar
9+
10+
### Intellij & WebStorm ###
11+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
12+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
13+
14+
# User-specific stuff
15+
.idea/
16+
.idea/**/workspace.xml
17+
.idea/**/tasks.xmlgit
18+
.idea/**/usage.statistics.xml
19+
.idea/**/dictionaries
20+
.idea/**/shelf
21+
22+
# Generated files
23+
.idea/**/contentModel.xml
24+
25+
# Sensitive or high-churn files
26+
.idea/**/dataSources/
27+
.idea/**/dataSources.ids
28+
.idea/**/dataSources.local.xml
29+
.idea/**/sqlDataSources.xml
30+
.idea/**/dynamic.xml
31+
.idea/**/uiDesigner.xml
32+
.idea/**/dbnavigator.xml
33+
34+
# Gradle
35+
.idea/**/gradle.xml
36+
.idea/**/libraries
37+
38+
# Gradle and Maven with auto-import
39+
# When using Gradle or Maven with auto-import, you should exclude module files,
40+
# since they will be recreated, and may cause churn. Uncomment if using
41+
# auto-import.
42+
# .idea/modules.xml
43+
# .idea/*.iml
44+
# .idea/modules
45+
# *.iml
46+
# *.ipr
47+
48+
# CMake
49+
cmake-build-*/
50+
51+
# Mongo Explorer plugin
52+
.idea/**/mongoSettings.xml
53+
54+
# File-based project format
55+
*.iws
56+
57+
# IntelliJ
58+
out/
59+
60+
# mpeltonen/sbt-idea plugin
61+
.idea_modules/
62+
63+
# JIRA plugin
64+
atlassian-ide-plugin.xml
65+
66+
# Cursive Clojure plugin
67+
.idea/replstate.xml
68+
69+
# Crashlytics plugin (for Android Studio and IntelliJ)
70+
com_crashlytics_export_strings.xml
71+
crashlytics.properties
72+
crashlytics-build.properties
73+
fabric.properties
74+
75+
# Editor-based Rest Client
76+
.idea/httpRequests
77+
78+
# Android studio 3.1+ serialized cache file
79+
.idea/caches/build_file_checksums.ser
80+
81+
### Intellij Patch ###
82+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
83+
84+
# *.iml
85+
# modules.xml
86+
# .idea/misc.xml
87+
# *.ipr
88+
89+
# Sonarlint plugin
90+
.idea/**/sonarlint/
91+
92+
# SonarQube Plugin
93+
.idea/**/sonarIssues.xml
94+
95+
# Markdown Navigator plugin
96+
.idea/**/markdown-navigator.xml
97+
.idea/**/markdown-navigator/
98+
99+
### VSCode ###
100+
.vscode/*
101+
!.vscode/settings.json
102+
!.vscode/tasks.json
103+
!.vscode/launch.json
104+
!.vscode/extensions.json
105+
*.code-workspace
106+
107+
# Created by https://www.gitignore.io/api/macos
108+
# Edit at https://www.gitignore.io/?templates=macos
109+
110+
### macOS ###
111+
# General
112+
.DS_Store
113+
.AppleDouble
114+
.LSOverride
115+
116+
# Icon must end with two \r
117+
Icon
118+
119+
# Thumbnails
120+
._*
121+
122+
# Files that might appear in the root of a volume
123+
.DocumentRevisions-V100
124+
.fseventsd
125+
.Spotlight-V100
126+
.TemporaryItems
127+
.Trashes
128+
.VolumeIcon.icns
129+
.com.apple.timemachine.donotpresent
130+
131+
# Directories potentially created on remote AFP share
132+
.AppleDB
133+
.AppleDesktop
134+
Network Trash Folder
135+
Temporary Items
136+
.apdisk
137+
138+
# End of https://www.gitignore.io/api/macos%

live-server/.prettierrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"printWidth": 80,
3+
"tabWidth": 4,
4+
"useTabs": true,
5+
"semi": true,
6+
"singleQuote": true,
7+
"trailingComma": "none",
8+
"bracketSpacing": true,
9+
"arrowParens": "avoid"
10+
}

live-server/app.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const io = require('socket.io')();
2+
const PORT = 3040;
3+
4+
const rooms = {};
5+
6+
io.on('connection', socket => {
7+
const isNotHost = (host, username) => host.username !== username;
8+
const isFirstVisit = (participants, username) => indexOfUser(participants, username) === -1;
9+
const indexOfUser = (participants, username) => participants.findIndex(participant => participant.username === username);
10+
11+
const handleCreateRoom = ({ projectId, user, project }) => {
12+
socket.user = user;
13+
socket.room = projectId;
14+
socket.join(projectId);
15+
16+
if (rooms[projectId]) {
17+
// 방에 참여하는 경우
18+
const { host, participants } = rooms[socket.room];
19+
const {
20+
user: { username }
21+
} = socket;
22+
23+
// 호스트 혹은 이미 접속한 사람이 재접속했을 때 참가자에 추가되는 것을 방지
24+
if (isNotHost(host, username) && isFirstVisit(participants, username)) participants.push(socket.user);
25+
26+
// 본인에게 알리기
27+
socket.emit('alreadyExistRoom', { host, project });
28+
} else {
29+
// 방을 생성하는 경우
30+
rooms[projectId] = {
31+
host: user,
32+
project,
33+
participants: []
34+
};
35+
36+
// 본인에게 알리기
37+
socket.emit('successCreatedRoom', { project });
38+
}
39+
40+
const { participants } = rooms[socket.room];
41+
42+
// 해당 방에 있는 사람들에게 알리기
43+
io.in(socket.room).emit('joinUser', { participants });
44+
};
45+
46+
const handleDisconnect = () => {
47+
if (!rooms[socket.room]) return;
48+
const { host, participants } = rooms[socket.room];
49+
const {
50+
user: { username }
51+
} = socket;
52+
socket.leave(socket.room);
53+
54+
if (host.username === username) {
55+
// 호스트가 연결이 끊긴 경우
56+
rooms[socket.room] = null;
57+
io.sockets.in(socket.room).emit('close');
58+
} else {
59+
// 게스트가 연결이 끊긴 경우
60+
participants.splice(indexOfUser(participants, username), 1);
61+
io.in(socket.room).emit('leaveUser', { participants });
62+
}
63+
};
64+
65+
const handleCloseSocket = () => {
66+
// 호스트가 라이브를 중지한 경우
67+
rooms[socket.room] = null;
68+
io.sockets.in(socket.room).emit('close');
69+
};
70+
71+
const handleOnChange = (filePath, operation) => {
72+
io.in(socket.room).emit('change', socket.id, filePath, operation);
73+
};
74+
75+
const handleOnMoveCursor = (filePath, position) => {
76+
socket.broadcast.emit('moveCursor', socket.user.username, filePath, position);
77+
};
78+
79+
socket.emit('connected');
80+
socket.on('createRoom', handleCreateRoom);
81+
socket.on('disconnect', handleDisconnect);
82+
socket.on('close', handleCloseSocket);
83+
socket.on('change', handleOnChange);
84+
socket.on('moveCursor', handleOnMoveCursor);
85+
});
86+
87+
io.listen(PORT);

live-server/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "live-server",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "nodemon app.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"diff": "^4.0.1",
14+
"socket.io": "^2.3.0"
15+
}
16+
}

0 commit comments

Comments
 (0)