오늘의 목표
더보기
✔️ 최종프로젝트 진행
⏱️ 오늘의 일정
최종 프로젝트 진행
📜 최종 프로젝트 진행
아무래도 이번 프로젝트는 최종프로젝트이니까 모든 팀원들이 서버에 작성한 모든 코드를 이해해야한다는 것을 팀원 모두 공유 했기 때문에 Live Share를 통해 같이 코드를 구현하도록 했다.
스켈레톤 코드의 내용은 회원가입, 로그인, 패킷을 주고 받는 과정, DB 연결 사용까지 구현하고,이후 지정되어 있는 패킷을 팀원들 각자 진행하기로 했다.
기존 강의로 배웠던 서버는 기본적으로 함수로 listen을 하고, connect 연결을 담당했는데나의 주장으로 클래스로 구성하기로 했다.
class Server {
private static gInstance : Server | null = null;
private protoMessages: { [key: string]: any } = {};
private server: net.Server;
private constructor() {
this.server = net.createServer(this.clientConnection);
}
static getInstance(){
if(Server.gInstance === null)
{
Server.gInstance = new Server();
}
return Server.gInstance;
}
clientConnection(socket : net.Socket)
{
const customSocket = socket as CustomSocket;
console.log(`Client connected from: ${socket.remoteAddress}:${socket.remotePort}`);
customSocket.buffer = Buffer.alloc(0);
customSocket.on('data', onData(customSocket));
customSocket.on('end', onEnd(customSocket));
customSocket.on('error', onError(customSocket));
}
listen () {
this.server.listen(config.server.port, config.server.host, () => {
console.log(`서버가 ${config.server.host}:${config.server.port}에서 실행 중입니다.`);
console.log(this.server.address());
});
}
start()
{
this.listen();
}
}
export default Server;
server라는 클래스를 생성해 start() 함수를 호출함으로써 서버를 시작한다.
import Server from './class/server.js';
function main() {
Server.getInstance().start();
}
main();
main.ts에서 server.start()를 호출하는 모습
import net from "net";
class Client {
private clientSocket: any;
constructor() {
this.clientSocket = new net.Socket();
}
connect(){
this.clientSocket.connect(5555, "127.0.0.1", async () => {
console.log(`"127.0.0.1" : 5555 서버와 연결`);
this.clientSocket.on('data', (data:Buffer) => {
});
});
}
}
const dummyClient = new Client();
dummyClient.connect();
간단하게 서버에 접속할 수 있는 client를 만들어 테스트
'내일배움캠프' 카테고리의 다른 글
[내일배움캠프][TIL] 최종프로젝트 진행 ( 기본틀 완성 ) (0) | 2024.11.20 |
---|---|
[내일배움캠프][TIL] 최종프로젝트 진행 ( 기본틀 부분완성 ) (0) | 2024.11.18 |
[내일배움캠프][TIL] - 최종프로젝트 진행 (0) | 2024.11.13 |
[내일배움캠프][TIL] 최종 프로젝트 시작 (0) | 2024.11.12 |
[내일배움캠프][TIL] 팀 프로젝트 진행 (0) | 2024.11.07 |