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 20:19:29 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
d52aeb96c6920db3e5b14987e48eb2b5981ddbcc
d52aeb96
1 parent
850cb0d3
ts-transformer-keys 제거, 테스트 재작성
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
97 additions
and
95 deletions
common/message.ts
common/package.json
common/yarn.lock
server/connection/Connection.ts
server/message/MessageHandlerChain.ts
server/test.test.ts
common/message.ts
View file @
d52aeb9
import
{
RoomDescription
,
RoomInfo
}
from
"./dataType"
;
import
{
keys
}
from
"ts-transformer-keys"
;
// 서버로 들어오는 메세지 타입을 정의합니다.
// 'result' 속성은 서버 요청 결과에만 포함되는 특별한 속성입니다.
...
...
@@ -122,9 +121,12 @@ interface ServerOutboundMessageMap {
};
}
export
type
ServerInboundMessageKey
=
keyof
ServerInboundMessageMap
;
export
interface
RawMessage
{
type
:
string
;
message
:
any
;
}
export
const
ServerInboundMessageKeyArray
=
keys
<
ServerInboundMessageMap
>
()
;
export
type
ServerInboundMessageKey
=
keyof
ServerInboundMessageMap
;
export
type
ServerInboundMessage
<
Key
extends
ServerInboundMessageKey
>
=
Omit
<
ServerInboundMessageMap
[
Key
],
...
...
@@ -143,5 +145,3 @@ export type ServerOutboundMessage<Key extends keyof ServerOutboundMessageMap> =
ServerOutboundMessageMap
[
Key
];
export
type
ServerOutboundMessageKey
=
keyof
ServerOutboundMessageMap
;
export
const
ServerOutboundMessageKeyArray
=
keys
<
ServerOutboundMessageMap
>
();
...
...
common/package.json
View file @
d52aeb9
...
...
@@ -3,7 +3,5 @@
"version"
:
"1.0.0"
,
"main"
:
"index.js"
,
"license"
:
"MIT"
,
"dependencies"
:
{
"ts-transformer-keys"
:
"^0.4.3"
}
"dependencies"
:
{}
}
...
...
common/yarn.lock
View file @
d52aeb9
...
...
@@ -2,7 +2,3 @@
# 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 @
d52aeb9
...
...
@@ -20,6 +20,9 @@ export class Connection {
type
:
T
,
message
:
ServerOutboundMessage
<
T
>
)
{
this
.
socket
.
emit
(
type
as
string
,
message
);
this
.
socket
.
emit
(
"msg"
,
{
type
:
type
as
string
,
message
:
message
,
});
}
}
...
...
server/message/MessageHandlerChain.ts
View file @
d52aeb9
import
{
Connection
}
from
"../connection/Connection"
;
import
{
RawMessage
,
ServerInboundMessage
,
ServerInboundMessageKey
,
ServerOutboundMessageKeyArray
,
ServerResponse
,
}
from
"../../common/index"
;
import
{
User
}
from
"../user/User"
;
...
...
@@ -13,20 +13,25 @@ export class MessageHandlerChain {
constructor
(
connection
:
Connection
)
{
this
.
connection
=
connection
;
// 유저 정보가 없으므로 로그인은 따로 핸들링
this
.
connection
.
socket
.
on
(
"login"
,
(
message
:
ServerInboundMessage
<
"login"
>
,
callback
:
Function
)
=>
{
connection
.
user
=
new
User
(
message
.
username
,
connection
);
console
.
log
(
`User
${
message
.
username
}
has logged in!`
);
this
.
connection
.
socket
.
on
(
"msg"
,
(
raw
:
RawMessage
,
callback
:
Function
)
=>
{
this
.
handleRaw
(
connection
,
raw
,
callback
);
});
}
callback
({
ok
:
true
});
private
handleRaw
(
connection
:
Connection
,
raw
:
RawMessage
,
callback
:
Function
)
{
const
type
=
raw
.
type
as
ServerInboundMessageKey
;
const
message
=
raw
.
message
;
// 유저 정보가 없으므로 로그인은 따로 핸들링
if
(
type
===
"login"
)
{
this
.
handleLogin
(
connection
,
message
,
callback
);
return
;
}
);
for
(
const
key
in
ServerOutboundMessageKeyArray
)
{
const
type
=
key
as
ServerInboundMessageKey
;
this
.
connection
.
socket
.
on
(
key
,
(
message
:
any
,
callback
:
Function
)
=>
{
// Game > Room > User 순으로 전달
if
(
connection
?.
user
?.
room
&&
...
...
@@ -41,15 +46,19 @@ export class MessageHandlerChain {
if
(
connection
?.
user
&&
connection
.
user
.
handler
.
handle
(
type
,
connection
.
user
,
message
,
callback
)
connection
.
user
.
handler
.
handle
(
type
,
connection
.
user
,
message
,
callback
)
)
return
;
});
}
private
handleLogin
(
connection
:
Connection
,
message
:
ServerInboundMessage
<
"login"
>
,
callback
:
Function
)
{
connection
.
user
=
new
User
(
message
.
username
,
connection
);
console
.
log
(
`User
${
message
.
username
}
has logged in!`
);
callback
({
ok
:
true
});
}
}
...
...
server/test.test.ts
View file @
d52aeb9
import
ioclient
,
{
Socket
}
from
"socket.io-client"
;
import
{
LoginMessage
,
MessageResponse
,
MessageType
,
RoomChatMessage
,
RoomJoinMessage
,
RoomLeaveMessage
,
RoomListRequestMessage
,
RoomUserUpdateMessage
,
}
from
"./message/types"
;
import
{
expect
}
from
"chai"
;
import
{
Server
}
from
"./Server"
;
import
{
RoomDescription
,
RoomInfo
}
from
"./room/types"
;
import
{
response
}
from
"express"
;
import
{
RawMessage
,
ServerInboundMessage
,
ServerInboundMessageKey
,
ServerOutboundMessage
,
ServerOutboundMessageKey
,
ServerResponse
,
}
from
"../common"
;
describe
(
"server"
,
()
=>
{
const
PORT
=
3000
;
...
...
@@ -39,39 +36,58 @@ describe("server", () => {
client2
.
close
();
});
var
roomUserUpdateMessage
:
RoomUserUpdateMessage
;
var
roomChatMessage
:
RoomChatMessage
;
var
roomUserUpdateMessage
:
ServerOutboundMessage
<
"updateRoomUser"
>
;
var
roomChatMessage
:
ServerOutboundMessage
<
"chat"
>
;
const
send
=
<
T
extends
ServerInboundMessageKey
>
(
socket
:
Socket
,
type
:
T
,
message
:
ServerInboundMessage
<
T
>
,
callback
:
(
response
:
ServerResponse
<
T
>
)
=>
void
)
=>
{
socket
.
emit
(
"msg"
,
{
type
:
type
as
string
,
message
:
message
,
},
callback
);
};
step
(
"register listeners"
,
()
=>
{
client1
.
on
(
MessageType
.
ROOM_USER_UPDATE
,
(
message
:
RoomUserUpdateMessage
)
=>
{
roomUserUpdateMessage
=
message
;
}
);
client1
.
on
(
"msg"
,
(
raw
:
RawMessage
)
=>
{
if
(
raw
.
type
==
"updateRoomUser"
)
roomUserUpdateMessage
=
raw
.
message
;
});
client1
.
on
(
MessageType
.
ROOM_CHAT
,
(
message
:
RoomChat
Message
)
=>
{
roomChatMessage
=
message
;
client1
.
on
(
"msg"
,
(
raw
:
Raw
Message
)
=>
{
if
(
raw
.
type
==
"chat"
)
roomChatMessage
=
raw
.
message
;
});
});
step
(
"login 1"
,
(
done
)
=>
{
client1
.
emit
(
MessageType
.
LOGIN
,
new
LoginMessage
(
"guest1"
),
(
response
:
MessageResponse
<
undefined
>
)
=>
{
expect
(
response
.
ok
).
to
.
equals
(
true
);
send
(
client1
,
"login"
,
{
username
:
"guest1"
,
},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
done
();
}
);
});
step
(
"login 2"
,
(
done
)
=>
{
client2
.
emit
(
MessageType
.
LOGIN
,
new
LoginMessage
(
"guest2"
),
(
response
:
MessageResponse
<
undefined
>
)
=>
{
expect
(
response
.
ok
).
to
.
equals
(
true
);
send
(
client2
,
"login"
,
{
username
:
"guest2"
,
},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
done
();
}
);
...
...
@@ -80,10 +96,7 @@ describe("server", () => {
var
roomToJoin
:
string
;
step
(
"room list"
,
(
done
)
=>
{
client1
.
emit
(
MessageType
.
ROOM_LIST_REQUEST
,
new
RoomListRequestMessage
(),
(
response
:
MessageResponse
<
RoomDescription
[]
>
)
=>
{
send
(
client1
,
"roomList"
,
{},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
expect
(
response
.
result
!==
undefined
).
to
.
eq
(
true
);
if
(
response
.
result
)
{
...
...
@@ -91,15 +104,11 @@ describe("server", () => {
roomToJoin
=
response
.
result
[
0
].
uuid
;
}
done
();
}
);
});
});
step
(
"room join 1"
,
(
done
)
=>
{
client1
.
emit
(
MessageType
.
ROOM_JOIN
,
new
RoomJoinMessage
(
roomToJoin
),
(
response
:
MessageResponse
<
RoomInfo
>
)
=>
{
send
(
client1
,
"joinRoom"
,
{
uuid
:
roomToJoin
},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
expect
(
response
.
result
!==
undefined
).
to
.
eq
(
true
);
if
(
response
.
result
)
{
...
...
@@ -108,15 +117,11 @@ describe("server", () => {
expect
(
response
.
result
.
users
[
0
].
username
).
to
.
eq
(
"guest1"
);
}
done
();
}
);
});
});
step
(
"room join 2"
,
(
done
)
=>
{
client2
.
emit
(
MessageType
.
ROOM_JOIN
,
new
RoomJoinMessage
(
roomToJoin
),
(
response
:
MessageResponse
<
RoomInfo
>
)
=>
{
send
(
client2
,
"joinRoom"
,
{
uuid
:
roomToJoin
},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
expect
(
response
.
result
!==
undefined
).
to
.
eq
(
true
);
if
(
response
.
result
)
{
...
...
@@ -124,8 +129,7 @@ describe("server", () => {
expect
(
response
.
result
.
users
.
length
).
to
.
eq
(
2
);
}
done
();
}
);
});
});
// TODO: RoomUserUpdateMessage가 아직 도착하지 않았는데 실행되는 경우
...
...
@@ -133,19 +137,15 @@ describe("server", () => {
expect
(
roomUserUpdateMessage
!==
undefined
).
to
.
eq
(
true
);
if
(
roomUserUpdateMessage
)
{
expect
(
roomUserUpdateMessage
.
state
).
to
.
eq
(
"added"
);
expect
(
roomUserUpdateMessage
.
user
data
.
username
).
to
.
eq
(
"guest2"
);
expect
(
roomUserUpdateMessage
.
user
.
username
).
to
.
eq
(
"guest2"
);
}
});
step
(
"client 2 send chat"
,
(
done
)
=>
{
client2
.
emit
(
MessageType
.
ROOM_CHAT
,
new
RoomChatMessage
(
"Hello World"
),
(
response
:
MessageResponse
<
undefined
>
)
=>
{
send
(
client2
,
"chat"
,
{
message
:
"Hello World"
},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
done
();
}
);
});
});
step
(
"client 1 received chat"
,
()
=>
{
...
...
@@ -157,21 +157,17 @@ describe("server", () => {
});
step
(
"client 2 leave"
,
(
done
)
=>
{
client2
.
emit
(
MessageType
.
ROOM_LEAVE
,
new
RoomLeaveMessage
(),
(
response
:
MessageResponse
<
undefined
>
)
=>
{
send
(
client2
,
"leaveRoom"
,
{},
(
response
)
=>
{
expect
(
response
.
ok
).
to
.
eq
(
true
);
done
();
}
);
});
});
step
(
"client 1 received user update"
,
()
=>
{
expect
(
roomUserUpdateMessage
!==
undefined
).
to
.
eq
(
true
);
if
(
roomUserUpdateMessage
)
{
expect
(
roomUserUpdateMessage
.
state
).
to
.
eq
(
"removed"
);
expect
(
roomUserUpdateMessage
.
user
data
.
username
).
to
.
eq
(
"guest2"
);
expect
(
roomUserUpdateMessage
.
user
.
username
).
to
.
eq
(
"guest2"
);
}
});
});
...
...
Please
register
or
login
to post a comment