Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
강동현
2021-05-25 16:24:17 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
5b32c036181722a154706920999e691f61e9de8a
5b32c036
1 parent
0bc79602
MessageResponse 구현
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
45 additions
and
15 deletions
server/message/MessageHandlerRegistry.ts
server/message/handler/LoginHandler.ts
server/message/handler/roomChatHandler.ts
server/message/handler/roomJoinHandler.ts
server/message/handler/roomLeaveHandler.ts
server/message/types.ts
server/message/MessageHandlerRegistry.ts
View file @
5b32c03
...
...
@@ -4,7 +4,7 @@ import { loginHandler } from "./handler/loginHandler";
import
{
roomChatHandler
}
from
"./handler/roomChatHandler"
;
import
{
roomJoinHandler
}
from
"./handler/roomJoinHandler"
;
import
{
roomLeaveHandler
}
from
"./handler/roomLeaveHandler"
;
import
{
Message
,
MessageType
}
from
"./types"
;
import
{
Message
,
Message
Response
,
Message
Type
}
from
"./types"
;
export
class
MessageHandlerRegistry
{
static
registerHandlers
(
connection
:
Connection
)
{
...
...
@@ -29,21 +29,25 @@ export class MessageHandlerRegistry {
private
static
registerHandler
<
T
extends
Message
>
(
connection
:
Connection
,
typeName
:
string
,
handler
:
(
connection
:
Connection
,
message
:
T
)
=>
void
handler
:
(
connection
:
Connection
,
message
:
T
)
=>
MessageResponse
)
{
connection
.
socket
.
on
(
typeName
,
(
message
:
T
)
=>
{
handler
(
connection
,
message
);
connection
.
socket
.
on
(
typeName
,
(
message
:
T
,
callback
:
Function
)
=>
{
const
response
=
handler
(
connection
,
message
);
callback
(
response
);
});
}
private
static
registerHandlerAuthed
<
T
extends
Message
>
(
connection
:
Connection
,
typeName
:
string
,
handler
:
(
user
:
User
,
message
:
T
)
=>
void
handler
:
(
user
:
User
,
message
:
T
)
=>
MessageResponse
)
{
connection
.
socket
.
on
(
typeName
,
(
message
:
T
)
=>
{
connection
.
socket
.
on
(
typeName
,
(
message
:
T
,
callback
:
Function
)
=>
{
if
(
connection
.
user
!==
undefined
)
{
handler
(
connection
.
user
,
message
);
const
response
=
handler
(
connection
.
user
,
message
);
callback
(
response
);
}
else
{
callback
({
ok
:
false
});
}
});
}
...
...
server/message/handler/LoginHandler.ts
View file @
5b32c03
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
LoginMessage
}
from
"../types"
;
import
{
LoginMessage
,
MessageResponse
}
from
"../types"
;
export
function
loginHandler
(
connection
:
Connection
,
message
:
LoginMessage
):
void
{
):
MessageResponse
{
connection
.
user
=
new
User
(
message
.
username
,
connection
);
console
.
log
(
`User
${
message
.
username
}
has logged in!`
);
RoomManager
.
instance
().
sendList
(
connection
);
return
{
ok
:
true
};
}
...
...
server/message/handler/roomChatHandler.ts
View file @
5b32c03
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
RoomChatMessage
,
RoomJoinMessage
}
from
"../types"
;
import
{
MessageResponse
,
RoomChatMessage
,
RoomJoinMessage
}
from
"../types"
;
export
function
roomChatHandler
(
user
:
User
,
message
:
RoomChatMessage
):
void
{
export
function
roomChatHandler
(
user
:
User
,
message
:
RoomChatMessage
):
MessageResponse
{
user
.
room
?.
sendChat
(
user
,
message
.
message
);
return
{
ok
:
true
};
}
...
...
server/message/handler/roomJoinHandler.ts
View file @
5b32c03
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
RoomJoinMessage
}
from
"../types"
;
import
{
MessageResponse
,
RoomJoinMessage
}
from
"../types"
;
export
function
roomJoinHandler
(
user
:
User
,
message
:
RoomJoinMessage
):
void
{
export
function
roomJoinHandler
(
user
:
User
,
message
:
RoomJoinMessage
):
MessageResponse
{
const
room
=
RoomManager
.
instance
().
get
(
message
.
uuid
);
if
(
room
!==
undefined
)
{
room
.
connect
(
user
);
return
{
ok
:
true
};
}
return
{
ok
:
false
};
}
...
...
server/message/handler/roomLeaveHandler.ts
View file @
5b32c03
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
RoomLeaveMessage
}
from
"../types"
;
import
{
MessageResponse
,
RoomLeaveMessage
}
from
"../types"
;
export
function
roomLeaveHandler
(
user
:
User
,
message
:
RoomLeaveMessage
):
void
{
export
function
roomLeaveHandler
(
user
:
User
,
message
:
RoomLeaveMessage
):
MessageResponse
{
user
.
room
?.
disconnect
(
user
);
return
{
ok
:
true
};
}
...
...
server/message/types.ts
View file @
5b32c03
...
...
@@ -6,6 +6,16 @@ export interface Message {
}
/**
* 서버에 Event를 보냈을 때 요청에 대한 결과를 전송받습니다.
* @param ok 요청의 성공 여부입니다.
* @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다.
*/
export
interface
MessageResponse
{
ok
:
boolean
;
reason
?:
string
;
}
/**
* 클라 -> 서버
* 로그인 정보를 서버에게 전송합니다.
*/
...
...
@@ -77,6 +87,7 @@ export class RoomChatMessage implements Message {
}
export
class
MessageType
{
static
readonly
RESPONSE
=
"response"
;
static
readonly
LOGIN
=
"login"
;
static
readonly
ROOM_LIST
=
"room_list"
;
static
readonly
ROOM_JOIN
=
"room_join"
;
...
...
Please
register
or
login
to post a comment