mirror of
https://github.com/cudr/slate-collaborative.git
synced 2024-10-27 20:34:06 +00:00
fix: socket io namespace message broadcast
This commit is contained in:
parent
5e0ee3e8d9
commit
ab3c36ab7d
@ -31,6 +31,7 @@ export default class SocketIOCollaboration {
|
|||||||
private io: SocketIO.Server
|
private io: SocketIO.Server
|
||||||
private options: SocketIOCollaborationOptions
|
private options: SocketIOCollaborationOptions
|
||||||
private backend: AutomergeBackend
|
private backend: AutomergeBackend
|
||||||
|
private autoSaveDoc: (docId: string) => void
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
@ -45,6 +46,15 @@ export default class SocketIOCollaboration {
|
|||||||
|
|
||||||
this.configure()
|
this.configure()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save document with throttle
|
||||||
|
*/
|
||||||
|
this.autoSaveDoc = throttle(
|
||||||
|
async (docId: string) =>
|
||||||
|
this.backend.getDocument(docId) && this.saveDocument(docId),
|
||||||
|
this.options?.saveFrequency || 2000
|
||||||
|
)
|
||||||
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,14 +118,15 @@ export default class SocketIOCollaboration {
|
|||||||
const { name } = socket.nsp
|
const { name } = socket.nsp
|
||||||
|
|
||||||
this.backend.createConnection(id, ({ type, payload }: CollabAction) => {
|
this.backend.createConnection(id, ({ type, payload }: CollabAction) => {
|
||||||
|
if (payload.docId === name) {
|
||||||
socket.emit('msg', { type, payload: { id: conn.id, ...payload } })
|
socket.emit('msg', { type, payload: { id: conn.id, ...payload } })
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
socket.on('msg', this.onMessage(id, name))
|
socket.on('msg', this.onMessage(id, name))
|
||||||
|
|
||||||
socket.on('disconnect', this.onDisconnect(id, socket))
|
socket.on('disconnect', this.onDisconnect(id, socket))
|
||||||
|
|
||||||
socket.join(id, () => {
|
|
||||||
const doc = this.backend.getDocument(name)
|
const doc = this.backend.getDocument(name)
|
||||||
|
|
||||||
socket.emit('msg', {
|
socket.emit('msg', {
|
||||||
@ -124,7 +135,6 @@ export default class SocketIOCollaboration {
|
|||||||
})
|
})
|
||||||
|
|
||||||
this.backend.openConnection(id)
|
this.backend.openConnection(id)
|
||||||
})
|
|
||||||
|
|
||||||
this.garbageCursors(name)
|
this.garbageCursors(name)
|
||||||
}
|
}
|
||||||
@ -148,16 +158,6 @@ export default class SocketIOCollaboration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Save document with throttle
|
|
||||||
*/
|
|
||||||
|
|
||||||
private autoSaveDoc = throttle(
|
|
||||||
async (docId: string) =>
|
|
||||||
this.backend.getDocument(docId) && this.saveDocument(docId),
|
|
||||||
this.options?.saveFrequency || 2000
|
|
||||||
)
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Save document
|
* Save document
|
||||||
*/
|
*/
|
||||||
@ -183,6 +183,8 @@ export default class SocketIOCollaboration {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
private onDisconnect = (id: string, socket: SocketIO.Socket) => async () => {
|
private onDisconnect = (id: string, socket: SocketIO.Socket) => async () => {
|
||||||
|
socket.leave(socket.nsp.name)
|
||||||
|
|
||||||
this.backend.closeConnection(id)
|
this.backend.closeConnection(id)
|
||||||
|
|
||||||
await this.saveDocument(socket.nsp.name)
|
await this.saveDocument(socket.nsp.name)
|
||||||
@ -219,8 +221,7 @@ export default class SocketIOCollaboration {
|
|||||||
garbageCursors = (nsp: string) => {
|
garbageCursors = (nsp: string) => {
|
||||||
const doc = this.backend.getDocument(nsp)
|
const doc = this.backend.getDocument(nsp)
|
||||||
|
|
||||||
if (doc == null || doc == undefined) return;
|
if (!doc || !doc.cursors) return
|
||||||
if (!doc.cursors) return
|
|
||||||
|
|
||||||
const namespace = this.io.of(nsp)
|
const namespace = this.io.of(nsp)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user