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 19:44:45 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
850cb0d3678c6a4039f5beb351d89783dd8cf254
850cb0d3
1 parent
5e18de73
타입 리팩토링
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
44 additions
and
22 deletions
common/message.ts
common/package.json
common/yarn.lock
server/connection/Connection.ts
server/message/MessageHandler.ts
server/message/MessageHandlerChain.ts
server/package.json
server/room/Room.ts
server/yarn.lock
common/message.ts
View file @
850cb0d
import
{
RoomDescription
,
RoomInfo
}
from
"./dataType"
;
import
{
keys
}
from
"ts-transformer-keys"
;
// 서버로 들어오는 메세지 타입을 정의합니다.
// 'result' 속성은 서버 요청 결과에만 포함되는 특별한 속성입니다.
export
interface
ServerInboundMessageMap
{
interface
ServerInboundMessageMap
{
// 로그인을 시도합니다.
login
:
{
username
:
string
;
...
...
@@ -47,7 +48,7 @@ export interface ServerInboundMessageMap {
}
// 서버에서 나가는 메세지 타입을 정의합니다.
export
interface
ServerOutboundMessageMap
{
interface
ServerOutboundMessageMap
{
// 방에 접속 중인 유저 목록이 업데이트 되었습니다.
updateRoomUser
:
{
state
:
"added"
|
"updated"
|
"removed"
;
...
...
@@ -121,10 +122,16 @@ export interface ServerOutboundMessageMap {
};
}
export
type
ServerInboundMessage
<
Key
extends
keyof
ServerInboundMessageMap
>
=
Omit
<
ServerInboundMessageMap
[
Key
],
"result"
>
;
export
type
ServerInboundMessageKey
=
keyof
ServerInboundMessageMap
;
export
interface
ServerResponse
<
Key
extends
keyof
ServerInboundMessageMap
>
{
export
const
ServerInboundMessageKeyArray
=
keys
<
ServerInboundMessageMap
>
();
export
type
ServerInboundMessage
<
Key
extends
ServerInboundMessageKey
>
=
Omit
<
ServerInboundMessageMap
[
Key
],
"result"
>
;
export
interface
ServerResponse
<
Key
extends
ServerInboundMessageKey
>
{
ok
:
boolean
;
reason
?:
string
;
result
?:
"result"
extends
keyof
ServerInboundMessageMap
[
Key
]
...
...
@@ -134,3 +141,7 @@ export interface ServerResponse<Key extends keyof ServerInboundMessageMap> {
export
type
ServerOutboundMessage
<
Key
extends
keyof
ServerOutboundMessageMap
>
=
ServerOutboundMessageMap
[
Key
];
export
type
ServerOutboundMessageKey
=
keyof
ServerOutboundMessageMap
;
export
const
ServerOutboundMessageKeyArray
=
keys
<
ServerOutboundMessageMap
>
();
...
...
common/package.json
0 → 100644
View file @
850cb0d
{
"name"
:
"common"
,
"version"
:
"1.0.0"
,
"main"
:
"index.js"
,
"license"
:
"MIT"
,
"dependencies"
:
{
"ts-transformer-keys"
:
"^0.4.3"
}
}
common/yarn.lock
0 → 100644
View file @
850cb0d
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
ts-transformer-keys@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
server/connection/Connection.ts
View file @
850cb0d
import
{
Socket
}
from
"socket.io"
;
import
{
ServerOutboundMessage
,
ServerOutboundMessage
Map
}
from
"../../common"
;
import
{
ServerOutboundMessage
,
ServerOutboundMessage
Key
}
from
"../../common"
;
import
{
MessageHandlerChain
}
from
"../message/MessageHandlerChain"
;
import
{
Room
}
from
"../room/Room"
;
import
{
User
}
from
"../user/User"
;
...
...
@@ -16,7 +16,7 @@ export class Connection {
this
.
messageHandlerChain
=
new
MessageHandlerChain
(
this
);
}
public
send
<
T
extends
keyof
ServerOutboundMessageMap
>
(
public
send
<
T
extends
ServerOutboundMessageKey
>
(
type
:
T
,
message
:
ServerOutboundMessage
<
T
>
)
{
...
...
server/message/MessageHandler.ts
View file @
850cb0d
import
{
Connection
}
from
"../connection/Connection"
;
import
{
ServerInboundMessage
,
ServerInboundMessage
Map
,
ServerInboundMessage
Key
,
ServerResponse
,
}
from
"../../common/index"
;
import
{
User
}
from
"../user/User"
;
type
UserHandlerMap
=
{
[
Key
in
keyof
ServerInboundMessageMap
]?:
(
[
Key
in
ServerInboundMessageKey
]?:
(
user
:
User
,
message
:
ServerInboundMessage
<
Key
>
)
=>
ServerResponse
<
Key
>
;
...
...
@@ -21,7 +21,7 @@ export class MessageHandler {
}
public
handle
(
type
:
keyof
ServerInboundMessageMap
,
type
:
ServerInboundMessageKey
,
user
:
User
,
message
:
any
,
callback
:
Function
...
...
server/message/MessageHandlerChain.ts
View file @
850cb0d
import
{
Connection
}
from
"../connection/Connection"
;
import
{
ServerInboundMessage
,
ServerInboundMessageMap
,
ServerInboundMessageKey
,
ServerOutboundMessageKeyArray
,
ServerResponse
,
}
from
"../../common/index"
;
import
{
keys
}
from
"ts-transformer-keys"
;
import
{
User
}
from
"../user/User"
;
export
class
MessageHandlerChain
{
...
...
@@ -24,8 +24,8 @@ export class MessageHandlerChain {
}
);
for
(
const
key
in
keys
<
ServerInboundMessageMap
>
()
)
{
const
type
=
key
as
keyof
ServerInboundMessageMap
;
for
(
const
key
in
ServerOutboundMessageKeyArray
)
{
const
type
=
key
as
ServerInboundMessageKey
;
this
.
connection
.
socket
.
on
(
key
,
(
message
:
any
,
callback
:
Function
)
=>
{
// Game > Room > User 순으로 전달
if
(
...
...
server/package.json
View file @
850cb0d
...
...
@@ -16,7 +16,6 @@
"socket.io"
:
"^4.1.2"
,
"socket.io-client"
:
"^4.1.2"
,
"ts-node"
:
"^9.1.1"
,
"ts-transformer-keys"
:
"^0.4.3"
,
"typescript"
:
"^4.2.4"
,
"uuid"
:
"^8.3.2"
},
...
...
server/room/Room.ts
View file @
850cb0d
...
...
@@ -6,7 +6,7 @@ import { MessageHandler } from "../message/MessageHandler";
import
{
ServerInboundMessage
,
ServerOutboundMessage
,
ServerOutboundMessage
Map
,
ServerOutboundMessage
Key
,
}
from
"../../common"
;
import
{
RoomDescription
,
RoomInfo
,
UserData
}
from
"../../common/dataType"
;
...
...
@@ -86,7 +86,7 @@ export class Room {
};
}
public
broadcast
<
T
extends
keyof
ServerOutboundMessageMap
>
(
public
broadcast
<
T
extends
ServerOutboundMessageKey
>
(
type
:
T
,
message
:
ServerOutboundMessage
<
T
>
,
except
?:
User
...
...
server/yarn.lock
View file @
850cb0d
...
...
@@ -1657,11 +1657,6 @@ ts-node@^9.1.1:
source-map-support "^0.5.17"
yn "3.1.1"
ts-transformer-keys@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
type-detect@^4.0.0, type-detect@^4.0.5:
version "4.0.8"
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"
...
...
Please
register
or
login
to post a comment