Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
F
festivelo_backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Festivelo
festivelo_backend
Commits
6449d964
Commit
6449d964
authored
6 months ago
by
nahyun
Browse files
Options
Downloads
Patches
Plain Diff
pref: 공동작업
parent
816a063e
No related branches found
No related tags found
1 merge request
!18
Secure
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
WebSocket.js
+83
-0
83 additions, 0 deletions
WebSocket.js
package-lock.json
+23
-1
23 additions, 1 deletion
package-lock.json
package.json
+2
-1
2 additions, 1 deletion
package.json
with
108 additions
and
2 deletions
WebSocket.js
0 → 100644
+
83
−
0
View file @
6449d964
const
WebSocket
=
require
(
'
ws
'
);
const
mongoose
=
require
(
'
mongoose
'
);
class
WebSocketServer
{
constructor
(
server
)
{
this
.
wss
=
new
WebSocket
.
Server
({
server
});
this
.
clients
=
new
Set
();
// 모든 클라이언트 관리
this
.
wss
.
on
(
'
connection
'
,
(
ws
)
=>
{
console
.
log
(
'
새로운 클라이언트 연결됨
'
);
this
.
clients
.
add
(
ws
);
ws
.
on
(
'
message
'
,
(
message
)
=>
{
try
{
const
data
=
JSON
.
parse
(
message
);
this
.
handleMessage
(
data
,
ws
);
}
catch
(
error
)
{
console
.
error
(
'
메시지 처리 중 오류:
'
,
error
);
}
});
ws
.
on
(
'
close
'
,
()
=>
{
this
.
clients
.
delete
(
ws
);
console
.
log
(
'
클라이언트 연결 해제됨
'
);
});
});
this
.
setupChangeStream
();
}
handleMessage
(
data
,
ws
)
{
// 메시지 타입에 따른 처리
switch
(
data
.
type
)
{
case
'
update
'
:
case
'
delete
'
:
case
'
addPlace
'
:
case
'
removePlace
'
:
case
'
create
'
:
// 다른 모든 클라이언트에게 변경사항 브로드캐스트
this
.
broadcast
(
data
,
ws
);
break
;
}
}
broadcast
(
data
,
sender
)
{
this
.
clients
.
forEach
(
client
=>
{
if
(
client
!==
sender
&&
client
.
readyState
===
WebSocket
.
OPEN
)
{
client
.
send
(
JSON
.
stringify
(
data
));
}
});
}
setupChangeStream
()
{
const
pipeline
=
[
{
$match
:
{
'
operationType
'
:
{
$in
:
[
'
update
'
,
'
insert
'
,
'
delete
'
]
}
}
}
];
const
changeStream
=
mongoose
.
model
(
'
Trip
'
).
watch
(
pipeline
,
{
fullDocument
:
'
updateLookup
'
});
changeStream
.
on
(
'
change
'
,
(
change
)
=>
{
const
message
=
{
type
:
change
.
operationType
,
documentId
:
change
.
documentKey
.
_id
.
toString
(),
data
:
change
.
fullDocument
};
// 모든 클라이언트에게 변경사항 전송
this
.
clients
.
forEach
(
client
=>
{
if
(
client
.
readyState
===
WebSocket
.
OPEN
)
{
client
.
send
(
JSON
.
stringify
(
message
));
}
});
});
}
}
module
.
exports
=
WebSocketServer
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
package-lock.json
+
23
−
1
View file @
6449d964
...
...
@@ -23,7 +23,8 @@
"mongodb"
:
"^6.11.0"
,
"mongoose"
:
"^8.8.1"
,
"passport"
:
"^0.7.0"
,
"passport-google-oauth20"
:
"^2.0.0"
"passport-google-oauth20"
:
"^2.0.0"
,
"ws"
:
"^8.18.0"
}
},
"node_modules/@mapbox/node-pre-gyp"
:
{
...
...
@@ -2092,6 +2093,27 @@
"integrity"
:
"sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ=="
,
"license"
:
"ISC"
},
"node_modules/ws"
:
{
"version"
:
"8.18.0"
,
"resolved"
:
"https://registry.npmjs.org/ws/-/ws-8.18.0.tgz"
,
"integrity"
:
"sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw=="
,
"license"
:
"MIT"
,
"engines"
:
{
"node"
:
">=10.0.0"
},
"peerDependencies"
:
{
"bufferutil"
:
"^4.0.1"
,
"utf-8-validate"
:
">=5.0.2"
},
"peerDependenciesMeta"
:
{
"bufferutil"
:
{
"optional"
:
true
},
"utf-8-validate"
:
{
"optional"
:
true
}
}
},
"node_modules/yallist"
:
{
"version"
:
"4.0.0"
,
"resolved"
:
"https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz"
,
...
...
This diff is collapsed.
Click to expand it.
package.json
+
2
−
1
View file @
6449d964
...
...
@@ -14,7 +14,8 @@
"
mongodb
"
:
"
^6.11.0
"
,
"
mongoose
"
:
"
^8.8.1
"
,
"
passport
"
:
"
^0.7.0
"
,
"
passport-google-oauth20
"
:
"
^2.0.0
"
"
passport-google-oauth20
"
:
"
^2.0.0
"
,
"
ws
"
:
"
^8.18.0
"
},
"scripts"
:
{
"start"
:
"node login.js"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment