Skip to content

Commit

Permalink
Resolve merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
SelwynAng committed Nov 2, 2024
2 parents 5b01757 + 295cb00 commit 608f28d
Show file tree
Hide file tree
Showing 9 changed files with 1,536 additions and 7,072 deletions.
3 changes: 2 additions & 1 deletion collab-service/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
38 changes: 38 additions & 0 deletions collab-service/app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import http from "http";
import index from "./index.js";
import { Server } from "socket.io";
import { addMessageToChat } from "./model/repository.js";
import ywsUtils from "y-websocket/bin/utils";
import { WebSocketServer } from "ws";
const setupWSConnection = ywsUtils.setupWSConnection;

const PORT = process.env.PORT || 3002;
const server = http.createServer(index);
const docs = ywsUtils.docs;

const io = new Server(server, {
cors: {
Expand All @@ -16,6 +20,27 @@ const io = new Server(server, {
},
});

const yjsWs = new WebSocketServer({ noServer: true });
yjsWs.on("connection", (conn, req) => {
setupWSConnection(conn, req, {
gc: req.url.slice(1) !== "ws/prosemirror-versions",
});
});

setInterval(() => {
let conns = 0;
docs.forEach((doc) => {
conns += doc.conns.size;
});
const stats = {
conns,
docs: docs.size,
websocket: `ws://localhost:${PORT}`,
http: `http://localhost:${PORT}`,
};
console.log(`${new Date().toISOString()} Stats: ${JSON.stringify(stats)}`);
}, 10000);

io.on("connection", (socket) => {
console.log("User connected to Socket.IO");

Expand All @@ -39,6 +64,19 @@ io.on("connection", (socket) => {
});
});

server.on("upgrade", (request, socket, head) => {
const pathname = new URL(request.url, `http://${request.headers.host}`)
.pathname;

if (pathname.startsWith("/yjs")) {
yjsWs.handleUpgrade(request, socket, head, (ws) => {
yjsWs.emit("connection", ws, request);
});
} else {
socket.destroy();
}
});

connectToMongo()
.then(() => {
server.listen(PORT, () => {
Expand Down
47 changes: 47 additions & 0 deletions collab-service/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Socket.IO Test</title>
<script src="https://cdn.socket.io/4.0.0/socket.io.min.js"></script>
</head>
<body>
<h2>Socket.IO Chat Test</h2>
<input id="roomInput" placeholder="Room ID" />
<button onclick="joinRoom()">Join Room</button>
<br><br>
<input id="messageInput" placeholder="Message" />
<button onclick="sendMessage()">Send Message</button>
<div id="messages"></div>

<script>
const socket = io("http://localhost:3002");

socket.on("connect", () => {
console.log("Connected to Socket.IO server");
});

socket.on("chatMessage", (message) => {
const messagesDiv = document.getElementById("messages");
const messageElem = document.createElement("p");
messageElem.textContent = `Received: ${message.text}`;
messagesDiv.appendChild(messageElem);
});

function joinRoom() {
const roomId = document.getElementById("roomInput").value;
socket.emit("joinRoom", roomId);
console.log(`Joined room: ${roomId}`);
}

function sendMessage() {
const roomId = document.getElementById("roomInput").value;
const text = document.getElementById("messageInput").value;
const message = { roomId, userId: "user1", text };
socket.emit("sendMessage", message);
console.log(`Sent message: ${text}`);
}
</script>
</body>
</html>

Loading

0 comments on commit 608f28d

Please sign in to comment.