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-06-01 17:52:04 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
ab78d0bfe5bb8705c038e9b90d0d9b586a8d8ab1
ab78d0bf
1 parent
4b9d5682
핸들러 수정 및 등록
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
76 additions
and
48 deletions
server/connection/Connection.ts
server/message/MessageHandler.ts
server/message/MessageHandlerChain.ts
server/message/handler/LoginHandler.ts
server/message/handler/roomChatHandler.ts
server/message/handler/roomJoinHandler.ts
server/message/handler/roomLeaveHandler.ts
server/message/handler/roomListRequestHandler.ts
server/room/Room.ts
server/room/RoomManager.ts
server/user/User.ts
server/connection/Connection.ts
View file @
ab78d0b
...
...
@@ -17,10 +17,6 @@ export class Connection {
this
.
messageHandlerChain
=
new
MessageHandlerChain
(
this
);
}
public
get
authenticated
():
boolean
{
return
this
.
user
!==
undefined
;
}
public
send
<
T
extends
keyof
ServerOutboundMessageMap
>
(
type
:
T
,
message
:
ServerOutboundMessageMap
[
T
]
...
...
server/message/MessageHandler.ts
View file @
ab78d0b
import
{
Connection
}
from
"../connection/Connection"
;
import
{
ServerInboundMessageMap
,
ServerResponse
}
from
"../../common/index"
;
import
{
ServerInboundMessage
,
ServerInboundMessageMap
,
ServerResponse
,
}
from
"../../common/index"
;
type
ServerHandlerMap
<
T
>
=
{
[
Key
in
keyof
ServerInboundMessageMap
]?:
(
connection
:
Connection
,
message
:
Omit
<
ServerInboundMessageMap
[
Key
],
"result"
>
,
message
:
ServerInboundMessage
<
Key
>
,
scope
:
T
)
=>
ServerResponse
<
"result"
extends
keyof
ServerInboundMessageMap
[
Key
]
?
ServerInboundMessageMap
[
Key
][
"result"
]
:
undefined
>
;
)
=>
ServerResponse
<
Key
>
;
};
export
class
HandlerMap
<
T
>
{
...
...
server/message/MessageHandlerChain.ts
View file @
ab78d0b
import
{
Connection
}
from
"../connection/Connection"
;
import
{
ServerInboundMessageMap
,
ServerResponse
}
from
"../../common/index"
;
import
{
keys
}
from
"ts-transformer-keys"
;
import
{
HandlerMap
}
from
"./MessageHandler"
;
import
{
loginHandler
}
from
"./handler/loginHandler"
;
export
class
MessageHandlerChain
{
connection
:
Connection
;
handler
:
HandlerMap
<
undefined
>
;
constructor
(
connection
:
Connection
)
{
this
.
connection
=
connection
;
this
.
handler
=
new
HandlerMap
(
undefined
,
{
login
:
loginHandler
,
});
for
(
const
key
in
keys
<
ServerInboundMessageMap
>
())
{
const
type
=
key
as
keyof
ServerInboundMessageMap
;
this
.
connection
.
socket
.
on
(
key
,
(
message
:
any
,
callback
:
Function
)
=>
{
if
(
connection
?.
user
&&
connection
.
user
.
handler
.
handle
(
connection
?.
user
?.
room
&&
connection
.
user
.
room
.
handler
.
handle
(
type
,
connection
,
message
.
data
,
message
,
callback
)
)
return
;
// TODO: Add more handlers
if
(
connection
?.
user
&&
connection
.
user
.
handler
.
handle
(
type
,
connection
,
message
,
callback
)
)
return
;
this
.
handler
.
handle
(
type
,
connection
,
message
,
callback
);
});
}
}
...
...
server/message/handler/LoginHandler.ts
View file @
ab78d0b
import
{
ServerInboundMessageMap
,
ServerResponse
}
from
"../../../common"
;
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
LoginMessage
,
MessageResponse
}
from
"../types"
;
export
function
loginHandler
(
connection
:
Connection
,
message
:
LoginMessage
):
MessageResponse
<
undefined
>
{
message
:
ServerInboundMessageMap
[
"login"
],
scope
:
undefined
):
ServerResponse
<
"login"
>
{
connection
.
user
=
new
User
(
message
.
username
,
connection
);
console
.
log
(
`User
${
message
.
username
}
has logged in!`
);
...
...
server/message/handler/roomChatHandler.ts
View file @
ab78d0b
import
{
ServerInboundMessage
,
ServerResponse
}
from
"../../../common"
;
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
Room
}
from
"../../room/Room"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
MessageResponse
,
RoomChatMessage
,
RoomJoinMessage
}
from
"../types"
;
export
function
roomChatHandler
(
user
:
User
,
message
:
RoomChatMessage
):
MessageResponse
<
undefined
>
{
user
.
room
?.
sendChat
(
user
,
message
.
message
);
export
function
chatHandler
(
connection
:
Connection
,
message
:
ServerInboundMessage
<
"chat"
>
,
scope
:
Room
):
ServerResponse
<
"chat"
>
{
scope
.
sendChat
(
user
,
message
.
message
);
return
{
ok
:
true
};
}
...
...
server/message/handler/roomJoinHandler.ts
View file @
ab78d0b
import
{
ServerInboundMessage
,
ServerResponse
}
from
"../../../common"
;
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
Room
}
from
"../../room/Room"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
RoomInfo
}
from
"../../room/types"
;
import
{
User
}
from
"../../user/User"
;
import
{
MessageResponse
,
RoomJoinMessage
}
from
"../types"
;
export
function
roomJoinHandler
(
user
:
User
,
message
:
RoomJoinMessage
):
MessageResponse
<
RoomInfo
>
{
connection
:
Connection
,
message
:
ServerInboundMessage
<
"joinRoom"
>
,
scope
:
Room
):
ServerResponse
<
"joinRoom"
>
{
const
room
=
RoomManager
.
instance
().
get
(
message
.
uuid
);
if
(
room
!==
undefined
)
{
room
.
connect
(
user
);
...
...
server/message/handler/roomLeaveHandler.ts
View file @
ab78d0b
import
{
ServerInboundMessage
,
ServerResponse
}
from
"../../../common"
;
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
Room
}
from
"../../room/Room"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
User
}
from
"../../user/User"
;
import
{
MessageResponse
,
RoomLeaveMessage
}
from
"../types"
;
export
function
roomLeaveHandler
(
user
:
User
,
message
:
RoomLeaveMessage
):
MessageResponse
<
undefined
>
{
connection
:
Connection
,
message
:
ServerInboundMessage
<
"leaveRoom"
>
,
scope
:
Room
):
ServerResponse
<
"leaveRoom"
>
{
user
.
room
?.
disconnect
(
user
);
return
{
ok
:
true
};
}
...
...
server/message/handler/roomListRequestHandler.ts
View file @
ab78d0b
import
{
ServerInboundMessage
,
ServerInboundMessageMap
,
ServerResponse
,
}
from
"../../../common"
;
import
{
RoomDescription
}
from
"../../../common/dataType"
;
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
RoomManager
}
from
"../../room/RoomManager"
;
import
{
RoomDescription
,
RoomInfo
}
from
"../../room/types"
;
import
{
User
}
from
"../../user/User"
;
import
{
MessageResponse
,
RoomListRequestMessage
}
from
"../types"
;
export
function
roomListRequestHandler
(
user
:
User
,
message
:
RoomListRequestMessage
):
MessageResponse
<
RoomDescription
[]
>
{
connection
:
Connection
,
message
:
ServerInboundMessage
<
"roomList"
>
,
scope
:
User
):
ServerResponse
<
"roomList"
>
{
return
{
ok
:
true
,
result
:
RoomManager
.
instance
().
list
()
};
}
...
...
server/room/Room.ts
View file @
ab78d0b
import
{
Connection
}
from
"../connection/Connection"
;
import
{
v4
as
uuidv4
}
from
"uuid"
;
import
{
RoomDescription
,
RoomInfo
}
from
"./types"
;
import
{
Message
,
RoomChatMessage
,
RoomUserUpdateMessage
,
}
from
"../message/types"
;
import
{
UserData
}
from
"../user/types"
;
import
{
User
}
from
"../user/User"
;
import
{
MessageHandlerChain
}
from
"../message/MessageHandlerChain"
;
import
{
HandlerMap
}
from
"../message/MessageHandler"
;
import
{
roomJoinHandler
}
from
"../message/handler/roomJoinHandler"
;
import
{
roomLeaveHandler
}
from
"../message/handler/roomLeaveHandler"
;
import
{
chatHandler
}
from
"../message/handler/roomChatHandler"
;
export
class
Room
{
public
readonly
uuid
:
string
;
...
...
@@ -19,10 +17,17 @@ export class Room {
private
closed
:
boolean
=
false
;
public
handler
:
HandlerMap
<
Room
>
;
constructor
(
name
:
string
,
maxUsers
:
number
=
8
)
{
this
.
uuid
=
uuidv4
();
this
.
name
=
name
;
this
.
maxUsers
=
maxUsers
;
this
.
handler
=
new
HandlerMap
<
Room
>
(
this
,
{
joinRoom
:
roomJoinHandler
,
leaveRoom
:
roomLeaveHandler
,
chat
:
chatHandler
,
});
}
public
connect
(
user
:
User
):
void
{
...
...
server/room/RoomManager.ts
View file @
ab78d0b
import
{
RoomDescription
}
from
"../../common/dataType"
;
import
{
Room
}
from
"./Room"
;
import
{
RoomDescription
}
from
"./types"
;
export
class
RoomManager
{
private
static
_instance
:
RoomManager
;
...
...
server/user/User.ts
View file @
ab78d0b
import
{
UserData
}
from
"../../common/dataType"
;
import
{
Connection
}
from
"../connection/Connection"
;
import
{
roomListRequestHandler
}
from
"../message/handler/roomListRequestHandler"
;
import
{
HandlerMap
}
from
"../message/MessageHandler"
;
import
{
Room
}
from
"../room/Room"
;
import
{
UserData
}
from
"./types"
;
export
class
User
{
public
readonly
username
:
string
;
...
...
@@ -15,7 +16,9 @@ export class User {
constructor
(
username
:
string
,
connection
:
Connection
)
{
this
.
username
=
username
;
this
.
connection
=
connection
;
this
.
handler
=
new
HandlerMap
<
User
>
(
this
,
{});
this
.
handler
=
new
HandlerMap
<
User
>
(
this
,
{
roomList
:
roomListRequestHandler
,
});
}
public
getData
():
UserData
{
...
...
Please
register
or
login
to post a comment