-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjitsi-channel.js
More file actions
207 lines (185 loc) · 7.2 KB
/
Copy pathjitsi-channel.js
File metadata and controls
207 lines (185 loc) · 7.2 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Stub JitsiMeetJS event constants so index.html doesn't need to change.
const JitsiMeetJS = {
events: {
conference: {
USER_JOINED: 'USER_JOINED',
MESSAGE_RECEIVED: 'MESSAGE_RECEIVED',
USER_LEFT: 'USER_LEFT',
PARTICIPANT_PROPERTY_CHANGED: 'PARTICIPANT_PROPERTY_CHANGED',
ENDPOINT_MESSAGE_RECEIVED: 'ENDPOINT_MESSAGE_RECEIVED',
DISPLAY_NAME_CHANGED: 'DISPLAY_NAME_CHANGED',
TRACK_AUDIO_LEVEL_CHANGED: 'TRACK_AUDIO_LEVEL_CHANGED',
TRACK_ADDED: 'TRACK_ADDED',
TRACK_REMOVED: 'TRACK_REMOVED',
CONFERENCE_JOINED: 'CONFERENCE_JOINED',
},
},
createLocalTracks: () => Promise.resolve([]),
};
/**
* Drop-in replacement for the Jitsi-based JitsiConnection class.
* Uses wss://chatroom.openfun.app/ws instead.
*/
class JitsiConnection {
constructor() {
this.ws = null;
this.room = null;
this._members = {}; // keyed by userId
this._myUserId = null;
this._onConnectionFailed = null;
}
// Called by index.html before initConferenceRoom.
// We just call onConnectionSuccess immediately — the real WS connects in initConferenceRoom.
initConnection(onConnectionSuccess, onConnectionFailed) {
this._onConnectionFailed = onConnectionFailed;
setTimeout(onConnectionSuccess, 0);
}
// Compatibility shim — kept so index.html can set connection = wrappedConnection.connection
get connection() { return this; }
setLocalParticipantProperty(properties) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify({type: 'set-meta', meta: properties}));
}
}
setDisplayName(name) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify({type: 'set-meta', meta: {name}}));
}
}
initConferenceRoom(roomID, displayName, participantProperty, onTrackAdded, onConferenceJoined) {
const members = this._members;
const self = this;
const handlers = {};
const room = {
// Proxy so callers can do: participants[id].getProperty(key) / getDisplayName()
participants: new Proxy(members, {
get(target, id) {
if (!Object.prototype.hasOwnProperty.call(target, id)) {
return Reflect.get(target, id);
}
const m = target[id];
return {
getProperty: (key) => (m.meta || {})[key],
getDisplayName: () => m.username,
};
},
has(target, id) { return id in target; },
ownKeys(target) { return Object.keys(target); },
getOwnPropertyDescriptor(target, id) {
if (id in target) return {enumerable: true, configurable: true, value: target[id]};
},
}),
on(eventName, handler) { handlers[eventName] = handler; },
sendTextMessage(text) {
if (!self.ws || self.ws.readyState !== WebSocket.OPEN) return;
self.ws.send(JSON.stringify({type: 'say', payload: {type: 'chat', message: text}}));
},
broadcastEndpointMessage(msg) {
if (!self.ws || self.ws.readyState !== WebSocket.OPEN) return;
self.ws.send(JSON.stringify({type: 'say', payload: msg}));
},
setLocalParticipantProperty(key, value) {
if (!self.ws || self.ws.readyState !== WebSocket.OPEN) return;
const meta = {};
meta[key] = value;
self.ws.send(JSON.stringify({type: 'set-meta', meta}));
},
getParticipantCount() {
return Object.keys(members).length;
},
};
this.room = room;
const ws = new WebSocket('wss://chatroom.openfun.app/ws');
this.ws = ws;
ws.onopen = () => {
ws.send(JSON.stringify({
type: 'join',
room: roomID,
username: displayName,
meta: participantProperty,
}));
};
ws.onmessage = (event) => {
const msg = JSON.parse(event.data);
if (msg.type === 'joined') {
self._myUserId = msg.userId;
(msg.members || []).forEach((m) => {
if (m.userId !== self._myUserId) members[m.userId] = m;
});
onConferenceJoined();
// Replay chat history
(msg.history || []).forEach((h) => {
if (h.type === 'say' && h.payload && h.payload.type === 'chat') {
const handler = handlers[JitsiMeetJS.events.conference.MESSAGE_RECEIVED];
if (handler) {
const ts = h.timestamp ? new Date(h.timestamp).getTime() : undefined;
handler(h.userId, h.payload.message, ts, h.username, {});
}
}
});
} else if (msg.type === 'user-joined') {
members[msg.userId] = {userId: msg.userId, username: msg.username, meta: msg.meta || {}};
const handler = handlers[JitsiMeetJS.events.conference.USER_JOINED];
if (handler) handler(msg.userId, {getDisplayName: () => msg.username});
} else if (msg.type === 'user-left') {
const username = members[msg.userId] ? members[msg.userId].username : 'unknown';
delete members[msg.userId];
const handler = handlers[JitsiMeetJS.events.conference.USER_LEFT];
if (handler) handler(msg.userId, {getDisplayName: () => username});
} else if (msg.type === 'say') {
if (msg.userId === self._myUserId) return;
const payload = msg.payload || {};
if (payload.type === 'chat') {
const handler = handlers[JitsiMeetJS.events.conference.MESSAGE_RECEIVED];
if (handler) {
const ts = msg.timestamp ? new Date(msg.timestamp).getTime() : undefined;
handler(msg.userId, payload.message, ts, msg.username, {});
}
} else {
// teleport and any other endpoint messages
const handler = handlers[JitsiMeetJS.events.conference.ENDPOINT_MESSAGE_RECEIVED];
if (handler) handler({getId: () => msg.userId}, payload);
}
} else if (msg.type === 'meta-updated') {
if (msg.userId === self._myUserId) return;
if (!members[msg.userId]) {
members[msg.userId] = {userId: msg.userId, username: msg.username || 'unknown', meta: {}};
}
Object.assign(members[msg.userId].meta, msg.meta || {});
if (msg.username) members[msg.userId].username = msg.username;
if (msg.meta) {
// Fire PARTICIPANT_PROPERTY_CHANGED for each changed positional/appearance key
const propHandler = handlers[JitsiMeetJS.events.conference.PARTICIPANT_PROPERTY_CHANGED];
if (propHandler) {
for (const key of ['top', 'left', 'character']) {
if (msg.meta[key] !== undefined) {
propHandler({getId: () => msg.userId}, key, undefined);
}
}
}
// Fire DISPLAY_NAME_CHANGED if name changed
if (msg.meta.name) {
const nameHandler = handlers[JitsiMeetJS.events.conference.DISPLAY_NAME_CHANGED];
if (nameHandler) nameHandler(msg.userId, msg.meta.name);
}
}
} else if (msg.type === 'error') {
console.error('WS error:', msg.code);
}
};
ws.onerror = () => {
if (self._onConnectionFailed) self._onConnectionFailed();
};
ws.onclose = () => {
self.room = null;
};
return room;
}
disconnect() {
if (this.ws) {
this.ws.close();
this.ws = null;
}
this.room = null;
}
}