Skip to content

Commit 71e5c8a

Browse files
committed
Merge branch 'collab-post-ms6-risky' into deploy-risky
2 parents a3b58da + adbd48f commit 71e5c8a

File tree

3 files changed

+52
-31
lines changed

3 files changed

+52
-31
lines changed

collab/main.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const (
2323
AUTH_FAIL = "auth_fail"
2424
CLOSE_SESSION = "close_session"
2525
CONTENT_CHANGE = "content_change"
26-
PING = "ping"
26+
PING = "ping"
2727
)
2828

2929
var upgrader = websocket.Upgrader{
@@ -140,7 +140,9 @@ func (h *Hub) Run() {
140140
case message := <-h.broadcast:
141141
h.mutex.Lock()
142142
// Update the current workspace for this RoomID
143-
h.workspaces[message.RoomID] = message.Content
143+
if message.Content != "" {
144+
h.workspaces[message.RoomID] = message.Content
145+
}
144146
for client := range h.clients {
145147
if client.roomID == message.RoomID {
146148

@@ -163,8 +165,6 @@ func (h *Hub) Run() {
163165
h.mutex.Unlock()
164166
}
165167

166-
167-
168168
}
169169
}
170170

@@ -306,14 +306,14 @@ func handleMessages(
306306
// if msgData["type"] == "ping" {
307307
// //receives ping from client1, need to send a ping to client2
308308
// //eventually, if present, client2 will send the ping back, which will be broadcasted back to client1.
309-
309+
310310
// userID, _ := msgData["userId"].(string)
311311
// request := Message {
312312
// RoomID: client.roomID,
313313
// UserID: userID,
314314
// Content: []byte("ping request"),
315315
// }
316-
316+
317317
// hub.broadcast <- request
318318
// }
319319

@@ -324,17 +324,30 @@ func handleMessages(
324324
Type: msgData.Type,
325325
}
326326
targetId := msgData.UserID
327-
data, err := persistMappings.Conn.HGetAll(context.Background(), targetId).Result()
327+
ownData, err := persistMappings.Conn.HGetAll(context.Background(), targetId).Result()
328328
if err != nil {
329329
log.Printf("Error retrieving data for userID %s: %v", targetId, err)
330330
} else {
331-
_, err1 := persistMappings.Conn.Del(context.Background(), targetId).Result()
332-
if err1 != nil {
333-
log.Printf("Error deleting data for userID %s: %v", targetId, err1)
331+
// delete room under user id if it curr matches the room ID
332+
ownRoomId := ownData["roomId"]
333+
if ownRoomId == client.roomID {
334+
_, err := persistMappings.Conn.Del(context.Background(), targetId).Result()
335+
if err != nil {
336+
log.Printf("Error deleting data for userID %s: %v", targetId, err)
337+
}
334338
}
335-
_, err2 := persistMappings.Conn.Del(context.Background(), data["otherUser"]).Result()
336-
if err2 != nil {
337-
log.Printf("Error deleting data for other user %s: %v", data["otherUser"], err2)
339+
// delete room under other user if it curr matches the room ID
340+
otherUser := ownData["otherUser"]
341+
othRoomId, err := persistMappings.Conn.HGet(context.Background(), otherUser, "roomId").Result()
342+
if err != nil {
343+
log.Printf("Error retrieving data for otherUser %s: %v", otherUser, err)
344+
} else {
345+
if othRoomId == client.roomID {
346+
_, err := persistMappings.Conn.Del(context.Background(), otherUser).Result()
347+
if err != nil {
348+
log.Printf("Error deleting data for other user %s: %v", otherUser, err)
349+
}
350+
}
338351
}
339352
}
340353
hub.broadcast <- closeMessage
@@ -349,29 +362,28 @@ func handleMessages(
349362
} else if msgData.Type == PING {
350363
// Broadcast the message to other clients
351364
hub.broadcast <- Message{
352-
RoomID: client.roomID,
353-
Type: msgData.Type,
354-
UserID: msgData.UserID,
365+
RoomID: client.roomID,
366+
Type: msgData.Type,
367+
UserID: msgData.UserID,
355368
}
356369

357370
extendExpiryTime(msgData.UserID, persistMappings)
358371
} else {
359372
log.Printf("Unknown message type: %s", msgData.Type)
360-
}
373+
}
361374
}
362375
}
363376

364377
func extendExpiryTime(userId string, persistMappings *verify.PersistMappings) {
365-
378+
366379
ctx := context.Background()
367-
if err := persistMappings.Conn.Expire(ctx, userId, time.Minute * 10).Err(); err != nil {
380+
if err := persistMappings.Conn.Expire(ctx, userId, time.Minute*10).Err(); err != nil {
368381
log.Println("Error extending room time on ping: ", err.Error())
369382
} else {
370-
383+
371384
log.Printf("expiration reset for 10 minutes for user %s: ", userId)
372385
}
373-
374-
386+
375387
}
376388

377389
type ClientWorkspace struct {

peerprep/components/questionpage/CollabEditor.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import CommsPanel from "./CommsPanel";
1919
// import { diff_match_patch } from "diff-match-patch";
2020
import { callFormatter } from "@/app/api/internal/formatter/helper";
2121
import { Ace } from "ace-builds";
22-
import { connect } from "http2";
2322

2423
const PING_INTERVAL_MILLISECONDS = 5000;
2524
const languages: Language[] = ["javascript", "python", "c_cpp"];
@@ -114,6 +113,7 @@ export default function CollabEditor({
114113
}
115114
} catch (e: unknown) {
116115
if (e instanceof Error) {
116+
alert(e.message);
117117
console.error(e.message);
118118
} else {
119119
console.error("An unknown error occurred");
@@ -265,7 +265,7 @@ export default function CollabEditor({
265265
const disconnectCheckInterval = setInterval(() => {
266266
if (
267267
lastPingReceived &&
268-
Date.now() - lastPingReceived > 2 * PING_INTERVAL_MILLISECONDS
268+
Date.now() - lastPingReceived > 3 * PING_INTERVAL_MILLISECONDS
269269
) {
270270
setOtherUserConnected(false);
271271
clearInterval(disconnectCheckInterval);
@@ -276,9 +276,7 @@ export default function CollabEditor({
276276
clearInterval(interval);
277277
clearInterval(disconnectCheckInterval);
278278
};
279-
}, [
280-
notifyRoomOfConnection, PING_INTERVAL_MILLISECONDS, connected, socket
281-
]);
279+
}, [notifyRoomOfConnection, connected, socket]);
282280

283281
const handleCloseConnection = () => {
284282
const confirmClose = confirm(

peerprep/components/questionpage/CommsPanel.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const socket = io(`/`, {
1313

1414
function CommsPanel({ className, roomId }: Props) {
1515
const [stream, setStream] = useState<MediaStream>();
16+
const [callStarts, setCallStarts] = useState<boolean>(false);
1617

1718
const myVideo = useRef<HTMLVideoElement>(null);
1819
const userVideo = useRef<HTMLVideoElement>(null);
@@ -81,10 +82,10 @@ function CommsPanel({ className, roomId }: Props) {
8182

8283
// when we receive the first peer connection, we immediately send out
8384
// a peer connection request.
84-
attachSocketInitiator(stream, roomId, userVideo, connectionRef);
85+
attachSocketInitiator(stream, roomId, userVideo, setCallStarts, connectionRef);
8586

8687
// as the receiver, I will propagate my data outwards now.
87-
attachSocketReceiver(stream, roomId, userVideo, connectionRef);
88+
attachSocketReceiver(stream, roomId, userVideo, setCallStarts, connectionRef);
8889

8990
socket.on("endCall", () => {
9091
// immediately destroy the socket listeners
@@ -96,13 +97,14 @@ function CommsPanel({ className, roomId }: Props) {
9697
tracks.stop();
9798
});
9899
userVideo.current.srcObject = null;
100+
setCallStarts(false);
99101
}
100102
if (connectionRef.current && !connectionRef.current.destroyed) {
101103
connectionRef.current.destroy();
102104
}
103105
// reattach the sockets
104-
attachSocketInitiator(stream, roomId, userVideo, connectionRef);
105-
attachSocketReceiver(stream, roomId, userVideo, connectionRef);
106+
attachSocketInitiator(stream, roomId, userVideo, setCallStarts, connectionRef);
107+
attachSocketReceiver(stream, roomId, userVideo, setCallStarts, connectionRef);
106108
// rejoin the room
107109
socket.emit("joinRoom", {
108110
target: roomId,
@@ -127,6 +129,11 @@ function CommsPanel({ className, roomId }: Props) {
127129
/>
128130
</div>
129131
<div className="video">
132+
{!callStarts &&
133+
<p>
134+
No signal from other user.
135+
(Don't worry - the call doesn't start until we get a signal from both users!)
136+
</p>}
130137
<video
131138
playsInline
132139
ref={userVideo}
@@ -148,6 +155,7 @@ function attachSocketReceiver(
148155
stream: MediaStream,
149156
roomId: string,
150157
userVideo: React.RefObject<HTMLVideoElement>,
158+
setCallStarts: React.Dispatch<React.SetStateAction<boolean>>,
151159
connectionRef: React.MutableRefObject<Peer.Instance | undefined>,
152160
) {
153161
socket.on("startCall", (data) => {
@@ -168,6 +176,7 @@ function attachSocketReceiver(
168176
if (userVideo.current) {
169177
console.log("user video exists");
170178
userVideo.current.srcObject = stream;
179+
setCallStarts(true);
171180
}
172181
});
173182

@@ -181,6 +190,7 @@ function attachSocketInitiator(
181190
stream: MediaStream,
182191
roomId: string,
183192
userVideo: React.RefObject<HTMLVideoElement>,
193+
setCallStarts: React.Dispatch<React.SetStateAction<boolean>>,
184194
connectionRef: React.MutableRefObject<Peer.Instance | undefined>,
185195
) {
186196
socket.on("peerConnected", () => {
@@ -200,6 +210,7 @@ function attachSocketInitiator(
200210
if (userVideo.current) {
201211
console.log("setting stream for handshake");
202212
userVideo.current.srcObject = stream;
213+
setCallStarts(true);
203214
}
204215
});
205216

0 commit comments

Comments
 (0)