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 16:31:12 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
000bc72508c5a8b92bb24f766b52ec487bc69149
000bc725
1 parent
53f5d2b5
방 접속시 유저 정보 업데이트
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
62 additions
and
1 deletions
server/message/types.ts
server/room/Room.ts
server/user/User.ts
server/user/types.ts
server/message/types.ts
View file @
000bc72
import
{
RoomData
}
from
"../room/types"
;
import
{
UserData
}
from
"../user/types"
;
export
interface
Message
{
readonly
type
:
string
;
...
...
@@ -24,9 +25,24 @@ export class RoomLeaveMessage implements Message {
constructor
()
{}
}
export
class
RoomInfoMessage
implements
Message
{
readonly
type
=
MessageType
.
ROOM_INFO
;
constructor
(
public
userdata
:
UserData
[])
{}
}
export
class
RoomUserUpdateMessage
implements
Message
{
readonly
type
=
MessageType
.
ROOM_USER_UPDATE
;
constructor
(
public
state
:
"added"
|
"updated"
|
"removed"
,
public
userdata
:
UserData
)
{}
}
export
class
MessageType
{
static
readonly
LOGIN
=
"login"
;
static
readonly
ROOM_LIST
=
"room_list"
;
static
readonly
ROOM_JOIN
=
"room_join"
;
static
readonly
ROOM_LEAVE
=
"room_leave"
;
static
readonly
ROOM_INFO
=
"room_info"
;
static
readonly
ROOM_USER_UPDATE
=
"room_user_update"
;
}
...
...
server/room/Room.ts
View file @
000bc72
import
{
Connection
}
from
"../connection/Connection"
;
import
{
v4
as
uuidv4
}
from
"uuid"
;
import
{
RoomData
}
from
"./types"
;
import
{
Message
,
RoomInfoMessage
,
RoomUserUpdateMessage
,
}
from
"../message/types"
;
import
{
UserData
}
from
"../user/types"
;
export
class
Room
{
public
readonly
uuid
:
string
;
...
...
@@ -19,6 +25,12 @@ export class Room {
}
public
connect
(
connection
:
Connection
):
void
{
// TODO: 더 나은 인증 처리
const
user
=
connection
.
user
;
if
(
user
===
undefined
)
{
return
;
}
if
(
this
.
connections
.
includes
(
connection
)
||
this
.
connections
.
length
>=
this
.
maxConnections
...
...
@@ -28,13 +40,27 @@ export class Room {
this
.
connections
.
push
(
connection
);
connection
.
room
=
this
;
// TODO: 더 나은 관리
this
.
broadcast
(
new
RoomUserUpdateMessage
(
"added"
,
user
.
getData
()));
var
users
:
UserData
[]
=
[];
this
.
connections
.
forEach
((
con
)
=>
{
if
(
con
.
user
!==
undefined
&&
connection
!==
con
)
{
users
.
push
(
con
.
user
.
getData
());
}
});
connection
.
send
(
new
RoomInfoMessage
(
users
));
}
public
disconnect
(
connection
:
Connection
):
void
{
const
index
=
this
.
connections
.
indexOf
(
connection
);
if
(
index
>
-
1
)
{
if
(
connection
.
user
!==
undefined
&&
index
>
-
1
)
{
this
.
connections
.
splice
(
index
,
1
);
connection
.
room
=
undefined
;
this
.
broadcast
(
new
RoomUserUpdateMessage
(
"removed"
,
connection
.
user
.
getData
())
);
}
}
...
...
@@ -47,6 +73,14 @@ export class Room {
};
}
public
broadcast
(
message
:
Message
,
except
?:
Connection
):
void
{
this
.
connections
.
forEach
((
connection
)
=>
{
if
(
connection
!==
except
)
{
connection
.
send
(
message
);
}
});
}
public
close
():
void
{
if
(
!
this
.
closed
)
{
this
.
connections
.
forEach
((
connection
)
=>
this
.
disconnect
(
connection
));
...
...
server/user/User.ts
View file @
000bc72
import
{
UserData
}
from
"./types"
;
export
class
User
{
public
readonly
username
:
string
;
constructor
(
username
:
string
)
{
this
.
username
=
username
;
}
public
getData
():
UserData
{
return
{
username
:
this
.
username
,
};
}
}
...
...
server/user/types.ts
0 → 100644
View file @
000bc72
export
interface
UserData
{
username
:
string
;
}
Please
register
or
login
to post a comment