Skip to content

Commit

Permalink
WebRTC 수업 code 자료
Browse files Browse the repository at this point in the history
  • Loading branch information
kdpark-phd committed Nov 29, 2022
1 parent a4e0918 commit 1c6bcef
Show file tree
Hide file tree
Showing 12 changed files with 2,511 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 kw-ic-web

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
41 changes: 41 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var createError = require("http-errors");
var express = require("express");
var path = require("path");
var cookieParser = require("cookie-parser");
var logger = require("morgan");

var indexRouter = require("./routes/index");
var usersRouter = require("./routes/users");

var app = express();

// view engine setup
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "jade");

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

app.use("/", indexRouter);
app.use("/users", usersRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
next(createError(404));
});

// error handler
app.use(function (err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};

// render the error page
res.status(err.status || 500);
res.send("something wrong!");
});

module.exports = app;
88 changes: 88 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require("../app");
var debug = require("debug")("express-skeleton-html:server");
var http = require("http");
const socketHandler = require("../modules/socketHandler");

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || "3000");
app.set("port", port);

/**
* Create HTTP server.
*/

const server = http.createServer(app);
socketHandler(server);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on("error", onError);
server.on("listening", onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== "listen") {
throw error;
}

var bind = typeof port === "string" ? "Pipe " + port : "Port " + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
49 changes: 49 additions & 0 deletions modules/socketHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const { Server } = require("socket.io");

const socketHandler = (server) => {
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST"],
},
});

let user = {};

io.on("connection", (socket) => {
// 접속 시 서버에서 실행되는 코드
const req = socket.request;
const socket_id = socket.id;
const client_ip =
req.headers["x-forwarded-for"] || req.connection.remoteAddress;
console.log("connection!");
console.log("socket ID : ", socket_id);
console.log("client IP : ", client_ip);

user[socket.id] = { nickname: "users nickname", point: 0 };

socket.on("disconnect", () => {
// 사전 정의 된 callback (disconnect, error)
//console.log(socket.id, " client disconnected");
delete user[socket.id];
});

socket.on("join_room", (roomName) => {
socket.join(roomName);
socket.to(roomName).emit("welcome");
});

socket.on("offer", (offer, roomName) => {
socket.to(roomName).emit("offer", offer);
});

socket.on("answer", (answer, roomName) => {
socket.to(roomName).emit("answer", answer);
});

socket.on("ice", (ice, roomName) => {
socket.to(roomName).emit("ice", ice);
});
});
};
module.exports = socketHandler;
Loading

0 comments on commit 1c6bcef

Please sign in to comment.