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-23 13:56:39 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
6921c1e6c9ea1e39bf0d61ab68f6b7115ed17856
6921c1e6
1 parent
61a7ad4c
MessageHandlerRegistry 구현
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
29 additions
and
22 deletions
server/connection/Connection.ts
server/message/MessageHandlerRegistry.ts
server/message/handler/LoginHandler.ts
server/message/handler/MessageHandler.ts
server/message/handler/MessageHandlerRegister.ts
server/message/types.ts
server/connection/Connection.ts
View file @
6921c1e
import
{
Socket
}
from
"socket.io"
;
import
{
MessageHandlerRegist
er
}
from
"../message/handler/MessageHandlerRegister
"
;
import
{
MessageHandlerRegist
ry
}
from
"../message/MessageHandlerRegistry
"
;
import
{
User
}
from
"../user/User"
;
export
class
Connection
{
...
...
@@ -10,7 +10,7 @@ export class Connection {
constructor
(
socket
:
Socket
)
{
this
.
socket
=
socket
;
MessageHandlerRegist
er
.
registerMessage
Handlers
(
this
);
MessageHandlerRegist
ry
.
register
Handlers
(
this
);
}
public
get
authenticated
():
boolean
{
...
...
server/message/MessageHandlerRegistry.ts
0 → 100644
View file @
6921c1e
import
{
Connection
}
from
"../connection/Connection"
;
import
{
loginHandler
}
from
"./handler/LoginHandler"
;
import
{
MessageType
}
from
"./types"
;
export
class
MessageHandlerRegistry
{
static
registerHandlers
(
connection
:
Connection
)
{
this
.
registerHandler
(
connection
,
MessageType
.
LOGIN
,
loginHandler
);
}
private
static
registerHandler
<
T
>
(
connection
:
Connection
,
typeName
:
string
,
handler
:
(
connection
:
Connection
,
message
:
T
)
=>
void
)
{
connection
.
socket
.
on
(
typeName
,
(
message
:
T
)
=>
{
handler
(
connection
,
message
);
});
}
}
server/message/handler/LoginHandler.ts
View file @
6921c1e
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
User
}
from
"../../user/User"
;
import
{
LoginMessage
}
from
"../types"
;
import
{
MessageHandler
}
from
"./MessageHandler"
;
export
class
LoginHandler
implements
MessageHandler
<
LoginMessage
>
{
public
handle
(
connection
:
Connection
,
message
:
LoginMessage
):
void
{
export
function
loginHandler
(
connection
:
Connection
,
message
:
LoginMessage
):
void
{
connection
.
user
=
new
User
(
message
.
username
);
console
.
log
(
`User
${
message
.
username
}
has logged in!`
);
}
}
...
...
server/message/handler/MessageHandler.ts
deleted
100644 → 0
View file @
61a7ad4
import
{
Connection
}
from
"../../connection/Connection"
;
export
interface
MessageHandler
<
T
>
{
handle
(
connection
:
Connection
,
message
:
T
):
void
;
}
server/message/handler/MessageHandlerRegister.ts
deleted
100644 → 0
View file @
61a7ad4
import
{
Connection
}
from
"../../connection/Connection"
;
import
{
LoginMessage
}
from
"../types"
;
import
{
LoginHandler
}
from
"./LoginHandler"
;
export
class
MessageHandlerRegister
{
static
registerMessageHandlers
(
connection
:
Connection
)
{
connection
.
socket
.
on
(
"login"
,
(
message
:
LoginMessage
)
=>
{
new
LoginHandler
().
handle
(
connection
,
message
);
});
}
}
server/message/types.ts
View file @
6921c1e
export
interface
LoginMessage
{
username
:
string
;
}
export
class
MessageType
{
static
readonly
LOGIN
=
"login"
;
}
...
...
Please
register
or
login
to post a comment