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-26 21:24:18 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
13077f781d1f697d9b0d987320461da631087d5f
13077f78
1 parent
32316719
MessageResponse에 응답 결과 데이터를 담을 수 있도록 변경
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
24 additions
and
18 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/room/Room.ts
server/message/MessageHandlerRegistry.ts
View file @
13077f7
...
...
@@ -26,10 +26,13 @@ export class MessageHandlerRegistry {
);
}
private
static
registerHandler
<
T
extends
Message
>
(
private
static
registerHandler
<
T
extends
Message
,
S
extends
Message
|
undefined
>
(
connection
:
Connection
,
typeName
:
string
,
handler
:
(
connection
:
Connection
,
message
:
T
)
=>
MessageResponse
handler
:
(
connection
:
Connection
,
message
:
T
)
=>
MessageResponse
<
S
>
)
{
connection
.
socket
.
on
(
typeName
,
(
message
:
T
,
callback
:
Function
)
=>
{
const
response
=
handler
(
connection
,
message
);
...
...
@@ -37,10 +40,13 @@ export class MessageHandlerRegistry {
});
}
private
static
registerHandlerAuthed
<
T
extends
Message
>
(
private
static
registerHandlerAuthed
<
T
extends
Message
,
S
extends
Message
|
undefined
>
(
connection
:
Connection
,
typeName
:
string
,
handler
:
(
user
:
User
,
message
:
T
)
=>
MessageResponse
handler
:
(
user
:
User
,
message
:
T
)
=>
MessageResponse
<
S
>
)
{
connection
.
socket
.
on
(
typeName
,
(
message
:
T
,
callback
:
Function
)
=>
{
if
(
connection
.
user
!==
undefined
)
{
...
...
server/message/handler/LoginHandler.ts
View file @
13077f7
...
...
@@ -6,7 +6,7 @@ import { LoginMessage, MessageResponse } from "../types";
export
function
loginHandler
(
connection
:
Connection
,
message
:
LoginMessage
):
MessageResponse
{
):
MessageResponse
<
undefined
>
{
connection
.
user
=
new
User
(
message
.
username
,
connection
);
console
.
log
(
`User
${
message
.
username
}
has logged in!`
);
...
...
server/message/handler/roomChatHandler.ts
View file @
13077f7
...
...
@@ -6,7 +6,7 @@ import { MessageResponse, RoomChatMessage, RoomJoinMessage } from "../types";
export
function
roomChatHandler
(
user
:
User
,
message
:
RoomChatMessage
):
MessageResponse
{
):
MessageResponse
<
undefined
>
{
user
.
room
?.
sendChat
(
user
,
message
.
message
);
return
{
ok
:
true
};
}
...
...
server/message/handler/roomJoinHandler.ts
View file @
13077f7
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
MessageResponse
,
RoomJoinMessage
}
from
"../types"
;
import
{
MessageResponse
,
Room
InfoMessage
,
Room
JoinMessage
}
from
"../types"
;
export
function
roomJoinHandler
(
user
:
User
,
message
:
RoomJoinMessage
):
MessageResponse
{
):
MessageResponse
<
RoomInfoMessage
>
{
const
room
=
RoomManager
.
instance
().
get
(
message
.
uuid
);
if
(
room
!==
undefined
)
{
room
.
connect
(
user
);
return
{
ok
:
tru
e
};
const
roomInfoMessage
=
room
.
connect
(
user
);
return
{
ok
:
roomInfoMessage
!==
undefined
,
result
:
roomInfoMessag
e
};
}
return
{
ok
:
false
};
}
...
...
server/message/handler/roomLeaveHandler.ts
View file @
13077f7
...
...
@@ -6,7 +6,7 @@ import { MessageResponse, RoomLeaveMessage } from "../types";
export
function
roomLeaveHandler
(
user
:
User
,
message
:
RoomLeaveMessage
):
MessageResponse
{
):
MessageResponse
<
undefined
>
{
user
.
room
?.
disconnect
(
user
);
return
{
ok
:
true
};
}
...
...
server/message/types.ts
View file @
13077f7
...
...
@@ -10,9 +10,10 @@ export interface Message {
* @param ok 요청의 성공 여부입니다.
* @param reason 요청 실패 사유입니다. 필요한 경우에만 포함됩니다.
*/
export
interface
MessageResponse
{
export
interface
MessageResponse
<
T
>
{
ok
:
boolean
;
reason
?:
string
;
result
?:
T
;
}
/**
...
...
@@ -87,7 +88,6 @@ 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"
;
...
...
server/room/Room.ts
View file @
13077f7
...
...
@@ -26,23 +26,23 @@ export class Room {
this
.
maxUsers
=
maxUsers
;
}
public
connect
(
user
:
User
):
voi
d
{
public
connect
(
user
:
User
):
RoomInfoMessage
|
undefine
d
{
if
(
this
.
users
.
includes
(
user
)
||
this
.
users
.
length
>=
this
.
maxUsers
)
{
return
;
return
undefined
;
}
this
.
broadcast
(
new
RoomUserUpdateMessage
(
"added"
,
user
.
getData
()));
this
.
users
.
push
(
user
);
user
.
room
=
this
;
// TODO: 더 나은 관리
this
.
broadcast
(
new
RoomUserUpdateMessage
(
"added"
,
user
.
getData
()));
var
users
:
UserData
[]
=
[];
this
.
users
.
forEach
((
u
)
=>
{
if
(
user
!==
u
)
{
users
.
push
(
u
.
getData
());
}
});
user
.
connection
.
send
(
new
RoomInfoMessage
(
users
)
);
return
new
RoomInfoMessage
(
users
);
}
public
disconnect
(
user
:
User
):
void
{
...
...
Please
register
or
login
to post a comment