Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE][Feat] #40 : websocket 연결 #206

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions backend/src/websocketServer.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import { WebSocketServer } from 'ws';

const activeConnections = {}; // token별로 연결을 관리하기 위한 객체
const clientLocations = {};

export const initializeWebSocketServer = server => {
const wss = new WebSocketServer({
server,
verifyClient: (info, done) => {
const { origin } = info;

if (origin === 'http://localhost:5173') {
done(true);
} else {
done(false, 403, 'Forbidden: Origin not allowed');
}
},
});

wss.on('error', err => {
console.error('WebSocket Server Error:', err);
});
Expand All @@ -39,28 +42,48 @@ export const initializeWebSocketServer = server => {

console.log(`Client connected with token: ${token}`);

// 새 클라이언트에게 기존 클라이언트의 위치 전송
ws.send(
JSON.stringify({
type: 'init',
clients: Object.entries(clientLocations).map(([clientToken, location]) => ({
token: clientToken,
location,
})),
}),
);

// 클라이언트로부터 메시지 받았을 때의 이벤트 처리
wss.on('message', message => {
ws.on('message', message => {
try {
const data = JSON.parse(message); // 위치 데이터 수신
if (data.latitude && data.longitude) {
// 브로드캐스트: 모든 클라이언트에게 위치 정보 전달
Object.values(activeConnections).forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({ token, ...data }));
const data = JSON.parse(message);
if (data.type === 'location' && data.location) {
clientLocations[token] = data.location;

// 모든 클라이언트에게 위치 정보 전송
Object.keys(activeConnections).forEach(otherToken => {
if (activeConnections[otherToken].readyState === ws.OPEN) {
activeConnections[otherToken].send(
JSON.stringify({
type: 'location',
token,
location: data.location,
}),
);
}
});
}
} catch (err) {
console.error('Invalid message received:', err);
console.error('Invalid message format:', err);
}
});

// 클라이언트 연결 종료 시
wss.on('close', (code, reason) => {
ws.on('close', (code, reason) => {
console.log(`Client disconnected with token: ${token}, Code: ${code}, Reason: ${reason}`);
// 연결이 종료되면 activeConnections에서 해당 token 제거
delete activeConnections[token];
delete clientLocations[token];
});
});
};
Loading