Toggle navigation
Toggle navigation
This project
Loading...
Sign in
201side
/
floater
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
3
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
robin*
2020-12-10 11:50:36 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
4dc83c7703cedd54c1d962caf6272f27e1abb586
4dc83c77
1 parent
c026b943
서브스레드 작성과 브로드캐스팅 구현
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
2 deletions
src/socket.js
src/socket.js
View file @
4dc83c7
const
sio
=
require
(
'socket.io'
);
const
db
=
require
(
'./db'
);
const
hash
=
require
(
'./hash'
);
let
io
;
module
.
exports
=
{
init
(
http
)
{
io
=
sio
(
http
);
io
.
on
(
'connection'
,
(
socket
)
=>
{
socket
.
on
(
'init'
,
(
id
)
=>
{
socket
.
on
(
'init'
,
async
(
id
)
=>
{
let
thread
=
await
db
.
get
(
'thread'
).
findOne
(
id
);
if
(
!
thread
)
{
socket
.
close
();
return
;
}
console
.
log
(
`init
${
id
}
`
);
db
.
get
(
'subthread'
).
find
({
parent
:
id
},
{
sort
:
'+_id'
}).
each
((
thread
,
_
)
=>
{
db
.
get
(
'subthread'
).
find
({
parent
:
thread
.
_
id
},
{
sort
:
'+_id'
}).
each
((
thread
,
_
)
=>
{
socket
.
emit
(
'thread'
,
thread
);
}).
then
(()
=>
{
socket
.
join
(
`thread-
${
thread
.
_id
}
`
);
console
.
log
(
`join thread-
${
thread
.
_id
}
`
);
});
socket
.
on
(
'write'
,
async
(
content
)
=>
{
console
.
log
(
`write
${
content
}
`
);
let
subthread
=
await
db
.
get
(
'subthread'
).
insert
({
parent
:
thread
.
_id
,
writer
:
hash
(
thread
.
_id
,
socket
.
handshake
.
address
),
content
,
no
:
++
thread
.
count
});
db
.
get
(
'thread'
).
update
(
thread
.
_id
,
{
$set
:
{
count
:
thread
.
count
,
lastUpdated
:
Date
.
now
()}});
io
.
to
(
`thread-
${
thread
.
_id
}
`
).
emit
(
'thread'
,
subthread
);
console
.
log
(
`broadcast thread-
${
thread
.
_id
}
`
);
});
});
});
...
...
Please
register
or
login
to post a comment