Skip to content

Commit

Permalink
Merge pull request #312 from connect-foundation/live-server
Browse files Browse the repository at this point in the history
1.3.0 배포!
  • Loading branch information
YukJiSoo authored Dec 20, 2019
2 parents d33c0b3 + 7bab6ac commit b5a7762
Show file tree
Hide file tree
Showing 5 changed files with 538 additions and 0 deletions.
138 changes: 138 additions & 0 deletions live-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Created by https://www.gitignore.io/api/intellij,webstorm
# Edit at https://www.gitignore.io/?templates=intellij,webstorm

# Dependency directories
node_modules/

# env
env.tar

### Intellij & WebStorm ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/
.idea/**/workspace.xml
.idea/**/tasks.xmlgit
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# Sonarlint plugin
.idea/**/sonarlint/

# SonarQube Plugin
.idea/**/sonarIssues.xml

# Markdown Navigator plugin
.idea/**/markdown-navigator.xml
.idea/**/markdown-navigator/

### VSCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace

# Created by https://www.gitignore.io/api/macos
# Edit at https://www.gitignore.io/?templates=macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.gitignore.io/api/macos%
10 changes: 10 additions & 0 deletions live-server/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 80,
"tabWidth": 4,
"useTabs": true,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": true,
"arrowParens": "avoid"
}
87 changes: 87 additions & 0 deletions live-server/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const io = require('socket.io')();
const PORT = 3040;

const rooms = {};

io.on('connection', socket => {
const isNotHost = (host, username) => host.username !== username;
const isFirstVisit = (participants, username) => indexOfUser(participants, username) === -1;
const indexOfUser = (participants, username) => participants.findIndex(participant => participant.username === username);

const handleCreateRoom = ({ projectId, user, project }) => {
socket.user = user;
socket.room = projectId;
socket.join(projectId);

if (rooms[projectId]) {
// 방에 참여하는 경우
const { host, participants } = rooms[socket.room];
const {
user: { username }
} = socket;

// 호스트 혹은 이미 접속한 사람이 재접속했을 때 참가자에 추가되는 것을 방지
if (isNotHost(host, username) && isFirstVisit(participants, username)) participants.push(socket.user);

// 본인에게 알리기
socket.emit('alreadyExistRoom', { host, project });
} else {
// 방을 생성하는 경우
rooms[projectId] = {
host: user,
project,
participants: []
};

// 본인에게 알리기
socket.emit('successCreatedRoom', { project });
}

const { participants } = rooms[socket.room];

// 해당 방에 있는 사람들에게 알리기
io.in(socket.room).emit('joinUser', { participants });
};

const handleDisconnect = () => {
if (!rooms[socket.room]) return;
const { host, participants } = rooms[socket.room];
const {
user: { username }
} = socket;
socket.leave(socket.room);

if (host.username === username) {
// 호스트가 연결이 끊긴 경우
rooms[socket.room] = null;
io.sockets.in(socket.room).emit('close');
} else {
// 게스트가 연결이 끊긴 경우
participants.splice(indexOfUser(participants, username), 1);
io.in(socket.room).emit('leaveUser', { participants });
}
};

const handleCloseSocket = () => {
// 호스트가 라이브를 중지한 경우
rooms[socket.room] = null;
io.sockets.in(socket.room).emit('close');
};

const handleOnChange = (filePath, operation) => {
io.in(socket.room).emit('change', socket.id, filePath, operation);
};

const handleOnMoveCursor = (filePath, position) => {
socket.broadcast.emit('moveCursor', socket.user.username, filePath, position);
};

socket.emit('connected');
socket.on('createRoom', handleCreateRoom);
socket.on('disconnect', handleDisconnect);
socket.on('close', handleCloseSocket);
socket.on('change', handleOnChange);
socket.on('moveCursor', handleOnMoveCursor);
});

io.listen(PORT);
16 changes: 16 additions & 0 deletions live-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "live-server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"diff": "^4.0.1",
"socket.io": "^2.3.0"
}
}
Loading

0 comments on commit b5a7762

Please sign in to comment.