-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
531 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { v4 as uuid, v4 } from "uuid" | ||
import WebSocket from "ws"; | ||
|
||
import { jwtValidate } from "../util/jwtValidate" | ||
import { randomMatch } from "../matchFinder"; | ||
|
||
const wsPort = process.env.WS_PORT || 3003 | ||
|
||
const wsServer = new WebSocket.Server({ port: wsPort as number }, () => { | ||
console.log(`ws Server is running on http://localhost:${wsPort}`) | ||
}) | ||
|
||
const handleWs = () => { | ||
wsServer.on("connection", async (connection, request) => { | ||
console.log(`Client connected`) | ||
|
||
if (!jwtValidate(request.headers.authorization || "")) { | ||
//cannot validate jwt | ||
console.log("cannot validate jwt for a user") | ||
connection.send("cannot validate your jwt") | ||
connection.close() | ||
return | ||
} | ||
|
||
connection.on("error", (error: Error) => { | ||
console.log("Connection Error: " + error.message) | ||
}) | ||
|
||
connection.on("close", () => { | ||
console.log("Client connection closed"); | ||
}) | ||
|
||
connection.on("message", async (message: string) => { | ||
const { userId } = JSON.parse(message) | ||
let findedUser = await randomMatch(userId) | ||
|
||
|
||
/** await setInterval(async () => { | ||
randomMatch(userId).then((result) => { | ||
console.log(`result is ${result}`) | ||
if (result instanceof String) { | ||
findedUser = result | ||
} | ||
}) | ||
}, 5000) */ | ||
|
||
|
||
console.log(`finded user is ${findedUser}`) | ||
}) | ||
}) | ||
|
||
wsServer.on("error", (error: Error) => { | ||
console.log("WebSocket server error: " + error.message) | ||
}) | ||
} | ||
|
||
export default handleWs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { createClient } from 'redis' | ||
|
||
const redisClient = createClient({ | ||
password: process.env.REDIS_PASS, | ||
socket: { | ||
host: process.env.REDIS_URL, | ||
port: 19908 | ||
} | ||
}) | ||
|
||
redisClient.on('error', err => console.log('Redis Client Error', err)) | ||
|
||
async function connectRedis() { | ||
try { | ||
await redisClient.connect() | ||
console.log("connected to redis") | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
} | ||
|
||
export { connectRedis, redisClient } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { redisClient } from "./data/redisConnector" | ||
|
||
async function randomMatch(userId: string) { | ||
const cardinality = await redisClient.sCard('user-pool') | ||
await redisClient.expire(`user-pool${userId}`, 3600) | ||
let poolUser | ||
|
||
if (cardinality < 1) { | ||
//if no user in pool, add user | ||
await redisClient.sAdd('user-pool', userId) | ||
} else { | ||
//if someone waiting in pool, match | ||
poolUser = await redisClient.sPop('user-pool') | ||
} | ||
|
||
return poolUser | ||
} | ||
|
||
function locationBasedMatch(userId: string) { } | ||
|
||
export { | ||
locationBasedMatch, randomMatch | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html lang="tr"> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>preview</title> | ||
</head> | ||
|
||
<body> | ||
<video class="video-player" id="user-1" autoplay playsinline ></video> | ||
<video class="video-player" id="user-2" autoplay playsinline ></video> | ||
<script src="./index.js"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
let localStream | ||
let remoteStream | ||
|
||
let init = async () => { | ||
localStream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true }) | ||
document.getElementById('user-1').srcObject = localStream | ||
} | ||
init() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#videos { | ||
display: grid; | ||
grid-template-columns: 1fr 1fr; | ||
gap: 2em; | ||
} | ||
|
||
.video-player { | ||
background-color: black; | ||
width: 100%; | ||
height: 300px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* @constant ERR -> on error | ||
* @constant RAND_FAIL -> across yourself | ||
* @constant NOT_ENOUGH_USER -> when there are not enough people in the pool | ||
*/ | ||
|
||
enum FinderStates { | ||
ERR = "0", | ||
RAND_FAIL = "1", | ||
NOTE_NOUGH_USER = "2" | ||
} | ||
export default FinderStates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import jwt, { JwtPayload } from 'jsonwebtoken'; | ||
|
||
function jwtValidate(token: string) { | ||
let result = false | ||
jwt.verify(token, process.env.JWT_SECRET_KEY as string, (error, decoded) => { | ||
if (error) { | ||
result = false | ||
} else { | ||
result = true; | ||
} | ||
}); | ||
return result | ||
} | ||
|
||
|
||
|
||
export { jwtValidate } |