-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity-socket.js
More file actions
112 lines (93 loc) · 2.74 KB
/
activity-socket.js
File metadata and controls
112 lines (93 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// WebSocket client for real-time activity updates
class ActivitySocket {
constructor() {
this.socket = null;
this.connected = false;
this.listeners = new Map();
}
async connect() {
if (this.connected) return;
const token = await chrome.storage.local.get(['authToken']);
if (!token.authToken) {
console.error('No auth token found');
return;
}
// Load Socket.IO from CDN
if (!window.io) {
await this.loadSocketIO();
}
const serverUrl = 'https://focus-backend-g1zg.onrender.com';
this.socket = io(serverUrl, {
transports: ['websocket', 'polling']
});
this.socket.on('connect', () => {
console.log('🔌 Connected to activity server');
this.connected = true;
// Authenticate
this.socket.emit('authenticate', token.authToken);
});
this.socket.on('authenticated', (data) => {
console.log('✅ Authenticated:', data.userId);
});
this.socket.on('auth-error', (data) => {
console.error('❌ Auth error:', data.error);
});
this.socket.on('friend-activity', (data) => {
console.log('📊 Friend activity update:', data);
this.emit('activity-update', data);
});
this.socket.on('friend-online', (data) => {
console.log('Friend online:', data.username);
this.emit('friend-online', data);
});
this.socket.on('friend-offline', (data) => {
console.log('Friend offline:', data.username);
this.emit('friend-offline', data);
});
this.socket.on('disconnect', () => {
console.log('🔌 Disconnected from activity server');
this.connected = false;
});
}
async loadSocketIO() {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = 'https://cdn.socket.io/4.6.1/socket.io.min.js';
script.onload = resolve;
script.onerror = reject;
document.head.appendChild(script);
});
}
updateActivity(status, startTime = null, currentUrl = null, videoTitle = null) {
if (!this.connected || !this.socket) return;
this.socket.emit('activity-update', {
status,
startTime,
currentUrl,
videoTitle
});
}
on(event, callback) {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event).push(callback);
}
emit(event, data) {
const callbacks = this.listeners.get(event);
if (callbacks) {
callbacks.forEach(cb => cb(data));
}
}
disconnect() {
if (this.socket) {
this.socket.disconnect();
this.socket = null;
this.connected = false;
}
}
}
// Export for use in other files
if (typeof module !== 'undefined' && module.exports) {
module.exports = ActivitySocket;
}